在Android游戏开发中,时常会遇到需要人物跟随手势的移动而移动,那么怎么实现呢?请看下面代码:
package com.lyc;
import android.app.Activity;
import android.os.Bundle;
import java.util.ArrayList;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
public class testAcvivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 全屏显示窗口
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
CustomView1 myView = new CustomView1(this);
setContentView(myView);
}
class CustomView1 extends View {
// private Paint paint;
private ArrayList<PointF> graphics = new ArrayList<PointF>();
private Paint lPaint;
private Path mPath;
private int of = 0;
private Boolean over =false;
private float mX, mY;
private Bitmap bit,bmp;
private static final float TOUCH_TOLERANCE = 3;
public CustomView1(Context context) {
super(context);
// paint = new Paint(Paint.ANTI_ALIAS_FLAG); // 抗锯齿
// paint.setColor(Color.WHITE);
// paint.setStrokeJoin(Paint.Join.ROUND); // 让画的线圆滑
// paint.setStrokeCap(Paint.Cap.ROUND);
// paint.setStrokeWidth(3); // 画笔划线的宽度,设为35可以将一个点画成一个圆,便于观察
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.yh);
lPaint = new Paint(Paint.ANTI_ALIAS_FLAG); //画线条
lPaint.setColor(Color.BLACK);
lPaint.setStyle(Paint.Style.STROKE);//空心
lPaint.setStrokeJoin(Paint.Join.ROUND);
lPaint.setStrokeCap(Paint.Cap.ROUND);
lPaint.setStrokeWidth(3);
mPath = new Path();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
over = false;
graphics.clear();
of=0;
graphics.add(new PointF(x, y));
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
graphics.add(new PointF(x, y));
touch_move(x, y);
invalidate(); //使整个窗口客户区无效
break;
case MotionEvent.ACTION_UP:
over = true;
touch_up();
invalidate();
break;
}
return true;
}
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
private void touch_up() {
//mPath.lineTo(mX, mY);
}
@Override
protected void onDraw(Canvas canvas) {
// canvas.drawColor(Color.TRANSPARENT,Mode.CLEAR);
canvas.drawColor(Color.BLACK);//画板颜色
canvas.drawPath(mPath, lPaint);
if(over&&graphics.size()>0){
// canvas.drawPoint(graphics.get(of).x, graphics.get(of).y, paint);
canvas.drawBitmap(bmp, graphics.get(of).x-25, graphics.get(of).y-25,lPaint);
of+=1;
if(of<graphics.size()){
if(of==graphics.size()-1){
mPath.reset();//移动完成后移除线条
}
invalidate();
}
}
}
}
}