在android中的显示内随机移动图像

时间:2012-12-04 06:49:30

标签: android android-layout animation android-intent android-animation

任何人都可以帮助我这样做。 我正在尝试构建一个Android应用程序,但卡在其间。

我使用以下代码移动图像。 iv是ImageView对象

moveImage = new TranslateAnimation( 0, xDest, 0, -yDest);
moveImage.setDuration(1000);
moveImage.setFillAfter( true );
iv.startAnimation(moveImage);

代码:

public class gameLogic extends Activity 
{
ImageView image;
TranslateAnimation moveImage;

public void onCreate(Bundle icicle) 
{
    super.onCreate(icicle);
    setContentView(R.layout.game_logic);
    imageMoveRandom(imageList(image,0));
}

ImageView imageList(ImageView v,int i)
{
    v = (ImageView) findViewById(R.id.rabbit);
    int imgId;
    int j = i;

    TypedArray imgs = getResources().obtainTypedArray(R.array.random_imgs);
    //get resourceid by index

    imgId = imgs.getResourceId(j, 0);
    // or set you ImageView's resource to the id
    v.setBackgroundResource(imgId);

    return v;

}

void imageMoveRandom(ImageView iv)throws NotFoundException
{
    DisplayMetrics dm = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics( dm );

    int xDest = dm.widthPixels/2;
    int yDest = dm.heightPixels/2;

//      Toast.makeText(gameLogic.this, dm.widthPixels, Toast.LENGTH_SHORT).show();
        //      Toast.makeText(this, dm.heightPixels, 2000).show();


        moveImage = new TranslateAnimation( 0, xDest, 0, -yDest);
        moveImage.setDuration(1000);
        moveImage.setFillAfter( true );
        iv.startAnimation(moveImage);
        //moveImage.reset();
    }
}

上面不是完整的代码..但是可能对参考文献有用的部分。

但我想在随机的地方继续移动图像,但在android显示器内。 任何人都可以建议解决方案。

提前致谢:)

1 个答案:

答案 0 :(得分:-2)

 class BitmapView extends View
 {
   changingX=10;
   changingY=10;
   public BitmapView(Context context) {
        super(context);
   }
   @Override
   public void onDraw(Canvas canvas) {
   Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.yourImageName);
    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap(bmp, changingX,changingY, null);
    changingX=changingX+5;
    changingY=changingY+10;
    invalidate();
   }
}

试试这个

相关问题