Android轻松画出触摸轨迹
作者:antkingwei 时间:2022-05-07 11:02:03
本文实例介绍了Android如何画出触摸轨迹的方法,分享给大家供大家参考,具体内容如下
效果图:
实现代码:
package com.android.gameview5;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
public class SurfaceViewActivity3 extends Activity {
public void onCreate(Bundle s){
super.onCreate(s);
//全屏显示
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new MyView(this));
}
public class MyView extends SurfaceView implements Callback,Runnable{
public static final int TIME_IN_FRAME =50;
Paint mPaint = null;
Paint mTextPaint = null;
SurfaceHolder mSurfaceHolder = null;
boolean mRunning = false;
Canvas mCanvas = null;
private Path mPath;
private float mPosX,mPosY;
public MyView(Context context){
super(context);
this.setFocusable(true);
this.setFocusableInTouchMode(true);
mSurfaceHolder = this.getHolder();
mSurfaceHolder.addCallback(this);
mCanvas = new Canvas();
mPaint = new Paint();
mPaint.setColor(Color.BLACK);
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(6);
mPath = new Path();
mTextPaint = new Paint();
mTextPaint.setColor(Color.BLACK);
mTextPaint.setTextSize(15);
}
public boolean onTouchEvent(MotionEvent event){
int action = event.getAction();
float x = event.getX();
float y = event.getY();
switch(action){
case MotionEvent.ACTION_DOWN:
mPath.moveTo(x, y);
break;
case MotionEvent.ACTION_MOVE:
mPath.quadTo(mPosX, mPosY, x, y);
break;
case MotionEvent.ACTION_UP:
//mPath.reset();
break;
}
//记录当前触摸点得当前得坐标
mPosX = x;
mPosY = y;
return true;
}
private void onDraw(){
mCanvas.drawColor(Color.WHITE);
//绘制曲线
mCanvas.drawPath(mPath, mPaint);
mCanvas.drawText("当前触笔X:"+mPosX,0,20,mTextPaint);
mCanvas.drawText("当前触笔Y:"+mPosY,0,40,mTextPaint);
}
public void run() {
// TODO Auto-generated method stub
while(mRunning){
long startTime = System.currentTimeMillis();
synchronized(mSurfaceHolder){
mCanvas = mSurfaceHolder.lockCanvas();
onDraw();
mSurfaceHolder.unlockCanvasAndPost(mCanvas);
}
long endTime = System.currentTimeMillis();
int diffTime = (int) (endTime - startTime);
while(diffTime<=TIME_IN_FRAME){
diffTime =(int)(System.currentTimeMillis()-startTime);
Thread.yield();
}
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mRunning = true;
new Thread(this).start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
mRunning = false;
}
}
}
标签:Android,触摸轨迹
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot实现单文件与多文件上传功能
2023-03-22 23:44:11
![](https://img.aspxhome.com/file/2023/9/76229_0s.png)
执行java请求时导致在脚本执行结束时JVM无法退出
2023-11-25 05:57:13
了解Java虚拟机JVM的基本结构及JVM的内存溢出方式
2023-02-20 03:08:51
![](https://img.aspxhome.com/file/2023/8/63288_0s.png)
关于activemq安装配置以及启动错误的解决
2023-11-13 05:18:02
![](https://img.aspxhome.com/file/2023/5/59485_0s.png)
C#实现窗体抖动的两种方法
2021-10-06 10:20:52
![](https://img.aspxhome.com/file/2023/6/80056_0s.jpg)
第一次使用Android Studio时你应该知道的一切配置(推荐)
2022-01-08 00:49:38
![](https://img.aspxhome.com/file/2023/0/66250_0s.png)
Java的Spring框架中AOP项目的一般配置和部署教程
2021-07-26 16:49:25
Unity使用LineRender断笔写字
2023-01-07 09:29:38
![](https://img.aspxhome.com/file/2023/0/86950_0s.png)
SpringBoot路径映射实现过程图解
2023-11-13 04:01:11
![](https://img.aspxhome.com/file/2023/6/59376_0s.png)
java.util.NoSuchElementException原因及两种解决方法
2022-02-10 15:18:58
一步步教你写一个SpringMVC框架
2022-11-26 03:37:21
![](https://img.aspxhome.com/file/2023/9/93139_0s.jpg)
SpringCloud Gateway使用详解
2023-11-27 02:54:36
java编码IDEA主题推荐
2021-10-21 03:54:18
![](https://img.aspxhome.com/file/2023/6/85506_0s.jpg)
关于C#中yield return用法的思考
2021-11-30 14:05:40
听说用了YYYY-MM-dd的程序员,前些天都在加班改Bug
2023-07-05 17:48:00
![](https://img.aspxhome.com/file/2023/2/77142_0s.jpg)
基于Mybatis plus 自动代码生成器的实现代码
2023-11-24 10:40:51
Spring Boot+Mybatis的整合过程
2023-12-20 06:50:18
![](https://img.aspxhome.com/file/2023/7/84317_0s.png)
Spring Boot实现配置文件的自动加载和刷新功能
2023-07-14 02:26:55
![](https://img.aspxhome.com/file/2023/1/82381_0s.png)
Java常用类String的面试题汇总(java面试题)
2023-11-23 20:40:45
c#使用filesystemwatcher监视文件系统的变化
2022-08-06 15:28:43