Android加载gif代码,Android加载Gif动画实现代码

android加载gif动画如何实现?相信大家都很好奇,本文就为大家揭晓,内容如下

f9d45749af04a4f87cbd870bc5a7974b.gif

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@+id/gif1"

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_gravity="center_horizontal"

android:enabled="false" />

主界面

package com.example.gifdemo;

import android.app.activity;

import android.os.bundle;

public class mainactivity extends activity {

private gifview gif1;

@override

public void oncreate(bundle savedinstancestate) {

super.oncreate(savedinstancestate);

setcontentview(r.layout.activity_main);

gif1 = (gifview) findviewbyid(r.id.gif1);

// 设置背景gif图片资源

gif1.setmovieresource(r.raw.red);

}

}

自定义view

package com.example.gifdemo;

import android.annotation.suppresslint;

import android.content.context;

import android.content.res.typedarray;

import android.graphics.canvas;

import android.graphics.color;

import android.graphics.movie;

import android.os.build;

import android.util.attributeset;

import android.view.view;

public class gifview extends view {

/**

* 默认为1秒

*/

private static final int default_movie_duration = 1000;

private int mmovieresourceid;

private movie mmovie;

private long mmoviestart;

private int mcurrentanimationtime = 0;

private float mleft;

private float mtop;

private float mscale;

private int mmeasuredmoviewidth;

private int mmeasuredmovieheight;

private boolean mvisible = true;

private volatile boolean mpaused = false;

/**

* 构造函数

*/

public gifview(context context) {

this(context, null);

}

public gifview(context context, attributeset attrs) {

this(context, attrs,0);

}

public gifview(context context, attributeset attrs, int defstyle) {

super(context, attrs, defstyle);

setviewattributes(context, attrs, defstyle);

setbackgroundcolor(color.parsecolor("#ffb6c1"));

}

@suppresslint("newapi")

private void setviewattributes(context context, attributeset attrs,

int defstyle) {

if (build.version.sdk_int >= build.version_codes.honeycomb) {

setlayertype(view.layer_type_software, null);

}

// 从描述文件中读出gif的值,创建出movie实例

final typedarray array = context.obtainstyledattributes(attrs,

r.styleable.gifview);

mmovieresourceid = array.getresourceid(r.styleable.gifview_gif, -1);

mpaused = array.getboolean(r.styleable.gifview_paused, false);

array.recycle();

if (mmovieresourceid != -1) {

mmovie = movie.decodestream(getresources().openrawresource(

mmovieresourceid));

}

}

/**

* 设置gif图资源

*/

public void setmovieresource(int movieresid) {

this.mmovieresourceid = movieresid;

mmovie = movie.decodestream(getresources().openrawresource(

mmovieresourceid));

requestlayout();

}

@override

protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {

if (mmovie != null) {

//gif动画的宽度、高度

int moviewidth = mmovie.width();

int movieheight = mmovie.height();

//控件的宽度

int maximumwidth = measurespec.getsize(widthmeasurespec);

//gif图片宽/控件宽

float scalew = (float) moviewidth / (float) maximumwidth;

mscale = 1f / scalew;

mmeasuredmoviewidth = maximumwidth;

mmeasuredmovieheight = (int) (movieheight * mscale);

setmeasureddimension(mmeasuredmoviewidth, mmeasuredmovieheight);

} else {

setmeasureddimension(getsuggestedminimumwidth(),

getsuggestedminimumheight());

}

}

// @override

// protected void onlayout(boolean changed, int l, int t, int r, int b) {

// super.onlayout(changed, l, t, r, b);

// mleft = (getwidth() - mmeasuredmoviewidth) / 2f;

// mtop = (getheight() - mmeasuredmovieheight) / 2f;

// mvisible = getvisibility() == view.visible;

// }

@override

protected void ondraw(canvas canvas) {

if (mmovie != null) {

if (!mpaused) {

updateanimationtime();

drawmovieframe(canvas);

invalidateview();

} else {

drawmovieframe(canvas);

}

}

}

private void updateanimationtime() {

long now = android.os.systemclock.uptimemillis();

// 如果第一帧,记录起始时间

if (mmoviestart == 0) {

mmoviestart = now;

}

// 取出动画的时长

int dur = mmovie.duration();

if (dur == 0) {

dur = default_movie_duration;

}

// 算出需要显示第几帧

mcurrentanimationtime = (int) ((now - mmoviestart) % dur);

}

private void drawmovieframe(canvas canvas) {

// 设置要显示的帧,绘制即可

mmovie.settime(mcurrentanimationtime);

canvas.save(canvas.matrix_save_flag);

canvas.scale(mscale, mscale);

mmovie.draw(canvas, mleft / mscale, mtop / mscale);

canvas.restore();

}

@suppresslint("newapi")

private void invalidateview() {

if (mvisible) {

if (build.version.sdk_int >= build.version_codes.jelly_bean) {

postinvalidateonanimation();

} else {

invalidate();

}

}

}

// --------------------以下方法未调用------------------------------------/

public void setmovie(movie movie) {

this.mmovie = movie;

requestlayout();

}

public movie getmovie() {

return mmovie;

}

public void setmovietime(int time) {

mcurrentanimationtime = time;

invalidate();

}

public void setpaused(boolean paused) {

this.mpaused = paused;

if (!paused) {

mmoviestart = android.os.systemclock.uptimemillis()

- mcurrentanimationtime;

}

invalidate();

}

public boolean ispaused() {

return this.mpaused;

}

@suppresslint("newapi")

@override

public void onscreenstatechanged(int screenstate) {

super.onscreenstatechanged(screenstate);

mvisible = screenstate == screen_state_on;

invalidateview();

}

@suppresslint("newapi")

@override

protected void onvisibilitychanged(view changedview, int visibility) {

super.onvisibilitychanged(changedview, visibility);

mvisible = visibility == view.visible;

invalidateview();

}

@override

protected void onwindowvisibilitychanged(int visibility) {

super.onwindowvisibilitychanged(visibility);

mvisible = visibility == view.visible;

invalidateview();

}

// --------------------------------------------------------/

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值