服务器之家

服务器之家 > 正文

Android轻松画出触摸轨迹

时间:2021-06-22 14:09     来源/作者:antkingwei

本文实例介绍了android如何画出触摸轨迹的方法,分享给大家供大家参考,具体内容如下

效果图:

Android轻松画出触摸轨迹

实现代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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轻松画出触摸轨迹的具体方法,希望对大家的学习有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部