2015-02-09 149 views
2

例如:myPos.getCoordX()是0和myPos.getCoordY()是0如何使動畫當我使用畫布

當我更新變量,myPos.getCoordX()是50和myPos.getCoordY()是100

然後我再次調用showPosition()方法。

我想:當你不使用自定義視圖和的onDraw方法來更新你的畫布開始從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; 
    } 

回答

2

,我建議使用屬性動畫製作動畫畫布。

下面是一個用canvas.translate(value,0);在X中移動視圖的示例動畫的代碼。您需要添加Y值,例如:canvas.translate(valueX,valueY);.只需爲Y定義另一個ValueAnimator,創建一個AnimatorSet,然後使用animatorSet.start()完成animatorSet.playTogether(valueanimatorX,valueanimatorY);

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

謝謝讓我試試 – 2015-02-22 06:06:11