Android--播放Gif的取巧办法

由于做的项目,要有个动画的等待效果,第一时间想到的就是Gif(懒,省事),但是试了好多据说能播放Gif的控件,也写过,但是放到魅族手机上就是不能播放,所有就想了个招,既然Gif能在浏览器上播放,那android 的 WebView 也能播放,写了个Demo,果然能播放。

1、将gif的文件放到android的资源文件夹里面

2、写个html,将android的gif源放到WebView里面去加载

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </WebView>

</RelativeLayout>

3、代码中使用

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView webView = (WebView) findViewById(R.id.webview);
        webView.loadDataWithBaseURL(null,
                "<HTML><body bgcolor='#f3f3f3'><div align=center><IMG src='file:///android_asset/load_wait_1_gif.gif'/></div></body></html>",
                "text/html", "UTF-8",null);

    }

好了,现在就可以播放了,感觉能够适配任何机型

posted @ 2016-03-03 10:37  灬布衣丶公爵丨  阅读(645)  评论(0编辑  收藏  举报