Android控制帧动画宽度,GitHub - Gtaiqin/FrameAnimation: 防OOM,低内存帧动画实现,比Android原生帧动画性能更好...

68747470733a2f2f6a69747061636b2e696f2f762f616e73656e3336302f4672616d65416e696d6174696f6e2e737667

68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3638353930643861333733393462326361373239386335646461646432333130

68747470733a2f2f696d672e736869656c64732e696f2f62616467652f617574686f722d616e73656e2d6666363962342e737667

2015年项目接到一个需求,实现一个向导动画,这个动画一共六十张图片,当时使用的是全志A33的开发(512的内存),通过使用Android的动画集实现,效果特别卡顿,然后想到这种方式来实现,效果很流畅.然后写成开一个开源项目供大家参考

对比以下两种方式实现帧动画,使用相同的80张280x280的png图片执行动画,资源占用情况对比:

687474703a2f2f6f6d613638396b38662e626b742e636c6f7564646e2e636f6d2f335f332e706e67

Android动画集实现: 内存占用56M左右

FrameAnimation实现: 内存占用4M左右

两者CUP占用都比较低,可忽略

Sample效果:

687474703a2f2f6f6d613638396b38662e626b742e636c6f7564646e2e636f6d2f335f345f6672616d655f616e696d6174696f6e

一、Android动画集实现帧动画

1 在drawable目录下创建动画集animalist.xml

android:oneshot="false">

android:drawable="@mipmap/c_1"

android:duration="50" />

android:drawable="@mipmap/c_2"

android:duration="50" />

android:drawable="@mipmap/circle_19"

android:duration="50" />

android:drawable="@mipmap/circle_20"

android:duration="50" />

2 在布局文件ImageView中使用该drawable

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.ansen.frameanimation.sample.MainActivity">

android:id="@+id/image"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/animlist" />

3 在代码中调用,启动动画

ImageView image = (ImageView) findViewById(R.id.image);

AnimationDrawable animationDrawable = (AnimationDrawable) image.getDrawable();

animationDrawable.start();

动画启动系统资源占用情况如下:

687474703a2f2f6f6d613638396b38662e626b742e636c6f7564646e2e636f6d2f335f312e706e67

手动触发GC,内存占用几乎没改变

二、FrameAnimation实现帧动画

1 定义需要播放动画的资源文件;在arrays文件中定义资源,或者在代码中定义

@mipmap/c_1

@mipmap/c_2

@mipmap/circle_19

@mipmap/circle_20

获取定义之后的资源数组(代码中可直接定义资源文件的数组,便可忽略上一步):

private int[] getRes() {

TypedArray typedArray = getResources().obtainTypedArray(R.array.c);

int len = typedArray.length();

int[] resId = new int[len];

for (int i = 0; i < len; i++) {

resId[i] = typedArray.getResourceId(i, -1);

}

typedArray.recycle();

return resId;

}

2 在代码中调用,启动动画

ImageView image = (ImageView) findViewById(R.id.image);

FrameAnimation frameAnimation = new FrameAnimation(image, getRes(), 50, true);

frameAnimation.setAnimationListener(new FrameAnimation.AnimationListener() {

@Override

public void onAnimationStart() {

Log.d(TAG, "start");

}

@Override

public void onAnimationEnd() {

Log.d(TAG, "end");

}

@Override

public void onAnimationRepeat() {

Log.d(TAG, "repeat");

}

});

动画启动系统资源占用情况如下:

687474703a2f2f6f6d613638396b38662e626b742e636c6f7564646e2e636f6d2f335f322e706e67

手动触发GC,内存占用有明显变化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值