来源: [转载]Android显示GIF动画完整示例(一) – 新一 – 博客园
MainActivity如下:
<pre class="java">package cc.testgif; import com.ant.liao.GifView; import com.ant.liao.GifView.GifImageType; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.app.Activity; /** * Demo描述: * 利用第三方控件显示GIF动画 * * 参考资料: * http://blog.csdn.net/leilu2008/article/details/6822517# * http://code.google.com/p/gifview/source/checkout * Thank you very much */ public class MainActivity extends Activity { private GifView mGifView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mGifView = (GifView) findViewById(R.id.gifView); mGifView.setGifImage(R.drawable.gif); mGifView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { System.out.println(" Click "); } }); mGifView.setShowDimension(300, 300); //加载方式 mGifView.setGifImageType(GifImageType.COVER); } }
main.xml如下:
</pre> <pre class="html"><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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:layout_centerHorizontal="true" /> <com.ant.liao.GifView android:id="@+id/gifView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:enabled="false" android:layout_centerInParent="true" /> </RelativeLayout>
MainActivity如下:
</pre> <pre class="java">package cc.testgif2; import android.os.Bundle; import android.app.Activity; /** * Demo描述: * 利用自定义View控件显示GIF动画 * 详细代码参见GIFView * * 参考资料: * http://blog.csdn.net/dawanganban/article/details/9816083 * Thank you very much */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
GIFView如下:
</pre> <pre class="java">package cc.testgif2; import android.content.Context; import android.graphics.Canvas; import android.graphics.Movie; import android.util.AttributeSet; import android.view.View; public class GIFView extends View { private Movie mMovie; private long startTime; private int gifDuration; private boolean isBeginPlay=true; public GIFView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public GIFView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public GIFView(Context context) { super(context); init(); } private void init(){ mMovie = Movie.decodeStream(getResources().openRawResource(R.drawable.gif)); gifDuration= mMovie.duration(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 从开机到现在的毫秒(不包括手机睡眠的时间在内) long currrentTime = android.os.SystemClock.uptimeMillis(); // 第一次播放 if (isBeginPlay) { startTime = currrentTime; isBeginPlay = false; } int playTime = (int) ((currrentTime - startTime) % gifDuration); mMovie.setTime(playTime); mMovie.draw(canvas, 0, 0); // 重绘 invalidate(); } }
main.xml如下:
</pre> <pre class="html"><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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:layout_centerHorizontal="true"/> <cc.testgif2.GIFView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> </RelativeLayout>