android - 如何在使用 Canvas 时制作动画

标签 android animation imageview

例如:myPos.getCoordX()为0,myPos.getCoordY()为0

当我更新变量时,myPos.getCoordX() 是 50 而 myPos.getCoordY() 是 100

然后我再次调用 showPosition() 方法。

我想:开始一个从 0,0 到 50,100 的移动动画

private void showPosition() {
        Bitmap floorPlan = BitmapFactory.decodeResource(getResources(),
                R.drawable.wallpaper).copy(Bitmap.Config.ARGB_4444, true);
        Bitmap point = BitmapFactory.decodeResource(getResources(),
                R.drawable.ic_launcher).copy(Bitmap.Config.ARGB_4444, true);
        Bitmap PositionOnFloorplan = overlay(floorPlan, point);


        // 1
        PositionOnFloorplan = floorPlan.copy(Bitmap.Config.ARGB_8888, true);

        Canvas canvas = new Canvas(PositionOnFloorplan);
        canvas.drawBitmap(point, (float) (myPos.getCoordX()),
                (float) (myPos.getCoordY()), null);

        ImageView imageView = (ImageView) findViewById(R.id.imggg);
        imageView.setAdjustViewBounds(true);
        imageView.setImageBitmap(PositionOnFloorplan);

    }


    private Bitmap overlay(Bitmap floorPlan, Bitmap point) {
        Bitmap bmOverlay = Bitmap.createBitmap(floorPlan.getWidth(),
                floorPlan.getHeight(), floorPlan.getConfig());
        Canvas canvas = new Canvas(bmOverlay);
        canvas.drawBitmap(floorPlan, new Matrix(), null);
        canvas.drawBitmap(point, (float) (myPos.getCoordX()),
                (float) (myPos.getCoordY()), null);
        return bmOverlay;
    }

最佳答案

由于您没有使用自定义 View 和 onDraw 方法来更新 Canvas ,我建议使用属性动画器来为 Canvas 绘图设置动画。

下面是一个示例动画的代码,它使用 canvas.translate(value,0); 在 X 中移动 View 。您需要添加 Y 值,例如:canvas.translate(valueX,valueY);。只需为 Y 定义另一个 ValueAnimator,创建一个 AnimatorSet,然后执行 animatorSet.playTogether(valueanimatorX, valueanimatorY),以 animatorSet.start() 结束;

public void doCanvas(){
    //Create our resources
    Bitmap bitmap = Bitmap.createBitmap(mLittleChef.getWidth(), mLittleChef.getHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);
    final Bitmap chefBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.dish_special);
    final Bitmap starBitmap= BitmapFactory.decodeResource(getResources(),R.drawable.star);

    //Link the canvas to our ImageView
    mLittleChef.setImageBitmap(bitmap);

    //animate from canvas width, to 0, and back to canvas width
    ValueAnimator animation= ValueAnimator.ofInt(canvas.getWidth(),0,canvas.getWidth());
    animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            int value = (Integer) animation.getAnimatedValue();
            //Clear the canvas
            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
            canvas.drawBitmap(chefBitmap, 0, 0, null);
            canvas.save();
            canvas.translate(value,0);
            canvas.drawBitmap(starBitmap, 0, 0, null);
            canvas.restore();
            //Need to manually call invalidate to redraw the view
            mLittleChef.invalidate();
        }
    });
    animation.addListener(new AnimatorListenerAdapter(){
        @Override
        public void onAnimationEnd(Animator animation) {
            simpleLock= false;
        }
    });
    animation.setInterpolator(new LinearInterpolator());
    animation.setDuration(mShortAnimationDuration);
    animation.start();
}

关于android - 如何在使用 Canvas 时制作动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28408608/

相关文章:

android - 使用 rxjava 重复条目转换异常

Android SQLite 数据库损坏

java - Android ImageView 不显示图像?

java - 无法保存使用应用程序拍摄的图像?

java - 为什么我在解析 json 响应时看到最后一项?

java - Android:动画 - 如何确定 xml 文件动画的结束并在 java 中启动 AlphaAnim?

ios - 如何在 UIImageView.animationImages 之后清除内存?

html - 类删除的完整动画

android - ImageView 的固定高度和宽度

java - 如何在 Ludo 棋盘中使用数组索引