标签:des android style io ar os 使用 sp 文件
android播放gif 我研究过3种
第一 :GifView支持android播放gif,效果是 先加载第一帧,然后慢慢加载完其他的针,这样效果视觉很不好,是从模糊到清晰的过程;
第二:是流行的把gif图片通过工具分拆成n帧,然后使用逐帧动画播放,我感觉很麻烦的样子;
第三 :使用Movie提供的Movie.decodeStream()方法解析gif,然后通过文件流的方式播放,效果特别好 ,和原图片没差
研究之后可以通过canvas实现对gif的拉伸,适配任何屏幕。
核心代码 自己加个layout,Activity测试
public class GifView extends View {
private Movie mMovie;
private long mMovieStart;
private int mWidth, mHeight;
private int mViewWidht, mViewHeight;
private OnPlayListener onPlayListener;
public GifView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public GifView(Context context) {
super(context);
mMovie = Movie.decodeStream(getResources().openRawResource(
R.raw.gif_anim));
}
public GifView(Context context, AttributeSet attrs) {
super(context, attrs);
//使用Movie解析gif图片
mMovie = Movie.decodeStream(getResources().openRawResource( R.raw.gif_anim));
//获得屏幕宽度,高度
mWidth = BaseApplication.getInstance().screenWidth;
mHeight = BaseApplication.getInstance().screenHeight;
//gif图片宽度,高度
mViewHeight = mMovie.height();
mViewWidht = mMovie.width();
}
public OnPlayListener getOnPlayListener() {
return onPlayListener;
}
public void setOnPlayListener(OnPlayListener onPlayListener) {
this.onPlayListener = onPlayListener;
}
boolean isDraw = true;
public void onDraw(Canvas canvas) {
long now = android.os.SystemClock.uptimeMillis();
if (isDraw) {
if (mMovieStart == 0) { // first time
mMovieStart = now;
}
if (mMovie != null) {
int dur = mMovie.duration();
if (dur == 0) {
dur = 5000;
}
//计算gif播放时间,gif播放完成,关闭界面
if (now - mMovieStart >= dur) {
isDraw = false;
if (onPlayListener != null) {
onPlayListener.onFinished();
}
}
int relTime = (int) ((now - mMovieStart) % dur);
mMovie.setTime(relTime);
//根据屏幕大小计算缩放比例
float saclex = (float) mWidth / (float) mViewWidht;
float sacley = (float) mHeight / (float) mViewHeight;
float sameRate = saclex > sacley ? saclex : sacley;
canvas.scale(sameRate, sameRate);
mMovie.draw(canvas, 0, 0);
invalidate();
}
}
}
//gif关闭接口
public static interface OnPlayListener {
public void onFinished();
}
}
标签:des android style io ar os 使用 sp 文件
原文地址:http://www.cnblogs.com/dongweiq/p/4096476.html