简体   繁体   English

android不显示gif

[英]android doesn't show gif

I'm new in android development, so I have the following issue. 我是android开发的新手,所以有以下问题。 I'm trying to show gif green.gif (res/drawable-xxhdpi/) via this code: GIFView class: 我正在尝试通过以下代码显示gif green.gif(res / drawable-xxhdpi /):GIFView类:

package ru.rkarasev.miptrain;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View;

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.green);
        movie = Movie.decodeStream(is);
    }

    public GIFView(Context context, AttributeSet attrs) {
        super(context, attrs);
        is = getResources().openRawResource(R.drawable.green);
        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, 100, 100);
        this.invalidate();
    }
}

XML file part: XML文件部分:

<TableRow>
     <TextView 
        android:id="@+id/final_1"
        android:width="200dp"
        android:textSize="25sp"/>
      <ru.rkarasev.miptrain.GIFView
        android:layout_marginLeft="30dp" 
        android:layout_gravity="center"
        android:layout_width="wrap_content" 
        android:layout_height="50dp"
        android:id="@+id/greenarrow">
        </ru.rkarasev.miptrain.GIFView>

     <TextView 
        android:id="@+id/time_1"
        android:text=""
        android:textSize="30sp"/>
</TableRow>

Activity code: 活动代码:

GIFView gifview = (GIFView) findViewById(R.id.greenarrow);

But the space where this view should be is empty, it doesn't show anything! 但是此视图应为空的空间,什么也没有显示! Could you tell me what's the problem? 你能告诉我什么问题吗?

UPD: When I open activity_main.xml file, it doesn't show how my layout will look but give me this error: UPD:当我打开activity_main.xml文件时,它没有显示布局外观,但出现了以下错误:

java.lang.NullPointerException
    at ru.rkarasev.miptrain.GIFView.onDraw(GIFView.java:34)
    at android.view.View.draw(View.java:13707)
    at android.view.View.draw(View.java:13591)
    at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
    at android.view.View.draw(View.java:13589)
    at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
    at android.view.View.draw(View.java:13589)
    at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
    at android.view.View.draw(View.java:13710)
    at android.view.View.draw(View.java:13591)
    at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
    at android.view.View.draw(View.java:13589)
    at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
    at android.view.View.draw(View.java:13710)
    at android.view.View.draw(View.java:13591)
    at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
    at android.view.View.draw(View.java:13710)

34th string in GIFView is: GIFView中的第34个字符串是:

int relTime = (int)((now - moviestart) % movie.duration());

It seems like there was no movie initialized, but why? 好像没有电影初始化,但是为什么呢?

If I also add 如果我还添加

System.out.println(movie.duration());

in the beginning of onDraw method, it gives me 2100ms, which is correct value. 在onDraw方法的开始,它给了我2100ms,这是正确的值。 It eludes me at all. 这根本使我难以理解。

It seems like your Movie object is null, and after tracing the source code of Movie Class it fails silently (by catching the thrown IOException) and returns a null object. 看来您的Movie对象为null,并且在跟踪Movie Class的源代码之后,它会静默失败(通过捕获抛出的IOException),并返回一个null对象。

just add a null check in your code and try to decode the gif file in another way. 只需在代码中添加一个空检查,然后尝试以另一种方式解码gif文件。 take a look here : 在这里看看:

Failed to decode GIF 无法解码GIF

or you could try to move your GIF to the assets folder as suggested here : SO Answer 或者您可以尝试将GIF移至此处建议的资产文件夹: SO解答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM