1.Movie类简介
2.代码实现
import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Movie; import android.graphics.Paint; import android.os.Bundle; import android.util.AttributeSet; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new MyGifView(this)); } class MyGifView extends View{ //movie管理gif图片里的多个帧 private Movie movie; //表示开始播放gif图片的绝对时间 private long movieStart; public MyGifView(Context context, AttributeSet attrs) { super(context, attrs); } public MyGifView(Context context) { super(context); movie = Movie.decodeStream(getResources().openRawResource(R.drawable.maidi3)); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); long currentTime = android.os.SystemClock.uptimeMillis(); //第一次播放 if(movieStart == 0){ movieStart = currentTime; } //循环播放 if(movie != null){ int duration = movie.duration(); int relTime = (int)((currentTime - movieStart)%duration); movie.setTime(relTime); movie.draw(canvas,10,20); // Paint paint = new Paint(); // paint.setColor(getResources().getColor(android.R.color.holo_green_light)); // movie.draw(canvas,10,20, paint); invalidate(); } } } }
时间: 2024-11-13 05:24:02