【问题标题】:Android scale animation to specified sizeAndroid将动画缩放到指定大小
【发布时间】:2011-10-19 16:28:10
【问题描述】:

我正在尝试将我的View 设置为指定大小。

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

根据我在 Android ScaleAnimation 文档中读到的内容,我们必须指定一个像 1.0 这样的比例因子。

有没有办法通过指定父视图的边界来制作动画,或者计算比例因子是唯一的方法?

顺便说一句,我使用的是 Android 2.1 SDK。

【问题讨论】:

    标签: android android-layout android-animation


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      使用网格视图或回收视图而不是单独的布局。

      约束布局是要走的路。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多