android - 无法在 Android Canvas 中缩放 gif 移动框架

标签 android scale android-canvas gif

我在缩放 GIF 电影帧时遇到问题。我在绘制框架之前使用 Canvas.scale(float sx, float sy) 。但它不绘制缩放框架,它什么也不画。有什么想法吗?

最佳答案

如果您尝试制作一个可缩放且可在另一个布局中重复使用的专用 View ,您还应该使用 View 的 getWidth() 和 getHeight() 方法。

我在更改View的专用尺寸时遇到了这个问题:canvas.getWidth()是整个屏幕的宽度,而不是 View 的宽度,所以我修改了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 语句检查电影是否大于 View ,在我的例子中我的 View 高度是 160 但我的 GIF 是 260,但是使用其他方法它总是绘制GIF 太大了,通过这些更改,我能够测试并看到它在我的 View 为 1280 X 900 且图像按比例放大以及我的 View 为 1000 X 160 并按比例缩小的两种情况下都有效。

祝你好运,玩得开心:D

-克里斯

关于android - 无法在 Android Canvas 中缩放 gif 移动框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11702773/

相关文章:

android - 在 MapView 上绘制透明

android - 获取 Canvas 的一部分作为位图

android - 什么是网络挂起状态

wcf - 哪种架构更具可扩展性

android - 导航 View 菜单项不响应任何单击

ios - Box2D 缩放说明

r - r中的选择性缩放函数使用不同的数据框进行缩放

android - 绘制填充外边界的矩形

android - 将参数传递给 fragment

android - android bootanimation子进程影响其父进程init内存占用的怪问题