SurfaceView 的触控和轨迹球事件
1,改开始什么也没有,当第一点击的时候,图标会出现在那个地方,然后点击其它地方,图标会在触控的地方显示
2,拖着图标移动,到鼠标事件UP的地方绘画停止
import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.os.Bundle; import android.view.MotionEvent; import android.view.SurfaceView; public class Touch_TrackActivity extends Activity { /** Called when the activity is first created. */ class MYSurFaceView extends SurfaceView{ float x; float y; Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.chart_pie);//用Bitmap获取Android图标 public MYSurFaceView(Context context) { super(context); setFocusable(true);//使用Key event,setFocusable(true)可以聚焦 // TODO Auto-generated constructor stub } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub x=event.getX();//获取触控的X坐标 y=event.getY();//获取触控的Y坐标 Draw(); return true; } @Override public boolean onTrackballEvent(MotionEvent event) { // TODO Auto-generated method stub x=event.getX();//获取轨迹的X坐标 y=event.getY();//获取轨迹的Y坐标 Draw(); return true; } private void Draw() { Canvas canvas=getHolder().lockCanvas();//定义Canvas,用SurfaceHolder的lockCanvas()方法锁住画布 canvas.drawBitmap(bitmap, x, y,null);//绘画 getHolder().unlockCanvasAndPost(canvas);//解锁画布 } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new MYSurFaceView(this)); } }
main.xml里面的东西什么都不用变