自定义View-补间动画TextView

效果图

在这里插入图片描述

MyAnimTextView

/**
* Author: jVen
* Time: 2020/8/24 17:53
* Description: 补间动画View
*/
public class MyAnimTextView extends AppCompatTextView {

  private Animation mAlpha;
  private Animation mTranslate;
  private Animation mScale;
  private Animation mRotate;
  private MyAnimListener myAnimListener;

  public MyAnimTextView(@NonNull Context context) {
      super(context);
  }

  public MyAnimTextView(@NonNull Context context, @Nullable AttributeSet attrs) {
      super(context, attrs, android.R.attr.textViewStyle);
      mAlpha = AnimationUtils.loadAnimation(context, R.anim.alpha);
      mTranslate = AnimationUtils.loadAnimation(context, R.anim.translate);
      mScale = AnimationUtils.loadAnimation(context, R.anim.scale);
      mRotate = AnimationUtils.loadAnimation(context, R.anim.rotate);

      TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyAnimTextView);
      final int anInt = typedArray.getInt(R.styleable.MyAnimTextView_animType, 0);
      typedArray.recycle();

      mAlpha.setInterpolator(new CycleInterpolator(0.5f));
      mTranslate.setInterpolator(new BounceInterpolator());
      mTranslate.setFillAfter(true);
      mScale.setInterpolator(new BounceInterpolator());
      mRotate.setInterpolator(new BounceInterpolator());
      mRotate.setFillAfter(true);

      setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View view) {
              switch (anInt) {
                  case 0:
                      startAnimation(mAlpha);
                      break;
                  case 1:
                      startAnimation(mTranslate);
                      break;
                  case 2:
                      startAnimation(mScale);
                      break;
                  case 3:
                      startAnimation(mRotate);
                      break;
              }
          }
      });

      mAlpha.setAnimationListener(new Animation.AnimationListener() {
          @Override
          public void onAnimationStart(Animation animation) {
              setClickable(false);
              setFocusable(false);
          }

          @Override
          public void onAnimationEnd(Animation animation) {
              setClickable(true);
              setFocusable(true);
              if (myAnimListener != null) {
                  myAnimListener.onClick(MyAnimTextView.this);
              }
          }

          @Override
          public void onAnimationRepeat(Animation animation) {

          }
      });

      mTranslate.setAnimationListener(new Animation.AnimationListener() {
          @Override
          public void onAnimationStart(Animation animation) {
              setClickable(false);
              setFocusable(false);
          }

          @Override
          public void onAnimationEnd(Animation animation) {
              setClickable(true);
              setFocusable(true);
              if (myAnimListener != null) {
                  myAnimListener.onClick(MyAnimTextView.this);
              }
          }

          @Override
          public void onAnimationRepeat(Animation animation) {

          }
      });

      mScale.setAnimationListener(new Animation.AnimationListener() {
          @Override
          public void onAnimationStart(Animation animation) {
              setClickable(false);
              setFocusable(false);
          }

          @Override
          public void onAnimationEnd(Animation animation) {
              setClickable(true);
              setFocusable(true);
              if (myAnimListener != null) {
                  myAnimListener.onClick(MyAnimTextView.this);
              }
          }

          @Override
          public void onAnimationRepeat(Animation animation) {

          }
      });

      mRotate.setAnimationListener(new Animation.AnimationListener() {
          @Override
          public void onAnimationStart(Animation animation) {
              setClickable(false);
              setFocusable(false);
          }

          @Override
          public void onAnimationEnd(Animation animation) {
              setClickable(true);
              setFocusable(true);
              if (myAnimListener != null) {
                  myAnimListener.onClick(MyAnimTextView.this);
              }
          }

          @Override
          public void onAnimationRepeat(Animation animation) {

          }
      });

  }

  public void setMyAnimListener(MyAnimListener myAnimListener) {
      this.myAnimListener = myAnimListener;
  }

  public MyAnimTextView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
  }

  public void setMyAnim(final Animation animation) {
      setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View view) {
              startAnimation(animation);
          }
      });
  }

  public interface MyAnimListener {
      void onClick(MyAnimTextView view);
  }
}

新建declare-styleable

    <declare-styleable name="MyAnimTextView">

        <attr name="animType">
            <enum name="alpha" value="0" />
            <enum name="translate" value="1" />
            <enum name="scale" value="2" />
            <enum name="rotate" value="3" />
        </attr>
    </declare-styleable>

使用

    <com.weiou.mydemo.anim.MyAnimTextView
        android:id="@+id/tv4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="rotate"
        app:animType="rotate" />
MyAnimTextView tv4 = findViewById(R.id.tv4);
tv4.setMyAnimListener(new MyAnimTextView.MyAnimListener() {
    @Override
    public void onClick(MyAnimTextView view) {
        LogUtil.e("动画播放结束后会执行");
    }
});

属性说明

  • animType
    • alpha 透明度动画
    • translate 平移动画
    • scale 缩放动画
    • rotate 选装动画

动画代码贴出

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromAlpha="1.0"
    android:toAlpha="0.5">
</alpha>
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fromDegrees="0"
    android:toDegrees="-270"
    >
</rotate>
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXScale="1"
    android:fromYScale="1"
    android:toXScale="1.5"
    android:toYScale="1.5"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fillAfter="true"
    >
</scale>
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="100%"
    android:toYDelta="0"
    >
</translate>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值