Home
JavaEE
Java GUI
Framework
DHtml
Android
插画
留言本
  • Android中AnimationDrawable实现简单动画的例子
  • Android中用Bitmap获取图片中某个区域的图象
  • s60v3的6120c与蓝牙耳机nokia BH-320的配对连接方法
  • 三月雨后大王山下娇艳欲滴的杜鹃
  • 必须转载的一篇非常感人的文章!
  • 用Handler重画和移动View的方法
  • Animation与Interpolator及AnimationSet的用法
  • 用LiveAndroid来调试运行Android的apk程序
  • 收到Google Voice的邀请,要US电话pin,无法开通
  • 今天居然收到Google Wave的开通邀请
  • 在客户端IE中使用jasperView,客户端显示JasperViewer
  • jspsmartupload取得参数的值是乱码的问题
  • 客户端显示JasperViewer并在Jasperview的applet中加超链接
  • Hibernate基于链接表的多对多many-to-many双向关联例子
  • Hibernate双主键的设置例子
  • Hibernate中的properties和formula的用法
  • Android中,Drawable接口及其子类的使用方法
  • Android中用Bitmap获取图片中某个区域的图象
  • Android中AnimationDrawable实现简单动画的例子
  • Animation与Interpolator及AnimationSet的用法
Android游戏开发中对象移动的例子
posted by David Chen at May 6, 2009, 5:22 PM

该方法只是个人在学习过程中找到的一个方法,因为在网上找Android的游戏开发相关的内容,根本就找不到什么有用的代码。 只是自已总结一下,是个人学习之用,不知是不是不有其它更好的方法。要深入学习了解后才能发现。先记下来再说。
该游戏对象移动,以view的背景作动画,假设开始的位置是父View坐标的(0,0)
游戏角色view的事件监听应该在父view中监听。
同时注意,AnimationDrawable.start()不能在Activity.onCreate事件未执行完就调用,
但可以用在比如点击按纽后就调用等。

public class Dog extends View implements OnKeyListener, Runnable {

// 左右移动对应不同的背景动画,用这个记下当前的背景动画
private AnimationDrawable nowAnim;
// 左移动对应的背景动画
private AnimationDrawable animLeft;
// 右移动对应的背景动画
private AnimationDrawable animRight;
private View parent;

// 记下view的坐标位置,用于移动后重画该区域。该坐标是相对于父View的。
// 同时要注意,如果父View是有padding的,要算上,因为是以父Vew的左上角点为原点的
// padding最好为每次位移量的正倍数。l,t,r,b为实时的坐标位置,step为每次位移量
private int l=0,t=0,r=22,b=20,step=3;

public Dog(Context context, View parent) {
super(context);
// TODO Auto-generated constructor stub
this.parent = parent;
// 记下父容器,即父View

l += parent.getPaddingLeft(); t += parent.getPaddingTop();
r += parent.getPaddingRight(); b += parent.getPaddingBottom();
// 如果父view有padding,要算上,因为view的坐标是以父view的左上角为原点的

// 生成左和右移动对应的背景动画,其实只是一个png,有对象移动时是的几个图片,将其分拆成移动的动画
animLeft = AndroidUtils.animationFromSplitImage(
context, R.drawable.woniu, 22, 20, 200);
// 只需一个移动方向的png就行了,该animationFromSplitImage函数是将图像水来翻转。
animRight = AndroidUtils.animationFromSplitImage(
AndroidUtils.imageFlipHorizintal(context, R.drawable.woniu), 22, 20, 200);

setAnimationDrawable( animRight );
//this.setOnKeyListener( this );

}

private void setAnimationDrawable(AnimationDrawable anim) {
if( nowAnim != null ) {
nowAnim.stop();
// 必须要先stop才行,不然会影响到下一个动画的情况
nowAnim = null;
}
nowAnim = anim;
this.setBackgroundDrawable(nowAnim);
}

// 返回当前的动画,用于在其它地方控制停止等。
public AnimationDrawable getBgAnimationDrawable() {
return nowAnim;
}

public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if( event.getAction() == KeyEvent.ACTION_DOWN )
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_LEFT: {
if( getLeft()>parent.getPaddingLeft() ) {
// 左或右位移量,是相对当前View的坐标
offsetLeftAndRight(-1*step);
// 记下当前的坐标值,用于在父view中重画经过的区域。
l -= step;
}
if( animLeft != nowAnim ) {
setAnimationDrawable( animLeft );
nowAnim.start();
}
break ;
}
case KeyEvent.KEYCODE_DPAD_RIGHT: {
if( getRight()<(parent.getWidth() - parent.getPaddingRight()) ) {
offsetLeftAndRight(step);
r += step;
}
if( animRight != nowAnim ) {
setAnimationDrawable( animRight );
nowAnim.start();
}
break ;
}
case KeyEvent.KEYCODE_DPAD_UP: {
if( getTop()>parent.getPaddingTop() ) {
offsetTopAndBottom(-1*step);
t -= step;
}
break ;
}
case KeyEvent.KEYCODE_DPAD_DOWN: {
if( getBottom()<(parent.getHeight() - parent.getPaddingBottom()) ) {
offsetTopAndBottom(step);
b += step;
}
break ;
}
case KeyEvent.KEYCODE_DPAD_CENTER: {
break ;
}
default: {}
}
repaint();
}
return true;
}

public void run() {
// TODO Auto-generated method stub
// 每次位移后,都重画位移前的区域的内容
parent.invalidate(l,t,r,b);
}

public void repaint() {
this.post(this);
}
}
Labels:   Java    Android  
Trackback:   http://cwq.iou1314.com/android-animation_a362
本站内的任何文章,只代表个人意见或学习所用,如有版权声明,请尊重作者的劳动成果,在转载时请保留原始链接并注明出处。
© 2007 - 2010 CWQ.IOU1314.COM All Rights Reserved.