AssetManager读取assets下多张图片资源输出到ImageView动画
这里面有几个要点和环节需要注意:
1,AssetManager读取事先放置到assets目录下的原始图片资源文件,组装成Android的Bitmap数组。
文件结构如图:
2,把1中读取到的Bitmap数组每隔一个较小时间内(如25ms)不间断循环设置到ImageView里面,从而在视觉上形成一种动画效果。
代码:
package zhangphil.test;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
public class AnimationActivity extends AppCompatActivity {
private boolean mStartLoadingAnimation = false;
private ImageView mImageView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animation_activity);
mImageView = findViewById(R.id.image);
mStartLoadingAnimation = true;
loadingAnimation();
}
private void loadingAnimation() {
new Thread(new Runnable() {
@Override
public void run() {
Bitmap[] bitmaps = getBimaps();
if (bitmaps == null || bitmaps.length == 0) {
return;
}
int i = 0;
while (mStartLoadingAnimation) {
mImageView.setImageBitmap(bitmaps[i++ % bitmaps.length]);
try {
TimeUnit.MILLISECONDS.sleep(25);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
private Bitmap[] getBimaps() {
final String parentPath = "loading";
Bitmap[] bitmaps = null;
AssetManager am = getAssets();
try {
String[] files = am.list(parentPath);
bitmaps = new Bitmap[files.length];
for (int i = 0; i < files.length; i++) {
InputStream is = am.open(parentPath + "/" + files[i]);
bitmaps[i] = BitmapFactory.decodeStream(is);
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return bitmaps;
}
}
原文发布时间为:2018-08-19
本文来自云栖社区合作伙伴“Android开发中文站”,了解相关信息可以关注“Android开发中文站”。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
thymeleaf 传递数据到js变量
thymeleaf 传递数据到js变量 如何把控制器传来的model中的值传递给js变量呢? 需要以下两个: <script th:inline="javascript"> var message = [[${message}]] 1.controller @RequestMapping(value = "message", method = RequestMethod.GET) public String messages(Model model) { model.addAttribute("message", "hello"); return "index"; } 2.not work var m = ${message}; // not working alert(m); 3.ok <script th:inline="javascript"> /*<![CDATA[*/ var message = [[${message}]]; console.log(message); /*]]>*/ </script>
- 下一篇
Python是一门动态语言
动态语言的定义 动态编程语言 是 高级程序设计语言 的一个类别,在计算机科学领域已被广泛应用。它是一类 在运行时可以改变其结构的语言 :例如新的函数、对象、甚至代码可以被引进,已有的函数可以被删除或是其他结构上的变化。动态语言目前非常具有活力。例如JavaScript便是一个动态语言,除此之外如 PHP 、 Ruby 、 Python 等也都属于动态语言,而 C 、 C++ 等语言则不属于动态语言。 给对象绑定(添加)属性 class Car(object): def __init__(self, Carname = None, CarMoney = None): self.Carname= Carname self.CarMoney= CarMoney C = Car("法拉利", "3000000") 在这里,我们定义了1个类Car,在这个类里,定义了两个初始属性Carname和CarMoney,但是人还有性别啊!如果这个类不是你写的是不是你会尝试访问性别这个属性呢? >>> C.user = "技术大爆炸">>> P.user'技术大爆炸'&g...
相关文章
文章评论
共有0条评论来说两句吧...