Android 向屏幕添加多个ImageView,并独立设置它们的动画
我想在一个Android 向屏幕添加多个ImageView,并独立设置它们的动画,android,animation,Android,Animation,我想在一个RelativeLayout中添加一些ImageView,或者不同的大小(始终是正方形),并单独设置它们的动画,使它们在屏幕上“浮动”。我有一个开始,但它不太正确 AnimationSet set = new AnimationSet(true); Animation animation = new AlphaAnimation(0.0f, 1.0f); animation.setDuration(100); set.addAnimation(anim
RelativeLayout
中添加一些ImageView
,或者不同的大小(始终是正方形),并单独设置它们的动画,使它们在屏幕上“浮动”。我有一个开始,但它不太正确
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(100);
set.addAnimation(animation);
animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
-1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f);
animation.setDuration(1500);
animation.setRepeatCount(-1);
animation.setRepeatMode(Animation.REVERSE);
//animation.setInterpolator(new LinearInterpolator());
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(set, 0.25f);
Resources res = getResources();
for (int i = 0;i < 20;i++) {
ImageView iv = new ImageView(this);
iv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Random r = new Random();
int i1 = r.nextInt(150 - 50) + 50;
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, i1, res.getDisplayMetrics());
Log.d("Size", String.valueOf(px));
iv.setMaxWidth((int) px);
iv.setMaxHeight((int) px);
Bitmap image = Bitmap.createBitmap((int) px, (int) px, Bitmap.Config.ARGB_8888);
String colour = String.valueOf(r.nextInt(999999));
String col = ("000000" + colour).substring(colour.length());
Log.d("Colour will be", "#"+col);
image.eraseColor(Color.parseColor("#"+col));
iv.setImageBitmap(image);
//iv.back
((RelativeLayout) findViewById(R.id.for_images)).addView(iv, 0);
}
((RelativeLayout) findViewById(R.id.for_images)).setLayoutAnimation(controller);
controller.start();
AnimationSet set=新的AnimationSet(true);
动画=新AlphaAnimation(0.0f,1.0f);
动画。设置持续时间(100);
set.addAnimation(动画);
动画=新的翻译动画(animation.RELATIVE\u TO\u SELF,
-1.0f,动画。相对于自身,0.0f,
Animation.RELATIVE_TO_SELF,0.0f,
动画。相对于自身,0.0f);
动画。设定持续时间(1500);
动画.setRepeatCount(-1);
设置重复模式(animation.REVERSE);
//setInterpolator(新的LinearInterpolator());
set.addAnimation(动画);
LayoutImationController=新的LayoutImationController(设置,0.25f);
Resources res=getResources();
对于(int i=0;i<20;i++){
ImageView iv=新的ImageView(本);
iv.setLayoutParams(新的ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_内容,ViewGroup.LayoutParams.WRAP_内容));
随机r=新随机();
inti1=r.nextInt(150-50)+50;
float px=TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,i1,res.getDisplayMetrics());
Log.d(“大小”,String.valueOf(px));
iv.setMaxWidth((int)px);
iv.setMaxHeight((int)px);
位图image=Bitmap.createBitmap((int)px,(int)px,Bitmap.Config.ARGB_8888);
字符串颜色=字符串的值(r.nextInt(999999));
字符串col=(“000000”+颜色).substring(color.length());
Log.d(“颜色为“,”#“+col);
image.eraseColor(Color.parseColor(#“+col));
iv.设置图像位图(图像);
//背
((RelativeLayout)findViewById(R.id.for_图像)).addView(iv,0);
}
((RelativeLayout)findViewById(R.id.用于_图像)).SetLayoutImation(控制器);
controller.start();
这就是我目前拥有的,但它们似乎一个接一个地进行动画制作,我如何才能让它们一次全部进行动画制作?这取决于您正在寻找的最终动画是什么,延迟播放,以及您可以获得不同结果的动画。目前,您已经为控制器设置了延迟,并且由于视图大小不同,
TranslateAnimation
在必须键入RELATIVE\u to\u SELF
时会产生并行效果。这可能就是您要找的:
animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_PARENT,
0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
LayoutAnimationController controller = new LayoutAnimationController(
set, 0);
我想要的最终效果是一堆不同大小和颜色的正方形在布局的其余部分后面移动,但这对我来说可能太复杂了,因为这只是我的一个想法,对应用程序来说并不是必需的。你试过我的答案了吗?我相信你会发现改变这些值的正确效果。也许每个视图都有不同的动画。是的,你的代码确实让它们同时移动,我接受它,因为它解决了这篇文章的问题。我想在尝试实现这个想法之前,我必须阅读更多关于动画如何实际工作的内容,因为我需要跟踪对象是否在视野之外等等。