123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- package xn.hxp.weidith;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.os.Handler;
- import android.util.AttributeSet;
- import android.view.animation.AlphaAnimation;
- import android.view.animation.Animation;
- import androidx.appcompat.widget.AppCompatImageView;
- import xn.hxp.R;
- public class SwitchImageView extends AppCompatImageView {
- private static final long DURATION = 120_000; // 120秒
- private int defaultSrc;
- private int switchedSrc;
- private final Handler handler = new Handler();
- private Runnable restoreTask;
- private boolean isSwitched = false;
- public SwitchImageView(Context context) {
- super(context);
- init(context, null);
- }
- public SwitchImageView(Context context, AttributeSet attrs) {
- super(context, attrs);
- init(context, attrs);
- }
- public SwitchImageView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- init(context, attrs);
- }
- private void init(Context context, AttributeSet attrs) {
- // 读取自定义属性
- TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SwitchImageView);
- defaultSrc = ta.getResourceId(R.styleable.SwitchImageView_defaultSrc, 0);
- switchedSrc = ta.getResourceId(R.styleable.SwitchImageView_switchedSrc, 0);
- ta.recycle();
- setImageResource(defaultSrc);
- setupClickListener();
- }
- private void setupClickListener() {
- setOnClickListener(v -> switchImageWithAnimation());
- }
- private void switchImageWithAnimation() {
- if (isSwitched) return; // 防止重复点击
- // 淡出动画
- AlphaAnimation fadeOut = new AlphaAnimation(1, 0);
- fadeOut.setDuration(300);
- fadeOut.setAnimationListener(new Animation.AnimationListener() {
- @Override
- public void onAnimationStart(Animation animation) {}
- @Override
- public void onAnimationEnd(Animation animation) {
- setImageResource(switchedSrc);
- // 淡入动画
- AlphaAnimation fadeIn = new AlphaAnimation(0, 1);
- fadeIn.setDuration(300);
- startAnimation(fadeIn);
- scheduleRestore();
- isSwitched = true;
- }
- @Override
- public void onAnimationRepeat(Animation animation) {}
- });
- startAnimation(fadeOut);
- }
- private void scheduleRestore() {
- // 取消之前的任务(如果有)
- if (restoreTask != null) {
- handler.removeCallbacks(restoreTask);
- }
- restoreTask = () -> {
- AlphaAnimation fadeOut = new AlphaAnimation(1, 0);
- fadeOut.setDuration(300);
- fadeOut.setAnimationListener(new Animation.AnimationListener() {
- @Override
- public void onAnimationStart(Animation animation) {}
- @Override
- public void onAnimationEnd(Animation animation) {
- setImageResource(defaultSrc);
- AlphaAnimation fadeIn = new AlphaAnimation(0, 1);
- fadeIn.setDuration(300);
- startAnimation(fadeIn);
- isSwitched = false;
- }
- @Override
- public void onAnimationRepeat(Animation animation) {}
- });
- startAnimation(fadeOut);
- };
- handler.postDelayed(restoreTask, DURATION);
- }
- @Override
- protected void onDetachedFromWindow() {
- super.onDetachedFromWindow();
- // 移除回调防止内存泄漏
- if (restoreTask != null) {
- handler.removeCallbacks(restoreTask);
- }
- }
- // 可选:手动恢复方法
- public void restoreImmediately() {
- if (restoreTask != null) {
- handler.removeCallbacks(restoreTask);
- }
- setImageResource(defaultSrc);
- isSwitched = false;
- }
- }
|