android 模擬觸控板控制滑鼠(解決小螢幕控制大螢幕)
阿新 • • 發佈:2019-02-03
一模擬觸控板類:
package com.example.lxb.paintview; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.util.AttributeSet; import android.view.MotionEvent;import android.view.View; import android.widget.RelativeLayout; /** * Created by lxb on 2017/2/16. */ public class PaintView extends RelativeLayout { private Paint paint; private Canvas cacheCanvas; private Bitmap cachebBitmap; private Path path; private Context context; private intmScreenWidth = 0; private int mScreenHeight = 0; public Bitmap getCachebBitmap() { return cachebBitmap; } public PaintView(Context context) { super(context); this.context = context; init(); } public PaintView(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; init(); } public PaintView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context = context; init(); } private void init() { paint = new Paint(); paint.setAntiAlias(true); paint.setStrokeWidth(3); paint.setStyle(Paint.Style.STROKE); paint.setColor(Color.BLACK); path = new Path(); mScreenWidth = ScreenInfoUtils.getScreenWidth(this.context); mScreenHeight = ScreenInfoUtils.getScreenHeight(this.context); /*cachebBitmap = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888); cacheCanvas = new Canvas(cachebBitmap); cacheCanvas.drawColor(Color.WHITE);*/ } public void clear() { if (cacheCanvas != null) { paint.setColor(Color.WHITE); cacheCanvas.drawPaint(paint); paint.setColor(Color.BLACK); cacheCanvas.drawColor(Color.WHITE); invalidate(); } } @Override protected void onDraw(Canvas canvas) { // canvas.drawColor(BRUSH_COLOR); //canvas.drawBitmap(cachebBitmap, 0, 0, null); canvas.drawPath(path, paint); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { int curW = cachebBitmap != null ? cachebBitmap.getWidth() : 0; int curH = cachebBitmap != null ? cachebBitmap.getHeight() : 0; if (curW >= w && curH >= h) { return; } if (curW < w) curW = w; if (curH < h) curH = h; Bitmap newBitmap = Bitmap.createBitmap(curW, curH, Bitmap.Config.ARGB_8888); Canvas newCanvas = new Canvas(); newCanvas.setBitmap(newBitmap); if (cachebBitmap != null) { newCanvas.drawBitmap(cachebBitmap, 0, 0, null); } cachebBitmap = newBitmap; cacheCanvas = newCanvas; } private float cur_x, cur_y; //當前座標 private float downX, downY; //手指按下的座標 private float endX, endY; //手指擡起座標 private float detaX, detaY; //座標增量 @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { cur_x = x; cur_y = y; path.moveTo(cur_x, cur_y); break; } case MotionEvent.ACTION_MOVE: { path.quadTo(cur_x, cur_y, x, y); cur_x = x; cur_y = y; break; } case MotionEvent.ACTION_UP: { //cacheCanvas.drawPath(path, paint); reset(); break; } } invalidate(); return true; } /** * 清空手勢軌跡 */ public void reset() { postDelayed(new Runnable() { @Override public void run() { path.reset(); invalidate(); } }, 500); } //手指按下事件 public void setTouchDown(int x, int y) { //記錄動作開始座標 /*cur_x = x; cur_y = y;*/ downX = x; downY = y; //計算便宜向量 detaX += endX - downX; detaY += endY - downY; //invalidate(); } //手指滑動的事件 public void setTouchMove(int x, int y) { float tmpx = x + detaX > mScreenWidth ? mScreenWidth : x + detaX; cur_x = cur_x > mScreenWidth ? mScreenWidth : cur_x; //path.quadTo(cur_x, cur_y, x + detaX, y + detaY); path.quadTo(cur_x, cur_y, tmpx, y + detaY); cur_x = x + detaX; cur_y = y + detaY; invalidate(); } //手指擡起事件 public void setTouchUp(int x, int y) { //reset(); invalidate(); //記錄動作結束的座標 endX = x; endY = y; //cur_x = x; //cur_y = y; path.moveTo(cur_x, cur_y); } }
二。展示結果類:
package com.example.lxb.paintview; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; /** * Created by lxb on 2017/2/16. */ public class TestView extends View { private Paint paint; private Canvas cacheCanvas; private Bitmap cachebBitmap; private Path path; private TouchListener touchListener; public void setTouchListener(TouchListener touchListener) { this.touchListener = touchListener; } public Bitmap getCachebBitmap() { return cachebBitmap; } public TestView(Context context) { super(context); init(); } public TestView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public TestView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { paint = new Paint(); paint.setAntiAlias(true); paint.setStrokeWidth(3); paint.setStyle(Paint.Style.STROKE); paint.setColor(Color.BLACK); path = new Path(); } public void clear() { if (cacheCanvas != null) { paint.setColor(Color.WHITE); cacheCanvas.drawPaint(paint); paint.setColor(Color.BLACK); cacheCanvas.drawColor(Color.WHITE); invalidate(); } } @Override protected void onDraw(Canvas canvas) { canvas.drawPath(path, paint); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { int curW = cachebBitmap != null ? cachebBitmap.getWidth() : 0; int curH = cachebBitmap != null ? cachebBitmap.getHeight() : 0; if (curW >= w && curH >= h) { return; } if (curW < w) curW = w; if (curH < h) curH = h; Bitmap newBitmap = Bitmap.createBitmap(curW, curH, Bitmap.Config.ARGB_8888); Canvas newCanvas = new Canvas(); newCanvas.setBitmap(newBitmap); if (cachebBitmap != null) { newCanvas.drawBitmap(cachebBitmap, 0, 0, null); } cachebBitmap = newBitmap; cacheCanvas = newCanvas; } private float cur_x, cur_y; private float downX,downY; private float detaX,detaY; @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { downX = x; downY = y; cur_x = x; cur_y = y; path.moveTo(cur_x, cur_y); if(touchListener !=null){ touchListener.onTouchDown((int)cur_x,(int)cur_y); } break; } case MotionEvent.ACTION_MOVE: { path.quadTo(cur_x, cur_y, x, y); cur_x = x; cur_y = y; detaX = x - downX; detaY = y - downY; if(touchListener !=null){ touchListener.onTouchMove((int)cur_x,(int)cur_y); } break; } case MotionEvent.ACTION_UP: { cur_x = x; cur_y = y; if(touchListener !=null){ touchListener.onTouchUp((int)cur_x,(int)cur_y); } //reset(); break; } } invalidate(); return true; } /** * 清空手勢軌跡 */ public void reset() { postDelayed(new Runnable() { @Override public void run() { path.reset(); invalidate(); } }, 500); } }
三。公共介面:
package com.example.lxb.paintview; /** * 觸控監聽器 * Created by lxb on 2017/2/10. */ public interface TouchListener { void onTouchDown(int x, int y); //手指按下滑動事件 void onTouchMove(int x, int y); //手指滑動事件 void onTouchUp(int x, int y); //手指彈起事件 }
四。佈局檔案:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.example.lxb.paintview.PaintView android:id="@+id/paintview" android:layout_width="match_parent" android:background="#345678" android:layout_height="1dp" android:layout_weight="2"> </com.example.lxb.paintview.PaintView> <Button android:id="@+id/btn_clear_result" android:layout_width="match_parent" android:layout_height="44dp" android:text="清除"/> <com.example.lxb.paintview.TestView android:id="@+id/testView" android:layout_gravity="center" android:layout_width="300dp" android:layout_height="1dp" android:background="#009097" android:layout_weight="1"/> <Button android:id="@+id/btn_clear_test" android:layout_width="match_parent" android:layout_height="44dp" android:text="清除"/> </LinearLayout>
測試類:
package com.example.lxb.paintview; import android.app.Activity; import android.content.DialogInterface; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.RelativeLayout; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity { PaintView paintView; TestView view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); paintView = (PaintView)findViewById(R.id.paintview); view =(TestView)findViewById(R.id.testView); view.setTouchListener(new TouchEvent()); Button btnClearResult = (Button)findViewById(R.id.btn_clear_result); Button btnClearTest = (Button)findViewById(R.id.btn_clear_test); EventClick eventClick = new EventClick(); btnClearResult.setOnClickListener(eventClick); btnClearTest.setOnClickListener(eventClick); } private class EventClick implements View.OnClickListener { @Override public void onClick(View v) { switch(v.getId()){ case R.id.btn_clear_result: paintView.reset(); break; case R.id.btn_clear_test: view.reset(); break; } } } private class TouchEvent implements TouchListener{ @Override public void onTouchDown(int x, int y) { paintView.setTouchDown(x,y); } @Override public void onTouchMove(int x, int y) { paintView.setTouchMove(x,y); } @Override public void onTouchUp(int x, int y) { paintView.setTouchUp(x,y); } } }
測試結果圖