在任的WebView或自定义视图的Gif播放 [英] Play Gif in either WebView or Custom View

查看:186
本文介绍了在任的WebView或自定义视图的Gif播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在简单地说,我有一个活动A,它适用的setContentView(R.id.layout_a)和一个将持续5秒运行。

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

我试图定义一个自定义视图类和使用Movie类加载GIF,然后安装自定义视图 layout_a ,但它显示只是空白。

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.

我试过的WebView,它可以显示GIF良好,但GIF动画可以仅在第一次我启动这一活动。它只会显示最后一帧时,后来推出的一个活动。

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.

我试过 WebView.reload(),但没有甜味。

下面是试图以应用的WebView,当我的code:

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

  }      
}

所以,这才是我的问题1 :如何从一开始就使这个GIF玩的时候推出活动阿?我注意到,当我打开使用Safari / Chrome浏览GIF,它将发挥只有一次,直到我手动刷新浏览器。

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.

这是我一直在寻找到第一个方式,但我不能找到工作,甚至一次。

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

活动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); 

  }      
}

在Custome查看:

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());
}

}

和layout_a的XML

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

所以,这才是我的问题2 :为什么这个自定义视图无法显示GIF?我设置的onDraw 破发点的方法和我肯定mMovie可以加载GIF(mMovie可以返回GIF的正确宽度和高度)

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)

我知道获得的gif在Android上工作是一个很老的话题,据我所知有3种方式:

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


  1. 电影类

  1. Movie Class

自定义视图

拆分GIF / Android的使用动画

Split GIF/Use Android Animation

我不能使用第三条道路,因为我用的是GIF有100帧,它并不便宜。

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

我已经通过所有的教程和SO的问题,但没有人可以使这项工作。

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

感谢您阅读我的问题。

推荐答案

为了在你的Andr​​oid手机玩GIF动画,我建议你使用的WebView(仅适用于Android 2.2的更高)。

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

有一个很好的教程可以在这里找到:
http://droid-blog.net/2011/10/17/tutorial-how-to-play-animated-gifs-in-android-part-3/

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

小心清除缓存,以便每次你打开你的WebView时间玩GIF动画。使用 webView.clearCache(假);

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

享受。

这篇关于在任的WebView或自定义视图的Gif播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆