概述
项目需求原因需要给触摸屏做一个精准度的测试程序,大概要求就是屏幕上布局好多个相同的方格,点一下方格,颜色就改变了。刚开始我是用girdview做的,感觉怪怪的,后来还是画出来的方格,demo代码如下:
package xxxx;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class TouchPadView extends View {
private final String TAG = "TouchPadView";
private Activity mActivity;
/**
* screen's height
*/
private int mScreenHeight;
/**
* screen's width
*/
private int mScreenWidth;
/**
* lattice's height
*/
private final int mGridHeight = 40;
/**
* lattice's width
*/
private final int mGridWidth = 40;
/**
* Each Row can has the number of lattice
*/
private int mRowCount;
/**
* Each column can has the number of lattice
*/
private int mColCount;
/**
* A total number of the lattice
*/
private int mRectCount;
/**
* rect's and isTouch's index
*/
private int mIndex = 0;
private Rect[] rect;
/**
* Record whether the lattice is touched
*/
private boolean[] isTouch;
private int[] coordinateX;
private int[] coordinateY;
public TouchPadView(Activity activity) {
super(activity);
this.mActivity = activity;
init();
}
private void init() {
getScreenHeightAndWidth();
mRowCount = mScreenWidth / mGridWidth + 1;
mColCount = mScreenHeight / mGridHeight + 1;
Log.v(TAG, "mRowCount = " + mRowCount);
Log.v(TAG, "mColCount = " + mColCount);
mRectCount = (mRowCount - 1) * (mColCount - 1);
Log.v(TAG, "mRectCount = " + mRectCount);
coordinateX = new int[mRowCount];
coordinateY = new int[mColCount];
rect = new Rect[mRectCount];
isTouch = new boolean[mRectCount];
/**
* Initialize All lattice have not been touched
*/
for(int i = 0; i < mRectCount; i++){
isTouch[i] = false;
}
/**
* Initialize All lattice's upper left corner X-coordinate
*/
for (int i = 0; i < mRowCount; i++) {
coordinateX[i] = i * mGridWidth;
Log.v(TAG, "coordinateX[" + i + "] = " + coordinateX[i]);
}
/**
* Initialize All lattice's upper left corner Y-coordinate
*/
for (int i = 0; i < mColCount; i++) {
coordinateY[i] = i * mGridHeight;
Log.v(TAG, "coordinateY[" + i + "] = " + coordinateY[i]);
}
for (int i = 0; i < mRowCount - 1; i++) {
for (int j = 0; j < mColCount - 1; j++) {
rect[mIndex++] = new Rect(coordinateX[i], coordinateY[j],
coordinateX[i + 1], coordinateY[j + 1]);
Log.v(TAG, "coordinateX[" + i + "] = " + coordinateX[i]);
Log.v(TAG, "coordinateY[" + j + "] = " + coordinateY[j]);
Log.v(TAG, "coordinateX[" + (i+1) + "] = " + coordinateX[i+1]);
Log.v(TAG, "coordinateY[" + (j+1) + "] = " + coordinateY[j+1]);
}
}
}
/**
* Get the screen's height and Width
*/
private void getScreenHeightAndWidth() {
DisplayMetrics displayMestrics = new DisplayMetrics();
mActivity.getWindowManager().getDefaultDisplay().getMetrics(
displayMestrics);
mScreenHeight = displayMestrics.heightPixels;
mScreenWidth = displayMestrics.widthPixels;
Log.v(TAG, "mScreenHeight = " + mScreenHeight);
Log.v(TAG, "mScreenWidth = " + mScreenWidth);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.v(TAG, "onDraw");
canvas.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
for (int i = 0; i < mRectCount; i++){
//Log.v(TAG, "index="+i);
canvas.drawRect(rect[i], paint);
}
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
for (int i = 0; i < mRectCount; i++){
if(isTouch[i] == true){
canvas.drawRect(rect[i], paint);
}
}
}
/**
* Judge all the grid Whether have been touched
*
*/
private boolean allRectChange(){
boolean isAllRectChange = false;
int index = 0;
for(int i = 0; i < mRectCount; i++){
index = i;
if(isTouch[i]){
continue;
}
else{
break;
}
}
Log.v(TAG,"index = "+index);
if(index + 1 == mRectCount){
isAllRectChange = true;
Log.v(TAG,"isAllRectChange = true");
}
return isAllRectChange;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(allRectChange()){
mActivity.finish();
}
int touchX = (int)event.getX();
int touchY = (int)event.getY();
if(touchX == mScreenWidth){
touchX = touchX -1;
}
if(touchY == mScreenHeight){
touchY = touchY -1;
}
Log.v(TAG, "touchX = "+touchX);
Log.v(TAG, "touchY = "+touchY);
touchWhere(touchX, touchY);
invalidate();
return true;
}
private void touchWhere(int touchX, int touchY){
int indexX = touchX / mGridWidth;
int indexY = touchY / mGridHeight;
Log.v(TAG, "indexX = "+indexX);
Log.v(TAG, "indexY = "+indexY);
isTouch[indexX * (mColCount - 1) + indexY] = true;
Log.v(TAG, indexX * (mColCount - 1) + indexY+"");
}
}
效果图如下:
代码下载地址:http://download.csdn.net/detail/sanjinxiong/4043388
最后
以上就是虚心眼神为你收集整理的触摸屏测试程序的全部内容,希望文章能够帮你解决触摸屏测试程序所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复