使用 android.graphics.Movie 显示 .gif [英] Show .gif with android.graphics.Movie

查看:24
本文介绍了使用 android.graphics.Movie 显示 .gif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用动画 GIF 创建视图..

I'm trying, create a view with an animated GIF..

当我尝试在模拟器中运行以下代码时,一切正常.但是当我尝试在真正的智能手机上运行时,没有任何反应..

When i try run the follow code in emulator all works fine. But when i try run in real Smart Phone, nothing happens..

我的观点:

public class GIFView extends View {

private Movie mMovie;
private long movieStart;

public GIFView(Context context) {
    super(context);
    initializeView();
}

public GIFView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initializeView();
}

public GIFView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initializeView();
}

private void initializeView() {
    InputStream is = getContext().getResources().openRawResource(
            R.drawable.cookies2);
    mMovie = Movie.decodeStream(is);
}

protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.TRANSPARENT);
    super.onDraw(canvas);
    long now = android.os.SystemClock.uptimeMillis();

    if (movieStart == 0) {
        movieStart = (int) now;
    }
    if (mMovie != null) {
        int relTime = (int) ((now - movieStart) % mMovie.duration());
        mMovie.setTime(relTime);
        mMovie.draw(canvas, getWidth() - mMovie.width(), getHeight()
                - mMovie.height());
        this.invalidate();
    }
}}

我的活动:

public class MainActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     GIFView gifView = new GIFView(this);
     setContentView(gifView);
}}

我的智能手机截图:我的模拟器截图:

My Smartphone screenshot: My emulator screenshot:

为什么我的应用无法在智能手机上运行?

Why my app doesn't run in smartphone?

推荐答案

不要关闭整个应用程序的硬件加速.那是瘫痪.只需将其关闭即可查看:

Don't turn off hardware acceleration for the whole application. That's crippling. Just turn it off for the view:

setLayerType(View.LAYER_TYPE_SOFTWARE, null);

这篇关于使用 android.graphics.Movie 显示 .gif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆