当前位置: 代码迷 >> Android >> android 基于surfaceview 的多点触控兑现
  详细解决方案

android 基于surfaceview 的多点触控兑现

热度:91   发布时间:2016-05-01 20:14:03.0
android 基于surfaceview 的多点触控实现

? ? ? ?hello,各位朋友。最近接到一个功能需要实现多点触控,也就是我们通常所看到的把手指放在屏幕上,可以放大和缩小图片的 操作。在google上查找了一些资料,发现很多都是使用imageView在实现的。但是在游戏的开发过程中,使用surfaceview效率会更高一些,所以就尝试使用surfaceview来实现。

看代码:

public class DisplayActivity extends Activity {	private SurfaceView videoView;	private SurfaceHolder sfh;	private Canvas canvas;	private Paint paint;	private boolean flag = true;	private int screenW, screenH;	private Bitmap bmpIcon;	//记录两个触屏点的坐标	private int x1, x2, y1, y2;	//倍率	private float rate = 1;	//记录上次的????	private float oldRate = 1;	//记录第一次触屏时线段的长??	private float oldLineDistance;	//判定是否头次多指触点屏幕	private boolean isFirst = true;		@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.media_play);		videoView = (SurfaceView)findViewById(R.id.videoView);		sfh = videoView.getHolder();		sfh.addCallback(new DisplaySurfaceView());		DisplayMetrics dm = new DisplayMetrics(); 		getWindowManager().getDefaultDisplay().getMetrics(dm); 		screenW = dm.widthPixels; 		screenH = (dm.heightPixels)/2;	}	class DisplaySurfaceView implements SurfaceHolder.Callback{		@Override		public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {					}		@Override		public void surfaceCreated(SurfaceHolder arg0) {			new ImageThread().start();		}		@Override		public void surfaceDestroyed(SurfaceHolder arg0) {			flag = false;		}			}		@Override	public boolean onTouchEvent(MotionEvent event) {		if (event.getAction() == MotionEvent.ACTION_UP) {			isFirst = true;			oldRate = rate;		} else {			if (event.getPointerCount() > 1) { 				x1 = (int) event.getX(0);				y1 = (int) event.getY(0);				x2 = (int) event.getX(1);				y2 = (int) event.getY(1);				if (event.getPointerCount() == 2) {					if (isFirst) {						//得到第一次触屏时线段的长??						oldLineDistance = (float) Math.sqrt(Math.pow(event.getX(1) - event.getX(0), 2) + Math.pow(event.getY(1) - event.getY(0), 2));						isFirst = false;					} else {						//得到非第????触屏时线段的长度						float newLineDistance = (float) Math.sqrt(Math.pow(event.getX(1) - event.getX(0), 2) + Math.pow(event.getY(1) - event.getY(0), 2));						//获取本次的缩放比??						rate = oldRate * newLineDistance / oldLineDistance;					}				}			}		}		return true;	}		@Override	public boolean onKeyDown(int keyCode, KeyEvent event) {		return super.onKeyDown(keyCode, event);	}		class ImageThread extends Thread{		@Override		public void run() {			while (flag) {				long start = System.currentTimeMillis();				myDraw();				long end = System.currentTimeMillis();				try {					if (end - start < 50) {						Thread.sleep(50 - (end - start));					}				} catch (InterruptedException e) {					e.printStackTrace();				}			}		}	}	public void myDraw() {		try {			canvas = sfh.lockCanvas();			if (canvas != null) {				canvas.drawColor(Color.BLACK);				canvas.save();				//缩放画布(以图片中心点进行缩放,XY轴缩放比例相??				bmpIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.aa);				canvas.scale(rate, rate, screenW / 2, screenH / 2);				int width = screenW / 2 - bmpIcon.getWidth() / 2;				int height = screenH / 2 - bmpIcon.getHeight() / 2;				//绘制位图icon				canvas.drawBitmap(bmpIcon, width, height, paint);				canvas.restore();				//便于观察,这里绘制两个触点时形成的线??//				canvas.drawLine(x1, y1, x2, y2, paint);				sfh.unlockCanvasAndPost(canvas);			}		} catch (Exception e) {			e.printStackTrace();		} finally {		}	}		@Override	public void onConfigurationChanged(Configuration newConfig) {		super.onConfigurationChanged(newConfig);	}}
?

media_play.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/displayName"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:gravity="left"        android:text="321321321"        android:textColor="#AA1122" />    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="400dp" >        <SurfaceView            android:id="@+id/videoView"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_gravity="center"            android:scaleType="matrix" />    </LinearLayout>    <Button        android:id="@+id/aaaBtn"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="123321" /></LinearLayout>
?这样就实现了多点触控,放大和缩小图片的效果了,aa这个图片是自己随便找的一个,可以根据实际情况修改。
  相关解决方案