java code
package com.test.AnimationDemo;
import java.io.InputStream;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.view.animation.TranslateAnimation;
/**
* 2010-09-XX
*
* @author Chunter
*
*/
public class DemonView extends View {
InputStream inputSteam = this.getContext().getResources()
.openRawResource(R.drawable.icon);
BitmapDrawable bmpDraw = new BitmapDrawable(inputSteam);
float startX, startY, aimX, aimY;
int msgTp = 1;
public DemonView(Context context, AttributeSet attrs) {
super(context, attrs);
new T1().execute(new Object());
}
Animation mCurrentAnimation = null;
Transformation mTransformation = new Transformation();
Handler myHandler = new Handler() {
public synchronized void handleMessage(Message msg) {
switch (msg.what) {
case 1:
initAndStartAmination(0, 0, 200, 200, bmpDraw); //ok
;
break;
case 2:
initAndStartAmination(100.0f, 100.0f, 0, 0, bmpDraw); //这里就会有问题,实际没有移动到(0,0)
}
super.handleMessage(msg);
}
};
public synchronized void onDraw(Canvas canvas) {
// this.initalAmination(int startX,int start Y)
this.makeBallMove(canvas, this.bmpDraw);
}
public void initAndStartAmination(float startX, float startY, float aimX,
float aimY, BitmapDrawable bmpDraw) {
Animation anim = new TranslateAnimation(startX, aimX, startY
+ bmpDraw.getBitmap().getHeight(), aimY
+ bmpDraw.getBitmap().getHeight());
this.startX = startX;
this.startY = startY;
this.aimX = aimX;
this.aimY = aimY;
this.mTransformation = new Transformation();
anim.setDuration(1000); // 1s
anim.setInterpolator(new AccelerateInterpolator(1.0f));
anim.setFillAfter(true);
startAnimation(anim);
}
public void startAnimation(Animation animation) {
// animation.setStartTime(animation.);
setAnimation(animation);
invalidate();
}
public void setAnimation(Animation animation) {
mCurrentAnimation = animation;
if (animation != null) {
animation.reset();
}
}
public void makeBallMove(Canvas canvas, BitmapDrawable bmpDraw) {
long curTime = SystemClock.uptimeMillis();
if (mCurrentAnimation == null) {
canvas.drawBitmap(bmpDraw.getBitmap(), 0, 0, null);
} else {
if (!mCurrentAnimation.isInitialized()) // initialize animation
mCurrentAnimation.initialize(0, 0, 0, 0);
boolean more = mCurrentAnimation.getTransformation(curTime,
mTransformation);
if (more) {
Matrix m = canvas.getMatrix();
canvas.setMatrix(mTransformation.getMatrix());
canvas.drawBitmap(bmpDraw.getBitmap(), startX, startY, null);
canvas.setMatrix(m);
this.invalidate();
} else {
// canvas.drawBitmap(bmpDraw.getBitmap(), aimX, aimY, null);
this.mCurrentAnimation = null;
msgTp = (msgTp + 1) % 2 == 0 ? 2 : 1;
}
}
}
private class T1 extends AsyncTask {
Object lock = new Object();
@Override
protected Object doInBackground(Object... arg0) {
while (true) {
try {
Thread.sleep(1999);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (mCurrentAnimation == null) {
messageSend(msgTp);
}
}
}
protected void onPostExecute(Object obj) {
// do Nothing
}
}
/**
* notify UI Thread to refresh the ChessBoard
*
* @param i
*/
private synchronized void messageSend(int i) {
Message message = new Message();
message.what = i;
myHandler.sendMessage(message);
}
}
xml code
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<com.test.AnimationDemo.DemonView android:id="@+id/animotion_test" android:layout_width="match_parent"
android:layout_height="match_parent" tileSize="24"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#ff0000"
android:textSize="24sp"
/>
</RelativeLayout>
</FrameLayout>
package com.test.AnimationDemo;
import java.io.InputStream;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.view.animation.TranslateAnimation;
/**
* 2010-09-XX
*
* @author Chunter
*
*/
public class DemonView extends View {
InputStream inputSteam = this.getContext().getResources()
.openRawResource(R.drawable.icon);
BitmapDrawable bmpDraw = new BitmapDrawable(inputSteam);
float startX, startY, aimX, aimY;
int msgTp = 1;
public DemonView(Context context, AttributeSet attrs) {
super(context, attrs);
new T1().execute(new Object());
}
Animation mCurrentAnimation = null;
Transformation mTransformation = new Transformation();
Handler myHandler = new Handler() {
public synchronized void handleMessage(Message msg) {
switch (msg.what) {
case 1:
initAndStartAmination(0, 0, 200, 200, bmpDraw); //ok
;
break;
case 2:
initAndStartAmination(100.0f, 100.0f, 0, 0, bmpDraw); //这里就会有问题,实际没有移动到(0,0)
}
super.handleMessage(msg);
}
};
public synchronized void onDraw(Canvas canvas) {
// this.initalAmination(int startX,int start Y)
this.makeBallMove(canvas, this.bmpDraw);
}
public void initAndStartAmination(float startX, float startY, float aimX,
float aimY, BitmapDrawable bmpDraw) {
Animation anim = new TranslateAnimation(startX, aimX, startY
+ bmpDraw.getBitmap().getHeight(), aimY
+ bmpDraw.getBitmap().getHeight());
this.startX = startX;
this.startY = startY;
this.aimX = aimX;
this.aimY = aimY;
this.mTransformation = new Transformation();
anim.setDuration(1000); // 1s
anim.setInterpolator(new AccelerateInterpolator(1.0f));
anim.setFillAfter(true);
startAnimation(anim);
}
public void startAnimation(Animation animation) {
// animation.setStartTime(animation.);
setAnimation(animation);
invalidate();
}
public void setAnimation(Animation animation) {
mCurrentAnimation = animation;
if (animation != null) {
animation.reset();
}
}
public void makeBallMove(Canvas canvas, BitmapDrawable bmpDraw) {
long curTime = SystemClock.uptimeMillis();
if (mCurrentAnimation == null) {
canvas.drawBitmap(bmpDraw.getBitmap(), 0, 0, null);
} else {
if (!mCurrentAnimation.isInitialized()) // initialize animation
mCurrentAnimation.initialize(0, 0, 0, 0);
boolean more = mCurrentAnimation.getTransformation(curTime,
mTransformation);
if (more) {
Matrix m = canvas.getMatrix();
canvas.setMatrix(mTransformation.getMatrix());
canvas.drawBitmap(bmpDraw.getBitmap(), startX, startY, null);
canvas.setMatrix(m);
this.invalidate();
} else {
// canvas.drawBitmap(bmpDraw.getBitmap(), aimX, aimY, null);
this.mCurrentAnimation = null;
msgTp = (msgTp + 1) % 2 == 0 ? 2 : 1;
}
}
}
private class T1 extends AsyncTask {
Object lock = new Object();
@Override
protected Object doInBackground(Object... arg0) {
while (true) {
try {
Thread.sleep(1999);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (mCurrentAnimation == null) {
messageSend(msgTp);
}
}
}
protected void onPostExecute(Object obj) {
// do Nothing
}
}
/**
* notify UI Thread to refresh the ChessBoard
*
* @param i
*/
private synchronized void messageSend(int i) {
Message message = new Message();
message.what = i;
myHandler.sendMessage(message);
}
}
xml code
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<com.test.AnimationDemo.DemonView android:id="@+id/animotion_test" android:layout_width="match_parent"
android:layout_height="match_parent" tileSize="24"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#ff0000"
android:textSize="24sp"
/>
</RelativeLayout>
</FrameLayout>