1

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>
  • You can make your textview background transparent and create view behind textview which will be holding your textView's background and then animate this view. – MyWay May 27 '16 at 06:53
  • Thanks but it is not working because view is holding TextView so View is rotate with TextView!! Any another idea about it? –  May 27 '16 at 06:58
  • It shouldnt be holding textView, look at @Abhishek Patel answer – MyWay May 27 '16 at 07:01
  • use `android.graphics.drawable.RotateDrawable` to rotate the background – pskink May 27 '16 at 09:18

4 Answers4

1

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>
Rock Star
  • 95
  • 9
0

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
                }
            });
Maheshwar Ligade
  • 6,709
  • 4
  • 42
  • 59
0

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.

Abhishek Patel
  • 4,280
  • 1
  • 24
  • 38
0

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();