Android通过Movie展示Gif格式图片
短信预约 -IT技能 免费直播动态提醒
本文实例为大家分享Android通过Movie展示Gif格式图片的相关代码,供大家参考,具体内容如下
public class CommonGifView extends View {
private Resources mResources;
private Movie mMovie;
private long startTime = 0;
private float widthRatio;
private float heightRatio;
public CommonGifView(Context context) {
this(context, null);
}
public CommonGifView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CommonGifView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mResources = context.getResources();
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.custom_gif_view);
int class="lazy" data-src_id = ta.getResourceId(R.styleable.custom_gif_view_gif_class="lazy" data-src, -1);
setGifViewBg(class="lazy" data-src_id);
ta.recycle();
}
private void setGifViewBg(int class="lazy" data-src_id) {
if (class="lazy" data-src_id == -1) { return; }
// 获取对应资源文件的输入流
InputStream is = mResources.openRawResource(class="lazy" data-src_id);
mMovie = Movie.decodeStream(is);// 解码输入流为Movie对象
requestLayout();
}
public void setGifStream(InputStream is) {
mMovie = Movie.decodeStream(is);
requestLayout();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
long now = SystemClock.uptimeMillis();
if (startTime == 0) { // 如果第一帧,记录起始时间
startTime = now;
}
if (mMovie != null) {// 如果返回值不等于null,就说明这是一个GIF图片
int duration = mMovie.duration();// 取出动画的时长
if (duration == 0) {
duration = 1000;
}
int currentTime = (int) ((now - startTime) % duration);// 算出需要显示第几帧
mMovie.setTime(currentTime);
// mMovie.draw(canvas, getWidth() - mMovie.width(), getHeight() - mMovie.height());
float scale = Math.min(widthRatio, heightRatio);
canvas.scale(scale, scale);
mMovie.draw(canvas, 0, 0);
invalidate();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mMovie != null) {// 如果返回值不等于null,就说明这是一个GIF图片
int w = mMovie.width();//宽度
int h = mMovie.height();//高度
if (w <= 0) {
w = 1;
}
if (h <= 0) {
h = 1;
}
int left = getPaddingLeft();
int right = getPaddingRight();
int top = getPaddingTop();
int bottom = getPaddingBottom();
int widthSize, heightSize;
w += left + right;
h += top + bottom;
w = Math.max(w, getSuggestedMinimumWidth());
h = Math.max(h, getSuggestedMinimumHeight());
widthSize = resolveSizeAndState(w, widthMeasureSpec, 0);//根据你提供的大小和MeasureSpec,返回你想要的大小值
heightSize = resolveSizeAndState(h, heightMeasureSpec, 0);
widthRatio = (float) widthSize / w;
heightRatio = (float) heightSize / h;
setMeasuredDimension(widthSize, heightSize);
}
else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
自定义属性res/values/attrs.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="custom_gif_view">
<attr name="gif_class="lazy" data-src" format="reference"></attr>
</declare-styleable>
</resources>
在Activity中使用:
public class MainActivity extends Activity {
private CommonGifView view;
private InputStream is;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (CommonGifView) findViewById(R.id.gif_test);
try {
is = getAssets().open("test01.gif");
view.setGifStream(is);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
您可能感兴趣的文章:Android实现轮播图片展示效果Android编程实现下载图片及在手机中展示的方法Android学习教程之九宫格图片展示(13)Android开发之使用GridView展示图片的方法Android实现3D层叠式卡片图片展示
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341