[英]Dynamically move animation on left or right android
嗨,我是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
}
}
您需要定義隨機類的對象並借助該對象生成隨機數,如下所示
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.