简体   繁体   中英

android doesn't show gif

I'm new in android development, so I have the following issue. I'm trying to show gif green.gif (res/drawable-xxhdpi/) via this code: GIFView class:

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:

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

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:

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

just add a null check in your code and try to decode the gif file in another way. take a look here :

Failed to decode GIF

or you could try to move your GIF to the assets folder as suggested here : SO Answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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