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;
}
}
分享到:
相关推荐
Programming Excel with VBA and .NET Preface Part I: Learning VBA Chapter 1. Becoming an Excel Programmer Section 1.1. Why Program? Section 1.2. Record and Read Code Section 1.3. Change ...
MariaDB and MySQL Common Table Expressions and Window Functions Revealed introduces and explains CTEs and window functions, newly available in MariaDB 10.2 and MySQL 8.0, and helps you understand why ...
Wavelets and Filter Banks_MIT Lecture 1 Discrete-time Filters: Convolution Fourier Transform Lowpass and Highpass Filters Lecture 2 Sampling Rate Change Operations: Upsampling and Downsampling ...
This book is in the form of a handbook providing tables and comprehensive lists of definitions concepts theorems and formulae Its emphasis is on basic statistics but also covers many advanced topics...
unity序列化编辑器插件----Odin Inspector and Serializ v3.2.1.0 Odin Inspector and Serializer v3.2.1.0 Odin Inspector and Serializer v3.2.1.0 Odin Inspector and Serializer v3.2.1.0 Odin Inspector and ...
The updated revision of the well-respected book on analyzing aircraft performance, This Second Edition of the bestselling Aircraft Control and Simulation has been expanded and updated to include the ...
在Swing中,LookAndFeel是一个关键概念,它决定了应用程序的视觉样式,包括控件的外观、颜色、字体等。Mac风格的LookAndFeel是专门为在Mac OS环境下提供与系统一致的UI体验而设计的。 LookAndFeel提供了跨平台的...
Whether for computer evaluation of otherworldly terrain or the latest high definition 3D blockbuster, digital image processing involves the acquisition, analysis, and processing of visual information ...
Convex Analysis and Minimization Algorithms II Advanced Theory and Bundle Methods Authors: Hiriart-Urruty, Jean-Baptiste, Lemarechal, Claude From the reviews: "The account is quite detailed and is ...
CANdb++软件是一款专为CAN(Controller Area Network)通信协议设计的数据库开发工具,它主要用于创建、编辑和管理CAN数据库。这些数据库包含了汽车电子系统中CAN网络上的信号定义、帧结构、节点信息等关键数据,是...
The second edition of Space Vehicle Dynamics and Controls includes over 260 pages of new material on the recent advances in dynamical modeling and control of advanced spacecraft such as agile imaging ...
CANdb++是一款专业的CAN(Controller Area Network)数据库编辑软件,主要用来创建、编辑和管理DBC(DBC是CAN数据库的文件格式,全称为Database for Communication)文件。DBC文件在汽车电子系统、工业自动化以及...
CANdb++ 3.0 SP27 最新版下载 Windows 7 (32 and 64 bit), Windows 10 New features: Optimization of the calculation of minimum and maximum values in signal dialog Correction of a crash when copying nodes...
Computer Organization and Design, Fifth Edition, is the latest update to the classic introduction to computer organization. The text now contains new examples and material highlighting the emergence ...
As a consequence, the grid converters should be able to exhibit advanced functions like: dynamic control of active and reactive current injection during faults, and grid services support.This book ...
Operating Systems: Internals and Design Principles (9th Edition) By 作者: William Stallings ISBN-10 书号: 0134670957 ISBN-13 书号: 9780134670959 Edition 版本: 9 出版日期: 2017-03-23 pages 页数: (800 )...
Title: Fast Fourier Transform - Algorithms and Applications Author(s): K.R. Rao, D.N. Kim, J.-J. Hwang (auth.) Series: Signals and Communication Technology Publisher: Springer Netherlands Year: ...