简体   繁体   English

旋转 TextView 而不旋转任何文本

[英]Rotate TextView without rotating any text

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?我有正在旋转的 TextView,但我只想旋转 TextView 的背景而不是文本,我试图将 TextView 放在 LinearLayout 中,但它仍然与文本一起旋转,那么我该如何解决这个问题?

My xml code is below:我的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主活动.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>

Use one parent view and apply the animation to layout instead of the TextView 使用一个父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 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 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
                }
            });

Try to make your xml like this 试着让你的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>

and apply animation for ivBackPic Imageview it may be work for you. 并为ivBackPic Imageview应用动画它可能适合你。

Use XML below code and use TextView with this id---android:id="@+id/txtRotate" for animating text background. 在代码下面使用XML并使用带有此id的TextView --- android:id =“@ + id / txtRotate”来设置动画文本背景。 and use another textview for text. 并使用另一个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>

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.如果我们使用任何方法将视图旋转 180f,这也会旋转文本。 Simply rotate it to 360f and the same will show the correct text.只需将其旋转到 360f,就会显示正确的文本。 I am using following code:我正在使用以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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