旋转TextView而不旋转任何文本

时间:2016-05-27 06:49:42

标签: android textview rotateanimation

我有旋转的TextView,但是我想只旋转TextView的背景而不是文本,我试图将TextView放在LinearLayout中但仍然用文本旋转,所以我该如何解决这个问题?

我的xml代码如下:

<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>

3 个答案:

答案 0 :(得分:0)

使用一个父view并将动画应用于布局而不是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
                }
            });

答案 1 :(得分:0)

尝试制作像这样的xml

<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>

并为ivBackPic Imageview应用动画,它可能适合您。

答案 2 :(得分:0)

在代码下面使用XML并使用带有此id的TextView --- android:id =&#34; @ + id / txtRotate&#34;用于动画文本背景。 并使用另一个textview文本。

<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>
相关问题