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

lichking8384 2010-09-13 09:08:13
为什么从(0,0)到(100,100)是预期的效果,而从(100,100)到(0,0),高手求救啊!!代码如下。
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 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>
...全文
4385 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ting_Ian 2011-04-28
  • 打赏
  • 举报
回复
我想问下假如我在view中放置了很多张图片,但是我想针对某一张图片做动画效果,请问怎么实现呢?
new8899_ 2011-03-26
  • 打赏
  • 举报
回复
看看先,现在我也在做个图片移动进入的动画,
lichking8384 2010-09-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yyy025025025 的回复:]
那就分情况了嘛

飞入时坐标是(50,50) 到 (100, 100)
Animation anim = new TranslateAnimation(startX, aimX, startY
+ bmpDraw.getBitmap().getHeight(), aimY
+ bmpDraw.getBitmap().getHeight());


飞出时坐标是(100, 100)……
[/Quote]
朋友,你好!!我有试过

Animation anim = new TranslateAnimation(100, 100, 0, 0);
大约到(50,50)时,动画结束了。。。

Animation anim = new TranslateAnimation(200, 200, 0, 0);
大约到(100,100)时,动画结束

也是就说只能走到两点连线的中间。。。
但是,如果起点为(0,0)的话,貌似正常。。无法理解


yyy025025025 2010-09-13
  • 打赏
  • 举报
回复
那就分情况了嘛

飞入时坐标是(50,50) 到 (100, 100)
Animation anim = new TranslateAnimation(startX, aimX, startY
+ bmpDraw.getBitmap().getHeight(), aimY
+ bmpDraw.getBitmap().getHeight());


飞出时坐标是(100, 100) 到(0,0)
Animation anim = new TranslateAnimation(startX, aimX, startY, aimY);
lichking8384 2010-09-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yyy025025025 的回复:]
看代码中这句话

Animation anim = new TranslateAnimation(startX, aimX, startY
+ bmpDraw.getBitmap().getHeight(), aimY
+ bmpDraw.getBitmap().getHeight());

会设置animX和animY的位置为 0+bmpDraw.getBitmap().……
[/Quote]

如果我只想让图片从0,0飞到100,100再从100,100飞到0,0该怎么调呢。。。
lichking8384 2010-09-13
  • 打赏
  • 举报
回复
(0,0)到(100,100)时,如果去掉这句话,图片会从屏幕外飞进来。
关键是,(100,100)飞到(0,0)时,大约图片飞到(50,50)就会结束。。这是为什么。。。
yyy025025025 2010-09-13
  • 打赏
  • 举报
回复
看代码中这句话

Animation anim = new TranslateAnimation(startX, aimX, startY
+ bmpDraw.getBitmap().getHeight(), aimY
+ bmpDraw.getBitmap().getHeight());

会设置animX和animY的位置为 0+bmpDraw.getBitmap().getHeight()

那么在图片高度不为0的情况下,就不会移动到(0,0)点了。

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧