2

我有一张图像,例如下面示出的图像: sample image source 3dwallpapersAnimate(缩放和翻译)Android画布视图的一部分图像

该图像有两个部分尺寸宽度的第1部分W的宽度和高度大号和第2部分(较小的部分)瓦特和高度ħ(呼叫第一部分作为来源和第2部分作为目的地)。设第1个矩形的坐标为(从左上角开始测量):top:100 left:10 right:200 bottom:300第2个矩形的坐标为(从左上角开始测量):top 50 left:500底部:100右:700

我想动画从源到目的地,使图像翻译和放大从源到目的地。

所以我的第一个画面看起来像: enter image description here

和我的第二个图像看起来像: enter image description here

如何在这两者之间吐温? 我的代码(不包括动画)看起来像如下:

public class SuperGame extends View implements OnGestureListener 
{ 
    int sreenHeight, screenWidth; 
    int drawCount = 0; 
    Bitmap bg; 

    public SuperGame(Context context, Bitmap bmp) 
    { 
     this.screenWidth = getContext().getResources().getDisplayMetrics().widthPixels; 
    this.screenHeight = getContext().getResources().getDisplayMetrics().heightPixels; 
     bg = BitmapFactory.decodeResource(getResources(),R.drawable.bg_img); 
    } 

    @Override 
    /* this function triggers the image change 
    */ 
    public boolean onDown(MotionEvent e) { 
    invalidate(0,0,screenWidth, screenHeight); 
    return true; 
} 

    @Override 
    public void onDraw(Canvas canvas) 
    { 
     Rect dest = new Rect(0,0,screenWidth, screenHeight); 
     if (count==0) //draw the first image part 
     { 
      count=1; 
      Rect src = new Rect(100,10,200,300);//coordinates of rectangle 1  
      canvas.drawBitmap(bg, src, dest, new Paint()); 
     } 
     else //draw the second image part - (I'd like to show movement/ transition between these) 
     { 
      Rect src = new Rect(50,500,100,700);//coordinates of rectangle 2  
      count=0; 
      canvas.drawBitmap(bg, src, dest, new Paint()); 
     } 
    } 
} 

如何我设置过渡动画效果(这涉及zoomin以及翻译)?

+0

Hi @ User42,我有相同的功能来实现。我必须在屏幕上显示图像的一部分,当用户滑动屏幕时,我必须更新屏幕的其他部分。它应该有从一个位置移动到另一个位置的动画。您可以通过查看解决方案来分享您的代码吗? – Noundla

回答

1

您可能需要创建一个类型为Rect/RectF的自定义ValueAnimator。 代码举例如下:

public class RectFEvaluator 
    implements TypeEvaluator<RectF> 
{ 

    @Override 
    public RectF evaluate(float fraction, RectF srcRect, RectF destRect) { 
     RectF betweenRect = new RectF(); 
     betweenRect.bottom = srcRect.bottom + fraction*(destRect.bottom-srcRect.bottom); 
     betweenRect.top = srcRect.top + fraction*(destRect.top-srcRect.top); 
     betweenRect.left = srcRect.left + fraction*(destRect.left-srcRect.left); 
     betweenRect.right = srcRect.right + fraction*(destRect.right-srcRect.right); 
     return betweenRect; 
    } 

} 

值动画师给你的src的Rect和dest矩形之间的中间值。 一旦你有这些值,使用动画更新它们