Rotate TextView without rotating any text

1.1k Views Asked by At

I have TextView which is rotating, but I want to rotate only the background of TextView not a text, I tried to put TextView inside the LinearLayout but still it is rotating with text, so how can I solve this?

My xml code is below:

<TextView
        android:id="@+id/txtRotate"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignBottom="@+id/gif1"
        android:layout_centerHorizontal="true"
        android:background="@drawable/round_image"
        android:text="Hello"
        android:textColor="@android:color/holo_red_dark"
        android:gravity="center" />

MainActivity.java

animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.rotate);
        animFadein.setFillAfter(true);
        animFadein.setAnimationListener(this);

txtVibrate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtRotate.startAnimation(animFadein);
            }
        });

rotate.xml

<?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <rotate android:fromDegrees="0"
            android:toDegrees="360"
            android:pivotX="50%"
            android:pivotY="50%"
            android:duration="6000"
            android:repeatMode="restart"
            android:repeatCount="0"
            android:interpolator="@android:anim/cycle_interpolator"/>
    </set>
4

There are 4 best solutions below

1
Maheshwar Ligade On

Use one parent view and apply the animation to layout instead of the TextView

 <FrameLayout 
            android:id="@+id/fmlayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >
            <TextView
        android:id="@+id/txtRotate"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignBottom="@+id/gif1"
        android:layout_centerHorizontal="true"
        android:background="@drawable/round_image"
        android:text="Hello"
        android:textColor="@android:color/holo_red_dark"
        android:gravity="center" />
        </FrameLayout>

rotate.xml

<?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <rotate android:fromDegrees="0"
            android:toDegrees="360"
            android:pivotX="50%"
            android:pivotY="50%"
            android:duration="6000"
            android:repeatMode="restart"
            android:repeatCount="0"
            android:interpolator="@android:anim/cycle_interpolator"/>
    </set>

MainActivity.java

 animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.rotate);
            animFadein.setFillAfter(true);
            animFadein.setAnimationListener(this);

    txtVibrate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
// here change the code and apply animation on framelayout
                }
            });
0
Abhishek Patel On

Try to make your xml like this

<RelativeLayout
    android:id="@+id/rlImages"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   >

    <ImageView
        android:id="@+id/ivBackPic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@drawable/round_image" />

    <TextView
        android:id="@+id/txtRotate"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignBottom="@+id/ivBackPic"
        android:layout_alignLeft="@+id/ivBackPic"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/ivBackPic"
        android:layout_alignTop="@+id/ivBackPic"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="Hello"
        android:textColor="@android:color/holo_red_dark" />

</RelativeLayout>

and apply animation for ivBackPic Imageview it may be work for you.

0
Rock Star On

Use XML below code and use TextView with this id---android:id="@+id/txtRotate" for animating text background. and use another textview for text.

<RelativeLayout
    android:layout_width="80dp"
    android:layout_height="80dp">

    <TextView
        android:id="@+id/txtRotate"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_blue_dark" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="Hello"
        android:textColor="@android:color/holo_red_dark" />
</RelativeLayout>
0
Ashirwad Kumar On

I had looking for the solution on same question and finally it is resolved. If we will rotate the view by 180f by using any method this will also rotate the text. Simply rotate it to 360f and the same will show the correct text. I am using following code:

ObjectAnimator animation = ObjectAnimator.ofFloat(textView, "rotationY", 0f, 360f);
            animation.setDuration(1000);
            animation.setInterpolator(new AccelerateDecelerateInterpolator());
            animation.start();