【问题标题】:Move an Image continously in Random position within the display in android在 android 的显示中以随机位置连续移动图像
【发布时间】:2012-12-04 06:49:29
【问题描述】:

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

在此先感谢 :)

【问题讨论】:

  • 你找到解决办法了吗?
  • @Chetan 我也在寻找...你找到解决方案了吗?

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


【解决方案1】:
 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();
   }
}

试试这个

【讨论】:

  • 谢谢!!!但是您能否使用 AnimationTranslation 提供上述代码中的更改 ..因为我一直在应用程序中间并且我无法更改它,因为它会影响其他代码.. 下面的代码: void imageMoveRandom(ImageView iv)throws NotFoundException { DisplayMetrics dm = 新的 DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(dm); int xDest = dm.widthPixels/2; int yDest = dm.heightPixels/2; moveImage = new TranslateAnimation(0, 0, 0, -yDest); moveImage.setDuration(1000); moveImage.setFillAfter(true); iv.startAnimation(moveImage); } }
  • 通过编辑将此代码写入您的问题。将您所有的 java 代码放在这里。
猜你喜欢
  • 1970-01-01
  • 2014-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 1970-01-01
  • 2015-10-28
  • 2010-11-08
相关资源
最近更新 更多