如何在android中更改gif图片大小

时间:2012-12-17 23:46:24

标签: java android android-canvas animated-gif

现在我知道如何在android中绘制GIF图片,这里是代码:

public class GIFView extends View{        
private Movie movie;  
private InputStream is;  
private long moviestart;  
public GIFView(Context context) {  
    super(context);
    is=getResources().openRawResource(R.drawable.anim_cerca);  
    movie=Movie.decodeStream(is);
}  

@Override  
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    long now=android.os.SystemClock.uptimeMillis();  

    if (moviestart == 0) 
        moviestart = now;  

    int relTime = (int)((now - moviestart) % movie.duration());
    movie.setTime(relTime);
    movie.draw(canvas,10,10);
    this.invalidate();
}  

}

最后,有movie.draw(canvas,x,y)代码,其中x和y是gif图片的坐标(x = left,y = top)。但是我怎么能改变电影的宽度和高度呢?也许给出正确和最低的坐标,但是如何,在哪里?谢谢!

2 个答案:

答案 0 :(得分:17)

我遇到同样的问题是我的解决方案, 在movie.draw

之前在ondraw方法中添加此代码
 canvas.scale((float)this.getWidth() / (float)movie.width(),(float)this.getHeight() /      (float)movie.height());

canvas.scale(1.9f, 1.21f);//this changes according to screen size 

答案 1 :(得分:0)

您可以使用

  implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.4'

图书馆 然后将其添加到xml

 android:scaleType="fitXY"