开发者

Android scale animation to specified size

I'm trying to animate my View to a specified size.

I have a LinearLayout that is divided into a grid of 3 rows and 3 columns. 开发者_JS百科So I have 9 LinearLayouts as squares. I am trying to scale the square to a specified size i.e. the size of the outer parent view. I need my subview to scale and fill the parent view.

From what I read in the Android ScaleAnimation docs, we have to specify a scale factor like 1.0.

Is there a way to animate by specifying the bounds of the parent view or is calculating a scale factor the only way?

BTW I'm using Android 2.1 SDK.


Things have changed a lot since 2011. If you're still reading this, update to min SDK to 15 (IceCreamSandwich) and you can do this either by using ViewPropertyAnimators, or using constraint animations, if you include ConstraintLayout.


Use a grid-view or recyclerview instead of seperate layouts.

Constraint layout is the way to go.

<?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>

And then the animation logic:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜