android 模拟触摸板控制鼠标(解决小屏幕控制大屏幕)

来源:互联网 发布:黄瓜什么意思网络用语 编辑:程序博客网 时间:2024/05/22 12:04

一模拟触摸板类:

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 int mScreenWidth = 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);        }    }}


测试结果图
















0 0
原创粉丝点击