0
我是android開發新手,所以我有以下問題。我想通過這個代碼顯示GIF green.gif(RES /繪製-xxhdpi /): GIFView類:android不顯示gif
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());
似乎沒有電影初始化,但爲什麼?
如果我還在的onDraw方法的開頭添加
System.out.println(movie.duration());
,它給了我2100ms,這是正確的值。它完全避開了我。
移動到文件夾的資產?這是什麼意思? – user1933770
把你的gif從project-base/res/drawable移到project-base/assets/ –
它沒有幫助,我會試試第一種方法... – user1933770