Rotate image vertically

I want to flip an Image View animation horizontally (around the x axis). I did the animation clockwise and counterclockwise. Here is the code I used ...

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <rotate 
        android:fromDegrees="0" 
        android:toDegrees="180" 
        android:pivotX="50%" 
        android:pivotY="50%" 
        android:duration="300" 
        android:fillAfter="true" 
        android:fillEnabled="true" />
</set>

I also want to rotate it continuously, and it should stop for a moment after each click.

+4
source share
2 answers

Try using the following XML code with the appropriate java logic, implements your activity using AnimationListener

animation1.xml

<?xml version="1.0" encoding="utf-8"?>
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="250"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="0.0"
    android:toYScale="1.0" />

animation2.xml

<?xml version="1.0" encoding="utf-8"?>
     <scale xmlns:android="http://schemas.android.com/apk/res/android"
      android:duration="250"
      android:fromXScale="0.0"
      android:fromYScale="1.0"
      android:pivotX="50%"
      android:pivotY="50%"
      android:toXScale="1.0"
      android:toYScale="1.0" />

Java logic

private Animation animation1, animation2;
animation1 = AnimationUtils.loadAnimation(this, R.drawable.to_middle);
animation1.setAnimationListener(this);
animation2 = AnimationUtils.loadAnimation(this, R.drawable.from_middle);
animation2.setAnimationListener(this);

     if (flag = true) {
        flipLayout.clearAnimation();
        flipLayout.setAnimation(animation1);
        flipLayout.startAnimation(animation1);
        flag = false;
    } else {
        flipLayout.clearAnimation();
        flipLayout.setAnimation(animation2);
        flipLayout.startAnimation(animation2);
        flag = true;
    }


@Override
public void onAnimationStart(Animation animation) {
    if (animation == animation1) {
        data = true;

    } else {
        if (id == true) {
            tv_calculation.setVisibility(View.GONE);
            id = false;
        } else {
            tv_calculation.setVisibility(View.VISIBLE);
            id = true;
        }
        data = false;
    }
}

@Override
public void onAnimationEnd(Animation animation) {

    if (animation == animation1) {

        flipLayout.clearAnimation();
        flipLayout.setAnimation(animation2);
        flipLayout.startAnimation(animation2);

    } else {

        flipLayout.clearAnimation();
        flipLayout.setAnimation(animation1);
        flipLayout.startAnimation(animation1);
    }

}

FlipLayout is an ImageView.

+6
source

use the animater object, it supports from API level 11 and above.

here is an example

( api 11),

+4

Source: https://habr.com/ru/post/1568954/


All Articles