0

我是android开发的新手,所以我有以下问题。我正在尝试通过以下代码显示 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文件部分:

<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>

活动代码:

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

但是这个视图应该是空的,它没有显示任何东西!你能告诉我有什么问题吗?

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)

GIFView 中的第 34 个字符串是:

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

似乎没有初始化电影,但为什么呢?

如果我也添加

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

在 onDraw 方法的开头,它给了我 2100 毫秒,这是正确的值。它完全避开了我。

4

1 回答 1

0

看起来您的 Movie 对象为 null,并且在跟踪Movie Class的源代码后,它会默默地失败(通过捕获抛出的 IOException)并返回一个 null 对象。

只需在您的代码中添加一个空检查并尝试以另一种方式解码 gif 文件。看看这里:

GIF解码失败

或者您可以尝试按照此处的建议将您的 GIF 移动到资产文件夹:SO Answer

于 2013-04-02T08:14:48.323 回答