android开发之android实现gif图片的动画效果

上一篇 / 下一篇  2013-09-17 08:33:58 / 个人分类:step by step android测试

step:
1、定义自己的一个实现gif展现的view
2、将该view应用到主界面中

具体代码如下:
1、定义自己的一个实现gif展现的view
public class CustomGifView extends View {
    private Movie mMovie;
    private long mMovieStart;
    public CustomGifView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mMovie = Movie.decodeStream(getResources().openRawResource(
                R.drawable.dotty));
    }
    public CustomGifView(Context context) {
        super(context);
        mMovie = Movie.decodeStream(getResources().openRawResource(
                R.drawable.dotty));
    }
    public void onDraw(Canvas canvas) {
        long now = android.os.SystemClock.uptimeMillis();
        if (mMovieStart == 0) { // first time
            mMovieStart = now;
        }
        if (mMovie != null) {
            int dur = mMovie.duration();
            if (dur == 0) {
                dur = 1000;
            }
            int relTime = (int) ((now - mMovieStart) % dur);
            mMovie.setTime(relTime);
            canvas.scale(0.7f,0.7f);
            mMovie.draw(canvas, 0, 0);
            invalidate();
        }
    }
}
2、将该view应用到主界面的layout中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="@drawable/background">
    <TextView android:layout_height="wrap_content"
              android:layout_width="fill_parent"
              android:text="@string/comtitle"
              android:background="#f4f407"
              android:textColor="#268551"
              android:textSize="24sp">
    </TextView>
    <com.period.page.activity.base.CustomGifView
            android:layout_height="150dp"
            android:layout_width="183dp"
            android:id="@+id/gifpicture"
            android:layout_gravity="bottom|right"
            android:layout_marginTop="30dp">
    </com.period.page.activity.base.CustomGifView>
</LinearLayout>

TAG:

 

评分:0

我来说两句

Open Toolbar