旋转 TextView DrawableRight OnClick

Posted

技术标签:

【中文标题】旋转 TextView DrawableRight OnClick【英文标题】:Rotate TextView DrawableRight OnClick 【发布时间】:2020-10-22 13:27:34 【问题描述】:

我有一个 TextView,它的右侧设置了一个箭头。我希望箭头在单击时旋转,并在再次单击以显示和隐藏文本时将其返回到先前的位置。我已经能够显示和隐藏我无法让旋转动画工作的信息。

XML

<TextView
   android:id="@+id/expandable_first_next_last_air_date"
   android:layout_
   android:layout_
   android:layout_gravity="center"
   android:layout_marginBottom="8dp"                    
   android:drawableEnd="@drawable/ic_keyboard_arrow_down_white_24dp"
   android:drawableTint="?attr/textColor"
   android:text="First, Next &amp; Last Air Date"
   android:textColor="?attr/textColor"
   android:textSize="26sp" />

TextView OnClick 监听器

        tvExpandableFirstNextLastAirDate = findViewById(R.id.expandable_first_next_last_air_date);

        tvExpandableFirstNextLastAirDate.setOnClickListener(v -> 
            if (isTextViewClicked)
                tvFirstNextLastAirDate.setMaxLines(0);
                isTextViewClicked = false;
            
            else
                tvFirstNextLastAirDate.setMaxLines(Integer.MAX_VALUE);
                isTextViewClicked = true;
            
        );

【问题讨论】:

【参考方案1】:

java:

 final TextView textView = findViewById(R.id.text_view);
        final ImageView imageView = findViewById(R.id.image_view);
    
    final TextView textView2 = findViewById(R.id.text_view2);
    final ImageView imageView2 = findViewById(R.id.image_view2);
    final AnimationSet animSetUp = new AnimationSet(true);
    animSetUp.setInterpolator(new DecelerateInterpolator());
    animSetUp.setFillAfter(true);
    animSetUp.setFillEnabled(true);

    final AnimationSet animSetDown = new AnimationSet(true);
    animSetDown.setInterpolator(new DecelerateInterpolator());
    animSetDown.setFillAfter(true);
    animSetDown.setFillEnabled(true);

    final RotateAnimation animRotateUp = new RotateAnimation(0.0f, 180.0f,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f);

    animRotateUp.setDuration(1500);
    animRotateUp.setFillAfter(true);
    animSetUp.addAnimation(animRotateUp);

    final RotateAnimation animRotateDown = new RotateAnimation(180.0f, 0.0f,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f);

    animRotateDown.setDuration(1500);
    animRotateDown.setFillAfter(true);
    animSetDown.addAnimation(animRotateDown);

    final boolean[] isTextViewClickedForTextView1 = true;
    final boolean[] isTextViewClickedForTextView2 = true;
    textView.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            if (isTextViewClickedForTextView1[0]) 
                textView.setMaxLines(0);
                isTextViewClickedForTextView1[0] = false;
                imageView.startAnimation(animSetUp);
             else 
                imageView.startAnimation(animSetDown);
                textView.setMaxLines(Integer.MAX_VALUE);
                isTextViewClickedForTextView1[0] = true;

            
        
    );    
    
    textView2.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            if (isTextViewClickedForTextView2[0]) 
                textView2.setMaxLines(0);
                isTextViewClickedForTextView2[0] = false;
                imageView2.startAnimation(animSetUp);
             else 
                imageView2.startAnimation(animSetDown);
                textView2.setMaxLines(Integer.MAX_VALUE);
                isTextViewClickedForTextView2[0] = true;
            
        
    );

XML:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text_view"
        android:layout_
        android:layout_
        android:layout_margin="10dp"
        android:padding="10dp"
        android:text="text view 1"
        android:textAlignment="viewStart"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/image_view"
        android:layout_
        android:layout_
        android:padding="5dp"
        android:src="@drawable/ic_baseline_arrow_drop_down_24"
        app:layout_constraintBottom_toBottomOf="@+id/text_view"
        app:layout_constraintEnd_toEndOf="@+id/text_view"
        app:layout_constraintTop_toTopOf="@+id/text_view" />

    <TextView
        android:id="@+id/text_view2"
        android:layout_
        android:layout_
        android:layout_margin="10dp"
        android:padding="10dp"
        android:text="text view 2"
        android:textAlignment="viewStart"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text_view"/>

    <ImageView
        android:id="@+id/image_view2"
        android:layout_
        android:layout_
        android:padding="5dp"
        android:src="@drawable/ic_baseline_arrow_drop_down_24"
        app:layout_constraintBottom_toBottomOf="@+id/text_view2"
        app:layout_constraintEnd_toEndOf="@+id/text_view2"
        app:layout_constraintTop_toTopOf="@+id/text_view2" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

res/anim/ic_baseline_arrow_drop_down_24.xml:

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

【讨论】:

谢谢,我会试试这个。如果我有多个 TextView,我可以使用相同的布尔值还是必须为每个 TextView 制作一个? 我是否还必须为多个文本视图复制图像视图? 我编辑了我的答案。但这根本不是标准的。请问您到底在找什么? 我想知道当我显示和隐藏 TextViews 时是否可以减少重复代码,因为我现在正在为 TextViews 使用 switch case,但编辑看起来像我想象的那样。谢谢

以上是关于旋转 TextView DrawableRight OnClick的主要内容,如果未能解决你的问题,请参考以下文章

是否可以使用 android:DrawableRight 在 Buttons 和 TextViews 中使用 VectorDrawable?

TextView简单学习

TextView简单学习

Android:Textview 通过代码设置 Drawable

安卓在代码中设置TextView的drawableLeftdrawableRightdrawableTopdrawableBottom

Android 文本,软键盘使用指南