我在使用canvas时如何制作动画

时间:2015-02-09 11:24:22

标签: 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;
    }

1 个答案:

答案 0 :(得分:2)

由于您没有使用自定义视图和onDraw方法来更新画布,我建议使用属性动画制作工具来为画布绘制设置动画。

下面是使用canvas.translate(value,0);在X中移动视图的示例动画的代码。您需要添加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();
}