android-显示多个图像动画
作者:互联网
我想显示背景图像和像“降雪”场景一样向下移动的多个图像,所以我该怎么做?背景图像不应该移动,只有上面的小图像应该掉下来.我该怎么做?
更新->
我已经在屏幕上显示了图像,但是它们都是同时出现的,但是我想显示在不同时间出现的图像,我该怎么做?嘿,请提出一些建议.这是正确的方法吗?方式 ….
这是我的代码:
public class AnimationActivity extends Activity implements AnimationListener
{
/** Called when the activity is first created. */
LinearLayout layout;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Animation movement;
layout = (LinearLayout) findViewById(R.id.linearLayout_ani);
movement = AnimationUtils.loadAnimation(this,R.layout.abc);
movement.reset();
movement.setFillAfter(true);
movement.setAnimationListener(this);
movement.setRepeatCount(1);
movement.setRepeatMode(7);
layout.startAnimation(movement);
}
@Override
public void onAnimationEnd(Animation movement)
{
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation arg0)
{
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation arg0)
{
}
}
这是动画布局文件:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0%"
android:toXDelta="50%"
android:fromYDelta="0%"
android:toYDelta="95%"
android:duration="10000"
android:zAdjustment="normal" />
主.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/wall">
<LinearLayout android:id="@+id/linearLayout_ani"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<ImageView android:id="@+id/snowimg1"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:visibility="visible"
android:background="@drawable/snow1"
></ImageView>
<ImageView android:id="@+id/snowimg2"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:visibility="visible"
android:background="@drawable/snow1"
></ImageView>
<ImageView android:id="@+id/snowimg3"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:visibility="visible"
android:background="@drawable/snow1"
></ImageView>
<ImageView android:id="@+id/snowimg4"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:visibility="visible"
android:background="@drawable/snow1"
></ImageView>
<ImageView android:id="@+id/snowimg5"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:visibility="visible"
android:background="@drawable/snow1"
></ImageView>
<ImageView android:id="@+id/snowimg6"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:visibility="visible"
android:background="@drawable/snow1"
>
</ImageView>
</LinearLayout>
</LinearLayout>
解决方法:
您可以为所有图像添加动画师,如下所示:
private final int REPEAT_COUNT = 1;
private final int REPEAT_MODE = 7;
private void startAnimations() {
// Using the ImageView not the layout.
ImageView snowImg1 = (ImageView) findViewById(R.id.snowimg1);
ImageView snowImg2 = (ImageView) findViewById(R.id.snowimg2);
ImageView snowImg3 = (ImageView) findViewById(R.id.snowimg3);
ImageView snowImg4 = (ImageView) findViewById(R.id.snowimg4);
ImageView snowImg5 = (ImageView) findViewById(R.id.snowimg5);
ImageView snowImg6 = (ImageView) findViewById(R.id.snowimg6);
ImageView snowArray[] = {snowImg1, snowImg2, snowImg3, snowImg4, snowImg5, snowImg6};
// If it is not the same movement, you will need to create different layouts
Animation snowMov1 = AnimationUtils.loadAnimation(this, R.layout.snowimg1);
Animation snowMov2 = AnimationUtils.loadAnimation(this, R.layout.snowimg2);
Animation snowMov3 = AnimationUtils.loadAnimation(this, R.layout.snowimg3);
Animation snowMov4 = AnimationUtils.loadAnimation(this, R.layout.snowimg4);
Animation snowMov5 = AnimationUtils.loadAnimation(this, R.layout.snowimg5);
Animation snowMov6 = AnimationUtils.loadAnimation(this, R.layout.snowimg6);
Animation movArray[] = {snowMov1, snowMov2, snowMov3, snowMov4, snowMov5, snowMov6};
// Start the movement animation.
startMovement(snowArray, movArray);
}
private void startMovement(ImageView imgArray[], Animation movArray[]) {
// Same length so there is no problem...
int length = imgArray.length;
for(int i = 0; i < length; i++) {
movArray[i].reset();
movArray[i].setFillAfter(true);
movArray[i].setAnimationListener(this);
movArray[i].setRepeatCount(REPEAT_COUNT);
movArray[i].setRepeatMode(REPEAT_MODE);
// Start the animation
imgArray[i].startAnimation(movArray[i]);
}
}
您将需要在动画文件中进行随机移动,这未经测试,因此我不确定是否可以运行…我希望它至少对我有帮助.
编辑:
别急,如果我的方法行不通,帮助会很快出现!,您只需要等一下…
标签:android-2-2-froyo,android 来源: https://codeday.me/bug/20191208/2089176.html