Play Gif in either WebView or Custom View

亡梦爱人 提交于 2019-12-12 14:03:05

问题


In a nutshell, I have an Activity A, which applied setContentView(R.id.layout_a), and A will run for 5 seconds.

I tried to define a custom View class and using Movie class to load the gif, then attach the custom view to layout_a but it displays just blank.

The WebView method

I tried WebView, it can display the gif well, but the gif can animate only the first time I launch this activity. It will just show the last frame when later activity A is launched.

I tried WebView.reload() but no sweet.

Here's my code when trying to apply the WebView:

public class activityA extends Activity {

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.activity_a);

    WebView splashFrame = (WebView)findViewById(R.id.splashFrame);

    splashFrame.setBackgroundColor(Color.TRANSPARENT);
    splashFrame.loadDataWithBaseURL("file:///android_res/drawable/", "<img align='middle' src='face_morph_gif.gif' width='100%' />", "text/html", "utf-8", null);
    splashFrame.reload();

    Handler handler = new Handler();

    // run a thread after 3 seconds to start the home screen
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {

            // make sure we close the splash screen so the user won't come back when it presses back key

            finish();
            // start the home screen

            Intent intent = new Intent(activityA.this, activityB.class);
            SplashScreenActivity.this.startActivity(intent);
        }

    }, 5000); 

  }      
}

So here comes my Question 1: How to make this gif play from the beginning when I launch activity A? I noticed that when I open this gif using Safari/Chrome, it will play just once till I manually refresh the browser.

The Custom View method

This is the first way I was looking into but I couldn't get work even once.

Activity A code:

public class activityA extends Activity {

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.activity_a);
    Handler handler = new Handler();

    // run a thread after 3 seconds to start the home screen
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {

            // make sure we close the splash screen so the user won't come back when it presses back key

            finish();
            // start the home screen

            Intent intent = new Intent(activityA.this, activityB.class);
            SplashScreenActivity.this.startActivity(intent);
        }

    }, 5000); 

  }      
}

The Custome View:

public class GifView extends View {

public Movie mMovie;
public long mMovieStart;

public GifView(Context context) {
    super(context);
    setFocusable(true);
    InputStream is = context.getResources().openRawResource(R.drawable.face_morph_gif);
    mMovie = Movie.decodeStream(is);
}

public GifView(Context context, AttributeSet attr) {
    super(context, attr);
    setFocusable(true);
    InputStream is = context.getResources().openRawResource(R.drawable.face_morph_gif);
    mMovie = Movie.decodeStream(is);
}

public GifView(Context context, AttributeSet attr, int defStyle) {
    super(context, attr, defStyle);
    setFocusable(true);
    InputStream is = context.getResources().openRawResource(R.drawable.face_morph_gif);
    mMovie = Movie.decodeStream(is);

}

@Override
protected void onDraw(Canvas canvas) {
   super.onDraw(canvas);
//       canvas.drawColor(Color.TRANSPARENT);
   final long now = SystemClock.uptimeMillis();
   if (mMovieStart == 0) {
      mMovieStart = now;
   }
   if (mMovie != null) {
       int dur = mMovie.duration();
//         Log.d("gif Canvas", "duration: " + mMovie.duration());
       if (dur == 0) {
           dur = 4000;
       }

       int relTime = (int)((now - mMovieStart) % dur);
       mMovie.setTime(relTime);
       mMovie.draw(canvas, 10, 10);
//         Log.d("gif Canvas", mMovie.width() + "x" + mMovie.height());
       invalidate();
   }
}

@Override
  protected void onMeasure(final int widthMeasureSpec,
     final int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight());
}

}

And the xml of layout_a

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/app_background"
tools:ignore="ContentDescription" >

<ImageView
    ... />

<ImageView
    ... />

<ImageView
    ... />

<ImageView
    ... />


<com.xxx.GifView 
    android:id="@+id/splashFrame"
    android:layout_width="95dp" 
    android:layout_height="156dp" 
    android:visibility="visible" />

So here comes my Question 2: Why this custom view can't display the gif? I set break points in onDraw method and I am sure the mMovie can load the gif (mMovie can return correct width and height of the gif)

I know getting gif to work on Android is a quite old topic, AFAIK there are 3 ways:

  1. Movie Class

  2. Custom View

  3. Split GIF/Use Android Animation

I can't use the third way because the gif I am using has 100 frames, and its not cheap.

I've go through all the tutorials and SO questions, but none of them can make this work.

Thank you for reading my questions.


回答1:


In order to play a GIF animation in your android phone, I suggest you to use a WebView (only for Android 2.2 an higher).

A good tutorial can be found here: http://droid-blog.net/2011/10/17/tutorial-how-to-play-animated-gifs-in-android-part-3/

Be careful to clear the cache in order to play the animated GIF each time you open your WebView. Use webView.clearCache(false);

Enjoy.




回答2:


You can convert your GIFs to WebP format.

A few utilities are available online, this is one of them: http://www.zamzar.com/convert/gif-to-webp/



来源:https://stackoverflow.com/questions/12838584/play-gif-in-either-webview-or-custom-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!