今天工作中,UI给了一个gif图,要求做成网络请求时的加载动画。之前自己记得android中并不能够实线Gif图片,今天去网上搜了一下,发现Android中可以实现Gif图片的显示。下文是通过借助开源‘Gifview‘实现Gif的显示。
效果图:
GifView类,是一位N人写的一个加载Gif动画的类,并且已经将该类做成一个jar类库,供开发者直接加载使用。这个类的使用非常方便,跟普通的UI控件ImageView、Button、TextView等的使用方法都是一样的。
设置GID动画显示的方式:(三种)
先加载后显示:GifImageType.WAIT_FINISH
边加载边显示:GifImageType.SYNC_DECODER
只显示第一帧再显示:GifImageType.COVER
布局文件:
<?xml version="1.0" encoding="utf-8"?> <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="#eeeeee" tools:context="iclassmate.net.onlyyou.Main2Activity"> <com.ant.liao.GifView android:id="@+id/gifView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <TextView android:id="@+id/tv_loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_below="@+id/gifView" android:text="Loading..." android:textSize="14sp" /> </RelativeLayout>主要代码(非常简单):
public class Main2Activity extends AppCompatActivity { private GifView mLoadingImg; private TextView mLoadingText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); initView(); } private void initView() { mLoadingImg = (GifView) findViewById(R.id.gifView); mLoadingText=(TextView) findViewById(R.id.tv_loading); mLoadingImg.setGifImageType(GifView.GifImageType.WAIT_FINISH); mLoadingImg.setGifImage(R.drawable.aa); mLoadingText.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/test.ttf")); } }