简体   繁体   English

在左或右android上动态移动动画

[英]Dynamically move animation on left or right android

Hi i am new in android animation i have image of octopus that moves in layout by XML animation 嗨,我是android动画的新手,我有章鱼的图像通过XML动画在布局中移动
and i have two flags images of different countries one flag is right on side and one is left on side now i want to move octopus automatically on one side of flag and the movement every 我有两个不同国家的国旗图像,一个国旗在右边,一个在左边,现在我想在章鱼的一侧自动移动章鱼,并且每移动一次章鱼
time will be different mean some time movement will be on left side and some time movement on right side now how can i do that anyone can give good idea. 时间会有所不同,这意味着某些时间移动将在左侧,而某些时间移动将在右侧,现在我该怎么做,任何人都可以给个好主意。 Below is my code ............. XML code for animation 下面是我的代码.............动画的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>

here is java code to access that file 这是访问该文件的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

}
 }

you need to define object of random Class and generate random number with the help of this object just like below 您需要定义随机类的对象并借助该对象生成随机数,如下所示

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM