Animator记录一次属性动画实现的逐渐出现和逐渐消失的动画

应用场景,View出场逐渐由小到大显示,View退出逐渐由大到小的效果:

代码:

Activity部分:

package com.lenovo.dh.zuidemo.ui.activity;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.lenovo.dh.zuidemo.R;

public class AnimatorActivity extends AppCompatActivity {
    private Button btAnimator,btAnimatorStop;
    private ImageView img;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zui_animator);
        btAnimator = (Button)findViewById(R.id.btAnimator);
        btAnimatorStop = (Button)findViewById(R.id.btAnimatorStop);
        img = (ImageView)findViewById(R.id.img);
        btAnimator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //逐渐消失动画
                img.setImageResource(R.mipmap.ic_launcher);
                ObjectAnimator animator = ObjectAnimator.ofInt(new WrapView(img),"width",10);
                animator.setDuration(10000);
                //animator.setRepeatCount(1); //动画重复的次数
                animator.start();
            }
        });


        btAnimatorStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //逐渐出现动画
                img.setImageResource(R.mipmap.ic_launcher);
                Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.enlarge);
                animation.setFillAfter(true);
                img.startAnimation(animation);
            }
        });
    }



    class WrapView{
        private View view;
        private int width;
        private int height;
        public WrapView(View view){
            this.view = view;
        }

        public int getWidth(){
            return view.getLayoutParams().width;
        }

        public void setWidth(int width){
            this.width = width;
            view.getLayoutParams().width = width;
            view.requestLayout();
        }

        public int getHeight(){
            return view.getLayoutParams().height;
        }

        public void setHeight(){
            this.height = height;
            view.getLayoutParams().height = height;
            view.requestLayout();
        }
    }
}

layout部分:

zui_animator.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <Button
        android:id="@+id/btAnimator"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="30dp"
        android:text="动画逐渐消失"
        />

    <Button
        android:id="@+id/btAnimatorStop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="30dp"
        android:text="动画逐渐出现"
        />

       <ImageView
           android:id="@+id/img"
           android:layout_width="200dp"
           android:layout_height="200dp"
           android:layout_gravity="center_horizontal"
           android:layout_marginTop="50dp"
           />
</LinearLayout>

 

res/anim部分:

enlarge.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false"
    >
   <scale
      android:duration="10000"
      android:fromXScale="0"
      android:fromYScale="0"
       android:pivotX="50%"
      android:pivotY="50%"
      android:startOffset="0"
      android:toXScale="1.0"
      android:toYScale="1.0"
   />

   <!--
      //动画消耗的时长
      android:duration="1000"
      //动画开始前X,Y的缩放,0.0为不显示,1.0为正常显示
      android:fromXScale="0"
      android:fromYScale="0"
      //动画起始位置,相对于屏幕的百分比,两个都为50%,表示从屏幕中间开始
       android:pivotX="50%"
      android:pivotY="50%"
      //动画多次执行的时间间隔,0只执行一次
      android:startOffset="0"
      //动画最终放大的倍数
      android:toXScale="1.0"
      android:toYScale="1.0"
   -->
</set>

(由于受限,无法上传效果视频)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值