Android上實現直接在屏幕上顯示點擊位置,方便調試,可支持多點,效果圖如下:
TestPointActivity.java
- package com.ckl.TestPoint;
-
- import java.util.ArrayList;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.View;
-
- public class TestPointActivity extends Activity {
-
- private PointView mPointView;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- mPointView = new PointView(this);
- mPointView.setImageResource(R.drawable.mj);
- mPointView.setOnTouchListener(new MyListener());
- setContentView(mPointView);
- }
-
- public class MyListener implements View.OnTouchListener {
-
- public boolean onTouch(View v, MotionEvent event) {
- testPoint(event);
- return true;
- }
-
- private void testPoint(MotionEvent event) {
- if (!debugOn) {
- return;
- }
- ArrayList<Float> mXs = null;
- ArrayList<Float> mYs = null;
- if (mXs == null && mYs == null) {
- mXs = new ArrayList<Float>();
- mYs = new ArrayList<Float>();
- }
- mXs.clear();
- mYs.clear();
-
- final int N = event.getPointerCount();
- float x, y;
- for (int i = 0; i < N; i++) {
- x = event.getX(event.getPointerId(i));
- y = event.getY(event.getPointerId(i));
- logd("x[" +i+ "],y[" +i+ "] = " + x + "," + y);
- mXs.add(x);
- mYs.add(y);
- }
- if (N > 0) mPointView.setPoints(mXs, mYs);
- }
-
- //調試
- private final boolean debugOn = true;
- private final String TAG = "MyListener";
- private int logd(String msg) {
- int retVal = 0;
- if (debugOn) {
- retVal = Log.i(TAG, msg);
- }
- return retVal;
- }
- }
- }
PointView.java
- package com.ckl.TestPoint;
-
- import java.util.ArrayList;
-
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.util.Log;
- import android.widget.ImageView;
-
- public class PointView extends ImageView {
-
- public PointView(Context context) {
- super(context);
- }
-
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- drawTouchPiont(canvas);
- }
-
- //繪制觸摸點
- ArrayList<Float> mXs = null, mYs = null;
- private boolean mDrawn = true;
- private Paint mPointPaint = null;
- private Paint mRectPaint = null;
- private Paint mTextPaint = null;
- public void setPoints(ArrayList<Float> mXs, ArrayList<Float> mYs) {
- if (debugOn) {
- if (mPointPaint == null) {
- mPointPaint = new Paint();
- mPointPaint.setAntiAlias(false);
- mPointPaint.setARGB(255, 0, 96, 255);
-
- mRectPaint = new Paint();
- mRectPaint.setARGB(0x88, 0x44, 0x44, 0x44);
-
- mTextPaint = new Paint();
- mTextPaint.setTextSize(15);
- mTextPaint.setARGB(0xff, 0xff, 0xff, 0xff);
-
- logd("init Paint");
- }
- this.mXs = mXs;
- this.mYs = mYs;
- mDrawn = false;
- invalidate();
- }
- }
-
- private void drawTouchPiont(Canvas canvas) {
- if (debugOn) {
- if (!mDrawn) {
- float x,y, rx, ry;
- float dx = 80, dy = 80;
- for (int i = 0; i < mXs.size(); i++) {
- x = mXs.get(i);
- y = mYs.get(i);
- canvas.drawLine(x, y - dy, x, y + dy, mPointPaint);
- canvas.drawLine(x - dx, y, x + dx, y, mPointPaint);
-
- rx = x;
- ry = y - 40;
- if (x + 75 > getRight())
- rx = x - 76;
- if (ry < getTop())
- ry = y + 20;
- canvas.drawRect(rx, ry, rx + 75, ry + 20, mRectPaint);
- canvas.drawText("" + (int)x + "," + (int)y, rx, ry + 15, mTextPaint);
- }
- mDrawn = true;
- }
- }
- }
-
- //調試
- private final boolean debugOn = true;
- private final String TAG = "PointView";
- private int logd(String msg) {
- int retVal = 0;
- if (debugOn) {
- retVal = Log.i(TAG, msg);
- }
- return retVal;
- }
- }
Android上實現直接在屏幕上顯示點擊位置源代碼下載地址:
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /pub/Android源碼集錦/2011年/10月/Android上實現直接在屏幕上顯示點擊位置源代碼/