Android将动画缩放到指定大小

unftdfkk  于 2023-01-11  发布在  Android
关注(0)|答案(2)|浏览(192)

我正在尝试将View设置为指定大小的动画。
我有一个LinearLayout,它被分成3行3列的网格。所以我有9个LinearLayout作为正方形。我试图将正方形缩放到指定的大小,即外部父视图的大小。我需要我的子视图缩放并填充父视图。
根据我在Android ScaleAnimation文档中读到的内容,我们必须指定一个比例因子,如1.0。
是否有一种方法可以通过指定父视图的边界来设置动画,或者计算比例因子是唯一的方法?
顺便说一句,我使用的是Android 2.1 SDK.

oaxa6hgo

oaxa6hgo1#

自2011年以来,情况发生了很大变化。如果您仍在阅读本文,请将min SDK更新到15(IceCreamSandwich),您可以通过使用ViewPropertyAnimators或使用约束动画(如果包含ConstraintLayout)来完成此操作。

1l5u6lss

1l5u6lss2#

使用网格视图或循环视图,而不是单独的布局。
约束布局是要走的路。

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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:id="@+id/ribbon_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@color/colorWhite"
    app:cardPreventCornerOverlap="false">
    
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="4dp"
        android:background="@drawable/button_ribbon">
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="102dp"
            android:layout_height="59dp"
            android:padding="6dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/ic_antarctica_service_medal_ribbon" />
        
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:text="@string/r1"
            android:textColor="@color/colorPrimary"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constrainedWidth="true"
            app:layout_constraintEnd_toStartOf="@+id/img_addRibbon"
            app:layout_constraintHorizontal_bias="0.103"
            app:layout_constraintStart_toEndOf="@+id/imageView"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="48dp"
            android:layout_marginLeft="48dp"
            android:textSize="15sp"
            app:layout_constraintEnd_toStartOf="@+id/img_addRibbon"
            app:layout_constraintHorizontal_bias="0.09"
            app:layout_constraintLeft_toRightOf="@+id/imageView"
            app:layout_constraintStart_toEndOf="@+id/imageView"
            app:layout_constraintTop_toBottomOf="@+id/textView" />
        
        <ImageView
            android:id="@+id/img_addRibbon"
            android:layout_width="50dp"
            android:layout_height="35dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:background="@drawable/ic_frame00"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.444" />
        
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

接下来是动画逻辑:

itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isRibbonOn) {
                    System.out.println(isRibbonOn);
                    isRibbonOn = true;
                    System.out.println(isRibbonOn);
                    itemView.setBackgroundColor(Color.CYAN);
                    ImageView checkyImage = (ImageView) v.findViewById(R.id.img_addRibbon);
                    checkyImage.setBackgroundResource(R.drawable.ribbon_anim);
                    AnimationDrawable checkyAnimation = (AnimationDrawable) checkyImage.getBackground();
                    checkyAnimation.start();
                } else if (isRibbonOn = true) {
                    System.out.println(isRibbonOn);
                    isRibbonOn = false;
                    System.out.println(isRibbonOn);

                    itemView.setBackgroundColor(Color.WHITE);
                    ImageView checkyImage = (ImageView) v.findViewById(R.id.img_addRibbon);
                    checkyImage.setBackgroundResource(R.drawable.ribbon_anim_return);
                    AnimationDrawable checkyAnimation = (AnimationDrawable) checkyImage.getBackground();
                    checkyAnimation.start();
                }
                {
                    if ((listener != null)) {
                        int position = getAdapterPosition();
                        int ribbon = mAwardList.get(position).getImageResource();
                        if (position != RecyclerView.NO_POSITION) {
                            listener.onAddRibbonClick(ribbon, position, isRibbonOn);
                        }
                        System.out.println("youclickedme" + ribbon);
                    }
                }
            }
        });

相关问题