Android缩放动画到指定大小

12

我想要将我的 View 动画到指定的大小。

我有一个 LinearLayout,它被分成了 3 行 3 列的网格。所以我有 9 个 LinearLayout 来表示九个正方形。我正在尝试将正方形缩放到指定的大小,即外部父视图的大小。我需要我的子视图缩放并填充父视图。

从我在 Android 的 ScaleAnimation 文档中读到的内容来看,我们必须指定一个比例因子,如 1.0。

有没有一种方法可以通过指定父视图的边界进行动画处理,还是计算比例因子是唯一的方法?

顺便说一下,我正在使用 Android 2.1 SDK。

2个回答

0

自2011年以来,事情发生了很大的变化。 如果您仍在阅读此内容,请将min SDK更新到15(IceCreamSandwich),您可以通过使用ViewPropertyAnimators或使用约束动画来实现,如果您包括ConstraintLayout。


0

使用网格视图或RecyclerView代替单独的布局。

约束布局是最好的选择。

<?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);
                    }
                }
            }
        });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接