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,触摸轨迹
0
投稿
猜你喜欢
详解Java设计模式之抽象工厂模式
2022-09-29 17:00:34
Android自定义View实现多边形统计图示例代码
2021-09-28 13:17:44
Android 可拖动的seekbar自定义进度值
2023-12-28 12:37:24
Java C++ 算法题解leetcode669修剪二叉搜索树示例
2022-09-22 04:56:13
C/C++与Java各数据类型所占字节数的详细比较
2022-09-08 11:39:55
springboot FeignClient注解及参数
2021-07-09 21:59:07
Android利用手势完成屏幕密码锁功能
2023-04-08 21:21:40
Android端代码量非常小的分页加载库
2022-08-25 17:24:48
C#实现简单的字符串加密
2022-12-28 17:09:29
简单实现Java通讯录系统
2021-08-16 03:28:22
如何用C#创建用户自定义异常浅析
2023-06-10 20:06:16
Java设计模式编程中的工厂方法模式和抽象工厂模式
2023-03-19 05:50:48
SpringBoot2.0整合jackson配置日期格式化和反序列化的实现
2022-06-29 14:13:52
Android TimePicker 直接输入的问题解决方案
2023-08-03 03:11:55
c# 基于wpf,开发OFD电子文档阅读器
2023-09-08 00:10:02
Android主线程和子线程区别详解
2023-12-18 17:33:38
Spring Boot实现热部署的实例方法
2023-10-24 08:12:50
Jenkins安装以及邮件配置详解
2023-04-20 12:42:39
聊聊Redis的单线程模型
2022-02-21 09:20:42
Spring MVC的web.xml配置详解
2023-08-10 04:45:01