Android中播放Gif动画取巧的办法

前端技术 2023/09/06 Android

由于做的项目,要有个动画的等待效果,第一时间想到的就是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);
}

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

以上所述是小编给大家介绍的Android中播放Gif动画取巧的办法,希望对大家有所帮助!

本文地址:https://www.stayed.cn/item/15690

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。