and

本文介绍了一个自定义的动画线程类实现,该类基于Android视图进行缩放动画处理,支持动态调整动画状态和参数,并提供了动画监听接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


import android.util.Log;
import android.view.View;

public class AnimationThread extends Thread {
private static final String TAG = AnimationThread.class.getSimpleName();
public static final long DEFAULT_SLEEP_TIME = 25;
public static final long DEFAULT_INCREMENT = 1;
public static final int STATE_ZOOM_UNKNOW = 0x00;
public static final int STATE_ZOOM_OUT = 0x01;
public static final int STATE_ZOOM_IN = 0x02;

public interface AnimationListener{
void onStart();
void onEnd();
void onError();
}

private float currentScale;
private float startScale;
private float endScale;
private float increment;
private long sleepTime;
private int animationState;
private boolean doNotNeedAnimation;
private boolean normalOver;
private View view;
private AnimationListener listener;

public AnimationThread(View view, float startScale, float endScale) {
this.view = view;
this.startScale = startScale;
this.endScale = endScale;
currentScale = startScale;
sleepTime = DEFAULT_SLEEP_TIME;
increment = DEFAULT_INCREMENT;
if (startScale > endScale) {
animationState = STATE_ZOOM_OUT;
} else if (startScale < endScale) {
animationState = STATE_ZOOM_IN;
} else {
animationState = STATE_ZOOM_UNKNOW;
}
}

public AnimationThread(View view, float startScale, float endScale, float increment) {
this(view, startScale, endScale);
this.increment = increment;
}

public AnimationThread(View view, float startScale, float endScale, float increment, long sleepTime) {
this(view, startScale, endScale, increment);
this.sleepTime = sleepTime;
}

public float getCurrentScale() {
return currentScale;
}

public void setOnListener(AnimationListener listener) {
this.listener = listener;
}

public boolean isNormalOver() {
return normalOver;
}

public void setEndScale(float value) {
endScale = value;
}

public void setCurrentScale(float value) {
currentScale = value;
}

public void setSleepTime(long time) {
sleepTime = time;
}

public void setAnimationState(int flag) {
if (flag != STATE_ZOOM_OUT && flag != STATE_ZOOM_IN) {
throw new IllegalArgumentException("flag[" + flag + "] value is error.");
}

if (animationState != flag) {
animationState = flag;
synchronized (TAG) {
if (animationState == STATE_ZOOM_OUT) {
if (currentScale < endScale) {
switchStart2EndScaleLocked();
}
} else {
if (currentScale > endScale) {
switchStart2EndScaleLocked();
}
}
}
}
}
public void changeAnimationState() {
if (animationState == STATE_ZOOM_IN) {
synchronized (TAG) {
animationState = STATE_ZOOM_OUT;
if (currentScale < endScale) {
switchStart2EndScaleLocked();
}
}
} else {
synchronized (TAG) {
animationState = STATE_ZOOM_IN;
if (currentScale > endScale) {
switchStart2EndScaleLocked();
}
}
}
}

public int getAnimationState() {
return animationState;
}

public void stopAnimation() {
doNotNeedAnimation = true;
}

@Override
public void run() {
try {
if (listener != null) {
listener.onStart();
}

while (!doNotNeedAnimation) {
synchronized (TAG) {
if (animationState == STATE_ZOOM_OUT) {
currentScale -= increment;
if (currentScale <= endScale) {
currentScale = endScale;
normalOver = true;
break;
}
} else if (animationState == STATE_ZOOM_IN) {
currentScale += increment;
if (currentScale >= endScale) {
currentScale = endScale;
normalOver = true;
break;
}
} else {
break;
}
}

view.postInvalidate();
sleep(sleepTime);
}

view.postInvalidate();

} catch (Exception e) {
Log.w(TAG, "thread interrupted case=" + e.toString());
if (listener != null) {
listener.onError();
}
}

if (normalOver && listener != null) {
listener.onEnd();
}
}

private void switchStart2EndScaleLocked() {
float tmp = endScale;
endScale = startScale;
startScale = tmp;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值