安卓开发-触控检测apk的实现
安卓开发-触控检测apk的实现
在经历了这么多天的努力下,终于完成了apk的开发,说起来也是很不容易的事情,但是,完成了就好。下面附上源码,希望能帮助到有同样需求的人。
-
package pointerlocation.ckl.com.gohitools;
-
-
import android.annotation.SuppressLint;
-
import android.content.Context;
-
import android.graphics.Bitmap;
-
import android.graphics.Canvas;
-
import android.graphics.Color;
-
import android.graphics.CornerPathEffect;
-
import android.graphics.Paint;
-
import android.graphics.Path;
-
import android.os.Bundle;
-
import android.support.v7.app.AppCompatActivity;
-
import android.util.SparseArray;
-
import android.view.MotionEvent;
-
import android.view.View;
-
import android.view.Window;
-
import android.view.WindowManager;
-
import android.widget.ImageView;
-
import android.widget.TextView;
-
import java.util.ArrayList;
-
public class PointerLocationView extends AppCompatActivity {
-
private TextView touchView;
-
private TextView eventlabel;
-
private int StartX;
-
private int StartY;
-
private int StopX;
-
private int StopY;
-
private float historyX;
-
private float historyY;
-
private int historySize;
-
-
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
requestWindowFeature(Window.FEATURE_NO_TITLE);
-
if (getSupportActionBar() != null) {
-
getSupportActionBar().hide();
-
}
-
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
-
WindowManager.LayoutParams.FLAG_FULLSCREEN);
-
setContentView(R.layout.activity_pointer_location_view);
-
final int[] event_label_arr = new int[]{R.id.event_label0, R.id.event_label1, R.id.event_label2, R.id.event_label3,
-
R.id.event_label4, R.id.event_label5, R.id.event_label6, R.id.event_label7, R.id.event_label8, R.id.event_label9};
-
final int[] ColorArr = new int[]{
-
Color.rgb(255, 0, 0), Color.rgb(0, 128, 0),
-
Color.rgb(0, 139, 139), Color.rgb(0, 0, 255),
-
Color.rgb(255, 0, 255), Color.rgb(255, 255, 0),
-
Color.rgb(139, 0, 0), Color.rgb(0, 100, 0),
-
Color.rgb(0, 0, 139), Color.rgb(0, 255, 0)};
-
WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
-
final int width = wm.getDefaultDisplay().getWidth();
-
final int height = wm.getDefaultDisplay().getHeight();
-
final ImageView iv = findViewById(R.id.iv);
-
final Bitmap bitmap = null;
-
final Bitmap bitmap1 = bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
-
final SparseArray<Path> mPaths = new SparseArray<>();
-
final Paint paint0 = new Paint();
-
paint0.setStrokeWidth(3);
-
paint0.setStyle(Paint.Style.STROKE);
-
paint0.setStrokeCap(Paint.Cap.ROUND);
-
paint0.setStrokeJoin(Paint.Join.ROUND);
-
paint0.setDither(true);
-
paint0.setPathEffect(new CornerPathEffect(2));
-
paint0.setColor(ColorArr[0]);
-
paint0.setAntiAlias(true);
-
paint0.setStrokeMiter(90);
-
Paint paint1 = new Paint();
-
paint1.setStrokeWidth(3);
-
paint1.setStyle(Paint.Style.STROKE);
-
paint1.setStrokeCap(Paint.Cap.ROUND);
-
paint1.setStrokeJoin(Paint.Join.ROUND);
-
paint1.setDither(true);
-
paint1.setPathEffect(new CornerPathEffect(2));
-
paint1.setColor(ColorArr[1]);
-
paint1.setAntiAlias(true);
-
paint1.setStrokeMiter(90);
-
Paint paint2 = new Paint();
-
paint2.setStrokeWidth(3);
-
paint2.setStyle(Paint.Style.STROKE);
-
paint2.setStrokeCap(Paint.Cap.ROUND);
-
paint2.setStrokeJoin(Paint.Join.ROUND);
-
paint2.setDither(true);
-
paint2.setPathEffect(new CornerPathEffect(2));
-
paint2.setColor(ColorArr[2]);
-
paint2.setAntiAlias(true);
-
Paint paint3 = new Paint();
-
paint3.setStrokeWidth(3);
-
paint3.setStyle(Paint.Style.STROKE);
-
paint3.setStrokeCap(Paint.Cap.ROUND);
-
paint3.setStrokeJoin(Paint.Join.ROUND);
-
paint3.setDither(true);
-
paint3.setPathEffect(new CornerPathEffect(2));
-
paint3.setColor(ColorArr[3]);
-
paint3.setAntiAlias(true);
-
Paint paint4 = new Paint();
-
paint4.setStrokeWidth(3);
-
paint4.setStyle(Paint.Style.STROKE);
-
paint4.setStrokeCap(Paint.Cap.ROUND);
-
paint4.setStrokeJoin(Paint.Join.ROUND);
-
paint4.setDither(true);
-
paint4.setPathEffect(new CornerPathEffect(2));
-
paint4.setColor(ColorArr[4]);
-
paint4.setAntiAlias(true);
-
Paint paint5 = new Paint();
-
paint5.setStrokeWidth(3);
-
paint5.setStyle(Paint.Style.STROKE);
-
paint5.setStrokeCap(Paint.Cap.ROUND);
-
paint5.setStrokeJoin(Paint.Join.ROUND);
-
paint5.setDither(true);
-
paint5.setPathEffect(new CornerPathEffect(2));
-
paint5.setColor(ColorArr[5]);
-
paint5.setAntiAlias(true);
-
Paint paint6 = new Paint();
-
paint6.setStrokeWidth(3);
-
paint6.setStyle(Paint.Style.STROKE);
-
paint6.setStrokeCap(Paint.Cap.ROUND);
-
paint6.setStrokeJoin(Paint.Join.ROUND);
-
paint6.setDither(true);
-
paint6.setPathEffect(new CornerPathEffect(2));
-
paint6.setColor(ColorArr[6]);
-
paint6.setAntiAlias(true);
-
Paint paint7 = new Paint();
-
paint7.setStrokeWidth(3);
-
paint7.setStyle(Paint.Style.STROKE);
-
paint7.setStrokeCap(Paint.Cap.ROUND);
-
paint7.setStrokeJoin(Paint.Join.ROUND);
-
paint7.setDither(true);
-
paint7.setPathEffect(new CornerPathEffect(2));
-
paint7.setColor(ColorArr[7]);
-
paint7.setAntiAlias(true);
-
paint7.setStrokeMiter(90);
-
Paint paint8 = new Paint();
-
paint8.setStrokeWidth(3);
-
paint8.setStyle(Paint.Style.STROKE);
-
paint8.setStrokeCap(Paint.Cap.ROUND);
-
paint8.setStrokeJoin(Paint.Join.ROUND);
-
paint8.setDither(true);
-
paint8.setPathEffect(new CornerPathEffect(2));
-
paint8.setColor(ColorArr[8]);
-
paint8.setAntiAlias(true);
-
Paint paint9 = new Paint();
-
paint9.setStrokeWidth(3);
-
paint9.setStyle(Paint.Style.STROKE);
-
paint9.setStrokeCap(Paint.Cap.ROUND);
-
paint9.setStrokeJoin(Paint.Join.ROUND);
-
paint9.setDither(true);
-
paint9.setPathEffect(new CornerPathEffect(2));
-
paint9.setColor(ColorArr[9]);
-
paint9.setAntiAlias(true);
-
final Paint paint = new Paint();
-
paint.setStrokeWidth(4);
-
paint.setStyle(Paint.Style.STROKE);
-
paint.setStrokeCap(Paint.Cap.ROUND);
-
paint.setStrokeJoin(Paint.Join.ROUND);
-
paint.setDither(true);
-
paint.setPathEffect(new CornerPathEffect(2));
-
paint.setColor(Color.RED);
-
paint.setAntiAlias(true);
-
paint.setStrokeMiter(90);
-
final ArrayList<Paint> paints = new ArrayList<>();
-
paints.add(paint0);
-
paints.add(paint1);
-
paints.add(paint2);
-
paints.add(paint3);
-
paints.add(paint4);
-
paints.add(paint5);
-
paints.add(paint6);
-
paints.add(paint7);
-
paints.add(paint8);
-
paints.add(paint9);
-
final Canvas canvas = new Canvas(bitmap1);
-
final Path path = new Path();
-
touchView = findViewById(R.id.touch_area);
-
touchView.setOnTouchListener(new View.OnTouchListener() {
-
-
public boolean onTouch(View v, MotionEvent event) {
-
int action = event.getActionMasked();
-
switch (action) {
-
case MotionEvent.ACTION_DOWN:
-
case MotionEvent.ACTION_POINTER_DOWN:
-
int actionIndex = event.getActionIndex();
-
int PointID = event.getPointerId(actionIndex);
-
for (int i = 0; i < event.getPointerCount(); i ) {
-
int x = (int) event.getX(actionIndex);
-
int y = (int) event.getY(actionIndex);
-
eventlabel = findViewById(event_label_arr[PointID]);
-
String eventType1 = "第" (PointID 1) "个手指头按下了!";
-
String msg1 = "";
-
msg1 = "Action:" eventType1 " ";
-
msg1 = "相对坐标:X:" String.valueOf(x) ";Y:" String.valueOf(y) " ";
-
eventlabel.setText(msg1);
-
Path path = new Path();
-
path.moveTo(event.getX(actionIndex), event.getY(actionIndex));
-
mPaths.append(PointID, path);
-
}
-
break;
-
case MotionEvent.ACTION_MOVE:
-
for (int i = 0; i < event.getPointerCount(); i ) {
-
PointID = event.getPointerId(i);
-
int PointIndex = event.findPointerIndex(PointID);
-
String eventType4 = "第" (PointID 1) "个手指在移动";
-
eventlabel = findViewById(event_label_arr[PointID]);
-
int x = (int) event.getX(PointIndex);
-
int y = (int) event.getY(PointIndex);
-
String msg4 = "";
-
msg4 = "Action:" eventType4 " ";
-
msg4 = "相对坐标:" String.valueOf(x) "," String.valueOf(y) " ";
-
eventlabel.setText(msg4);
-
historySize = event.getHistorySize();
-
for (int j = 0; j < historySize; j ) {
-
historyX = event.getHistoricalX(PointIndex,j);
-
historyY = event.getHistoricalY(PointIndex,j);
-
Path path = mPaths.get(event.getPointerId(i));
-
path.lineTo(historyX,historyY);
-
}
-
Path path = mPaths.get(event.getPointerId(i));
-
path.lineTo(event.getX(i), event.getY(i));
-
}
-
for (int i = 0; i < mPaths.size(); i ) {
-
canvas.drawPath(mPaths.valueAt(i),paints.get(i));
-
}
-
iv.setImageBitmap(bitmap1);
-
break;
-
case MotionEvent.ACTION_POINTER_UP:
-
PointID = event.getPointerId(event.getActionIndex());
-
int PointIndex = event.findPointerIndex(PointID);
-
eventlabel = findViewById(event_label_arr[PointID]);
-
int x = (int) event.getX(PointIndex);
-
int y = (int) event.getY(PointIndex);
-
String eventType3 = "#ID=" (PointID 1) "的手指离开了!";
-
String msg3 = "";
-
msg3 = "Action:" eventType3 " ";
-
msg3 = "相对坐标:X:" String.valueOf(x) ",Y:" String.valueOf(y) " ";
-
eventlabel.setText(msg3);
-
break;
-
case MotionEvent.ACTION_UP:
-
String msg2 = "手指都离开了";
-
for (int i = 0; i < event_label_arr.length; i ) {
-
eventlabel = findViewById(event_label_arr[i]);
-
eventlabel.setText(msg2);
-
}
-
break;
-
}
-
return true;
-
}
-
});
-
}
-
}
注释比较多,嫌麻烦都给他删掉了。
本来实现的方式就是最笨的方式,如果大家有更好的实现办法,希望能共同讨论。
下面是mainActivity里面的内容:
-
package pointerlocation.ckl.com.gohitools;
-
-
import android.content.Intent;
-
import android.support.v7.app.AppCompatActivity;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.widget.Button;
-
-
public class MainActivity extends AppCompatActivity {
-
-
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
Button button = findViewById(R.id.PST);
-
button.setOnClickListener(new View.OnClickListener() {
-
-
public void onClick(View v) {
-
Intent intent = new Intent();
-
intent.setClass(MainActivity.this,PointerLocationView.class);
-
startActivity(intent);
-
}
-
});
-
}
-
-
}
写了这么多天,就这么短短的一些,发现开发确实不易。
下面是mainActivityLayut中的内容:
-
-
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
xmlns:app="http://schemas.android.com/apk/res-auto"
-
xmlns:tools="http://schemas.android.com/tools"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
tools:context="pointerlocation.ckl.com.gohitools.PointerLocationView">
-
<TextView
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"/>
-
-
<ImageView
-
android:id="@ id/iv"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent" />
-
<TextView
-
android:id="@ id/touch_area"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
/>
-
<TextView
-
android:id="@ id/history_label"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
/>
-
<LinearLayout
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:orientation="vertical"
-
android:gravity="center_vertical">
-
<TextView
-
android:id="@ id/event_label0"
-
android:textStyle="italic"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:orientation="vertical"
-
android:text="请随机触摸!"
-
android:gravity="center"
-
android:textSize="10sp"
-
android:textColor="#ff0000"/>
-
<TextView
-
android:id="@ id/event_label1"
-
android:textStyle="italic"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="请随机触摸1!"
-
android:gravity="top"
-
android:textSize="10sp"
-
android:textColor="#008000"/>
-
<TextView
-
android:id="@ id/event_label2"
-
android:textStyle="italic"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="请随机触摸!"
-
android:gravity="top"
-
android:textSize="10sp"
-
android:textColor="#008b8b"/>
-
<TextView
-
android:id="@ id/event_label3"
-
android:textStyle="italic"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="请随机触摸!"
-
android:gravity="top"
-
android:textSize="10sp"
-
android:textColor="#0000ff"/>
-
<TextView
-
android:id="@ id/event_label4"
-
android:textStyle="italic"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="请随机触摸!"
-
android:gravity="top"
-
android:textSize="10sp"
-
android:textColor="#00ffff"/>
-
<TextView
-
android:id="@ id/event_label5"
-
android:textStyle="italic"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="请随机触摸!"
-
android:gravity="top"
-
android:textSize="10sp"
-
android:textColor="#ff00ff"/>
-
<TextView
-
android:id="@ id/event_label6"
-
android:textStyle="italic"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="请随机触摸!"
-
android:gravity="top"
-
android:textSize="10sp"
-
android:textColor="#ffff00"/>
-
<TextView
-
android:id="@ id/event_label7"
-
android:textStyle="italic"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="请随机触摸!"
-
android:gravity="top"
-
android:textSize="10sp"
-
android:textColor="#8b0000"/>
-
<TextView
-
android:id="@ id/event_label8"
-
android:textStyle="italic"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="请随机触摸!"
-
android:gravity="top"
-
android:textSize="10sp"
-
android:textColor="#006400"/>
-
<TextView
-
android:id="@ id/event_label9"
-
android:textStyle="italic"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="请随机触摸!"
-
android:gravity="top"
-
android:textSize="10sp"
-
android:textColor="#00008b"/>
-
</LinearLayout>
-
-
</android.support.constraint.ConstraintLayout>
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgcaece
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13