2

谁能帮我做到这一点。我正在尝试构建一个 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 显示器内连续移动图像。任何人都可以提出解决方案。

提前致谢 :)

4

1 回答 1

-3
 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();
   }
}

尝试这个

于 2012-12-04T07:11:13.930 回答