Android开发: 使用Property Animation可以做到给imageview循环播放我的一些drawable么
是的,使用Property Animation可以实现给ImageView循环播放一系列的drawable。以下是一种可能的实现方法:
-
首先,将你的一系列drawable资源文件放在res/drawable目录下。
-
在你的布局文件中,添加一个ImageView组件:
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/your_starting_drawable"
/>
- 在你的Java代码中,使用Property Animation来实现循环播放drawable资源。
ImageView imageView = findViewById(R.id.imageView);
// 加载需要循环播放的drawable资源
int[] drawableIds = {R.drawable.drawable1, R.drawable.drawable2, R.drawable.drawable3};
Drawable[] drawables = new Drawable[drawableIds.length];
for (int i = 0; i < drawableIds.length; i++) {
drawables[i] = getResources().getDrawable(drawableIds[i]);
}
// 创建Property Animation对象
ObjectAnimator animator = ObjectAnimator.ofInt(imageView, "imageResource", drawableIds);
animator.setDuration(1000); // 设置动画时长
animator.setRepeatCount(ValueAnimator.INFINITE); // 设置重复次数为无限
animator.setRepeatMode(ValueAnimator.RESTART); // 设置重复模式为重新开始
// 开始动画
animator.start();
这样,ImageView将会循环播放drawable1、drawable2和drawable3这三个资源文件。你可以根据自己的需求来修改数组drawableIds中的drawable资源文件
原文地址: http://www.cveoy.top/t/topic/iOfJ 著作权归作者所有。请勿转载和采集!