图片在view中调用Animation动画移动

本文介绍了一个名为DemonView的自定义视图类,用于在Android应用中展示动画效果。通过使用Animation类和Canvas,实现了从一个位置移动到另一个位置的动画,并在不同阶段更新视图状态。

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

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值