嗨,我是Android动画的新手,我有章鱼的形象,在布局中通过XML动画来移动 我有两个不同国家的国旗图像,一面旗帜就在旁边,一面留在一边,现在我想在旗帜的一侧自动移动章鱼,每次移动 时间会有所不同意味着一些时间运动将在左侧,一些时间运动 现在右边我怎么能这样做,任何人都可以给出好主意。 下面是我的代码.............动画的XML代码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator" >
<!-- Use startOffset to give delay between animations -->
<!-- Move -->
<translate
android:duration="2500"
android:fillAfter="true"
android:fromXDelta="0%p"
android:startOffset="300"
android:toXDelta="25%p" />
<translate
android:duration="2500"
android:fillAfter="true"
android:fromYDelta="0%p"
android:startOffset="2800"
android:toYDelta="50%p" />
<translate
android:duration="2500"
android:fillAfter="true"
android:fromXDelta="0%p"
android:startOffset="5300"
android:toXDelta="-25%p" />
<translate
android:duration="2500"
android:fillAfter="true"
android:fromYDelta="0%p"
android:startOffset="7800"
android:toYDelta="-50%p" />
<!-- Rotate 360 degrees -->
<rotate
android:duration="2000"
android:fromDegrees="0"
android:interpolator="@android:anim/cycle_interpolator"
android:pivotX="25%"
android:pivotY="50%"
android:startOffset="10300"
android:repeatMode="restart"
android:toDegrees="180" />
</set>
这是访问该文件的java代码
Animation fadein_anim;
ImageView animimagej, imageView;// imageView Button and animimagej is octopus image
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
animimagej = (ImageView) findViewById(R.id.animimage);
imageView = (ImageView) findViewById(R.id.iv);
fadein_anim = AnimationUtils.loadAnimation(getApplicationContext(),
R.animator.sequence);
fadein_anim.setAnimationListener(this);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
animimagej.setVisibility(View.VISIBLE);
animimagej.startAnimation(fadein_anim);
}
});
}
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
您需要定义随机类的对象并在此对象的帮助下生成随机数,如下所示
Random a = new Random();
int b = a.nextInt(5 - 1) + 1;<-------------- generating random number
switch (b) {
case 1:
start.youranimationhere()
break;
case 2:
start.youranimation2here()
break;
break;
case 3:
//do your job for all case that you declare variable b above