imageview不跟踪路径android

发布于 2024-12-11 05:38:28 字数 1318 浏览 0 评论 0原文

我正在使用翻译动画来使 imageview 跟踪一条路径。最初,我只是让我的 imageview 翻译一组特定的点,但事实并非如此。这是我在 ondraw 方法中的代码:

@Override
    public void onDraw(Canvas canvas) {
         Log.w(this.getClass().getName(),"onDraw of Balls called");
      super.onDraw(canvas);
      mCanvas = canvas;



         canvas.drawLine(0, 240, 160, 0, mPaint);
         canvas.drawLine(160, 0, 320, 240, mPaint);

         mBal = (ImageView)findViewById(R.id.strike);
         TranslateAnimation mTrans = new TranslateAnimation(0, 240, 160, 0);
         mTrans.setDuration(6000);
         mTrans.setFillEnabled(true);
         mTrans.setFillAfter(true);
         mTrans.start();

    }

请帮助。

=================================================== ===========

编辑 1:-

这是我修改后的代码,但翻译仍然不起作用。PLz 帮助

@Override
    public void onDraw(Canvas canvas) {

      Log.w(this.getClass().getName(),"onDraw of Balls called");

      BallsOnDraw(canvas);

    }

     void BallsOnDraw(Canvas canvas)
     {

            canvas.drawLine(0, 240, 160, 0, mPaint);
            canvas.drawLine(160, 0, 320, 240, mPaint);

            TranslateAnimation mTrans = new TranslateAnimation(0, 320, 0,240);

            mTrans.setDuration(6000);
            SitoliaActivity.mBal.startAnimation(mTrans);


     }

I am using translate animation to make imageview trace a path.Initially I am just making my imageview to translate through a particular set of points but it does not.here is my code in ondraw method:

@Override
    public void onDraw(Canvas canvas) {
         Log.w(this.getClass().getName(),"onDraw of Balls called");
      super.onDraw(canvas);
      mCanvas = canvas;



         canvas.drawLine(0, 240, 160, 0, mPaint);
         canvas.drawLine(160, 0, 320, 240, mPaint);

         mBal = (ImageView)findViewById(R.id.strike);
         TranslateAnimation mTrans = new TranslateAnimation(0, 240, 160, 0);
         mTrans.setDuration(6000);
         mTrans.setFillEnabled(true);
         mTrans.setFillAfter(true);
         mTrans.start();

    }

plz help.

=============================================================

Edit 1:-

This is my modified code but the translation is still not working.PLz help

@Override
    public void onDraw(Canvas canvas) {

      Log.w(this.getClass().getName(),"onDraw of Balls called");

      BallsOnDraw(canvas);

    }

     void BallsOnDraw(Canvas canvas)
     {

            canvas.drawLine(0, 240, 160, 0, mPaint);
            canvas.drawLine(160, 0, 320, 240, mPaint);

            TranslateAnimation mTrans = new TranslateAnimation(0, 320, 0,240);

            mTrans.setDuration(6000);
            SitoliaActivity.mBal.startAnimation(mTrans);


     }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一张白纸 2024-12-18 05:38:28

您将自定义类与 ImageView 和动画混合在一起。恕我直言,你应该只使用其中一种方法。另外,我没有看到您实际上将 TranslationAnimation 与 ImageView 本身连接起来。

因此,一种解决方案是使用 TranslateAnimation,但随后您需要调用 mBal.startAnimation(mTrans) 而不是 mTrans.start()。

另一种解决方案是使用自定义视图,重写 onDraw,并自己处理动画:

  • 保存当前时间 (System.curentTimeMillis)
  • 使用 canvas.drawBitmap(bitmap, x, y, Paint) 将位图直接绘制到画布上;
  • 根据经过的时间更新坐标
  • 如果动画仍应运行,则

,请调用 postInvalidateDelayed(40);这是一个自定义视图示例:

public class C extends View {

    private static final float FROM_X = 400;
    private static final float FROM_Y = 100;
    private static final float TO_X = 10;
    private static final float TO_Y = 400;
    private static final int DURATION = 2*1000; // 2 seconds

    private Bitmap mImage;
    private long mStart = -1;
    private Paint mPaint = new Paint();

    public C(Context context) {
        super(context);
        mImage = ((BitmapDrawable)context.getResources().getDrawable(R.drawable.icon)).getBitmap();
    }

    @Override
    public void onDraw(Canvas canvas) {
        boolean finished = false;
        if (mStart == -1) {
            // Time to start the animation
            mStart = SystemClock.uptimeMillis();
        }
        int elapsed = (int)(SystemClock.uptimeMillis() - mStart);
        if (elapsed >= DURATION) {
            elapsed = DURATION;
            finished = true;
        }
        float x = FROM_X + (TO_X - FROM_X) * elapsed / DURATION;
        float y = FROM_Y + (TO_Y - FROM_Y) * elapsed / DURATION;
        canvas.drawBitmap(mImage, x, y, mPaint);
        if (!finished) {
            postInvalidateDelayed(10);
        }
    }

}

You are mixing custom classes with ImageViews and animations. IMHO you should use only one of the methods. Also I don't see that you actually connect the TranslationAnimation with the ImageView itself.

So, one solution would be to use TranslateAnimation, but then you would need to call mBal.startAnimation(mTrans) instead of mTrans.start().

The other solution would be to use a custom view, override onDraw, and handle the animation yourself:

  • save the current time (System.curentTimeMillis)
  • draw the Bitmap directly to the Canvas using canvas.drawBitmap(bitmap, x, y, paint);
  • update the coordinates based on the elapsed time
  • if the animation should still run, call postInvalidateDelayed(40);

Here is an example custom view:

public class C extends View {

    private static final float FROM_X = 400;
    private static final float FROM_Y = 100;
    private static final float TO_X = 10;
    private static final float TO_Y = 400;
    private static final int DURATION = 2*1000; // 2 seconds

    private Bitmap mImage;
    private long mStart = -1;
    private Paint mPaint = new Paint();

    public C(Context context) {
        super(context);
        mImage = ((BitmapDrawable)context.getResources().getDrawable(R.drawable.icon)).getBitmap();
    }

    @Override
    public void onDraw(Canvas canvas) {
        boolean finished = false;
        if (mStart == -1) {
            // Time to start the animation
            mStart = SystemClock.uptimeMillis();
        }
        int elapsed = (int)(SystemClock.uptimeMillis() - mStart);
        if (elapsed >= DURATION) {
            elapsed = DURATION;
            finished = true;
        }
        float x = FROM_X + (TO_X - FROM_X) * elapsed / DURATION;
        float y = FROM_Y + (TO_Y - FROM_Y) * elapsed / DURATION;
        canvas.drawBitmap(mImage, x, y, mPaint);
        if (!finished) {
            postInvalidateDelayed(10);
        }
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文