2012-07-28 17 views

回答

1

这应该工作。

protected void onDraw(Canvas canvas) 
{ 
    canvas.drawColor(Color.TRANSPARENT); 

    canvas.scale(0.5f, 0.5f); // scale gif to its half size 

    super.onDraw(canvas); 

    /* movie.setTime . . . */ 

    movie.draw(canvas, ...); 

    this.invalidate(); 

} 
2

如果你想做一个专门的观点是规模,能和可重用的另一个布局中,你应该使用视图的的getWidth()和getHeight()方法为好。 canvas.getWidth()在整个屏幕的宽度,而不是意见宽度,使我修改的onDraw方法如下:

我当改变视图的专用尺寸就遇到了这个问题。

@Override 
protected void onDraw(Canvas canvas) { 
    canvas.drawColor(Color.TRANSPARENT); 
    super.onDraw(canvas); 

    long now = android.os.SystemClock.uptimeMillis(); 

    Paint p = new Paint(); 
    p.setAntiAlias(true); 

    if (cur == 0) cur = now; 

    if(movie != null) 
    { 
     float scale = 1f; 
     if(movie.height() > getHeight() || movie.width() > getWidth()) 
      scale = (1f/Math.min(canvas.getHeight()/movie.height(), canvas.getWidth()/movie.width())) + 0.25f; 
     else  
      scale = Math.min(canvas.getHeight()/movie.height(), canvas.getWidth()/movie.width()); 

     canvas.scale(scale, scale); 
     canvas.translate(((float)getWidth()/scale - (float)movie.width())/2f, 
       ((float)getHeight()/scale - (float)movie.height())/2f); 

     int relTime = (int)((now - cur) % movie.duration()); 
     movie.setTime(relTime); 
     movie.draw(canvas, 0, 0); 


     invalidate(); 
    } 
} 

通知之的getWidth()的getHeight()if语句检查,看看电影是超过视图大小,在我的情况,我的看法高度为160,但我的GIF为260,但使用另一种方法是总是会画出GIF太大的图像,在这两种情况下,我都能够测试并看到它的工作情况,在这两种情况下,我的视图是1280 X 900,图像放大了,我的视图也是1000 X 160,并缩小了比例。

最好的运气,和有乐趣:d

克里斯

+0

我发现,只有使用绘图代码的“其他”语句产生更好的结果 - 即。它扩展到所有设备。我在我的项目中混淆了'if'陈述。 – EricRobertBrewer 2016-04-08 23:42:06