【问题标题】:How to make a simple touch screen test application?如何制作一个简单的触摸屏测试应用程序?
【发布时间】:2015-09-17 16:31:40
【问题描述】:

我想做一个简单的触摸屏测试应用,比如三星设备诊断工具。

来自三星设备诊断工具的截图:http://i.stack.imgur.com/7KgKW.jpg

我对 Android 应用开发不是很熟悉。你会建议我用哪种方式来制作一个像我上面提到的工具这样的简单应用程序?

【问题讨论】:

  • 找到解决办法??

标签: android touch screen


【解决方案1】:

这个应用程序的开发很容易。你需要明白:

如何获取点击坐标。

@Override
public boolean onTouchEvent(MotionEvent event) {
    int x = (int)(event.getX()/tileSize);
    int y = (int)(event.getY()/tileSize);
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        map[x][y] = true;
        break;
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_UP:
    }
return false;
}

重写onDraw方法,绘制矩形。

  private void init(){
  tileSize = 10;

  paint1 = new Paint();
  paint1.setColor(Color.BLUE);
  paint1.setStrokeWidth(10);
  paint1.setStyle(Paint.Style.STROKE);

  paint2 = new Paint();
  paint2.setColor(Color.RED);
  paint2.setStrokeWidth(10);
  paint2.setStyle(Paint.Style.STROKE);

 }

 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
for (int i = 0; i < x; i++){
    for (int j = 0; j < y; j++){
        Paint p = null; 
        if(map[i][j]){
        p=paint1;
        }else{
        p=paint2;
        }
        canvas.drawRect(i*tileSize, j*tileSize, tileSize, tileSize, paint);
    }

}
}
}

【讨论】:

  • 你们能提供完整的解决方案吗?
【解决方案2】:

最近我还不得不在我的应用程序中进行这个屏幕测试。所以我想出了一个解决办法。我正在获取RectF 的数组,即ArrayList&lt;RectF&gt; arr = new ArrayList&lt;&gt;(),并按以下顺序绘制Rects

  1. 左竖线

    private void drawLeftLine(int width, int height) {
    int leftPoint = 0;
    int topPoint = 0;
    int rightPoint = 100;
    int bottomPoint = 100;
    
    int maxNoOfRect = height / 100;
    int lastRectHeight = height % 100;
    
    for (int i = 0; i < maxNoOfRect; i++) {
        arr.add(new RectF(leftPoint, topPoint, rightPoint, bottomPoint));
        topPoint = bottomPoint;
        bottomPoint = bottomPoint + 100;
    }
    

    }

  2. 顶部水平线

    private void drawTopLine(int w, int h) {

    int leftPoint = 0;
    int topPoint = 0;
    int rightPoint = 100;
    int bottomPoint = 100;
    
    int maxNoOfRect = w / 100;
    int lastRectWidth = w % 100;
    
    for (int i = 0; i < maxNoOfRect; i++) {
        arr.add(new RectF(leftPoint, topPoint, rightPoint, bottomPoint));
        leftPoint = rightPoint;
        rightPoint = rightPoint + 100;
    }
    arr.add(new RectF(leftPoint, topPoint, rightPoint + lastRectWidth, bottomPoint));
    

    }

和类似的; 3. 右垂直线 4. 底部水平线,借助大小为 100 的 RectF 集合。

所以,当用户触摸一个点时,检查该坐标是否位于RectF 的任何边界内?如果是,请从数组中删除 RectF。 喜欢:

     @Override
public boolean onTouchEvent(MotionEvent event) {
    float touchX = event.getX();
    float touchY = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (mOnViewTouchedListener != null)
                mOnViewTouchedListener.onViewTouched();
            Point point = new Point();
            point.x = (int) event.getX();
            point.y = (int) event.getY();
            Log.d("TAG", "point: " + point);

            for (int i = 0; i < arr.size(); i++) {
                if (arr.get(i).contains(point.x, point.y)) {
                    Log.d("TAG", "Touch IN");
                    arr.remove(i);
                    invalidate();
                    break;
                }
            }
            break;
        case MotionEvent.ACTION_MOVE:
            if (mOnViewTouchedListener != null)
                mOnViewTouchedListener.onViewTouched();
            Log.d("TAG", "ACTION_MOVE");
            Point point1 = new Point();
            point1.x = (int) event.getX();
            point1.y = (int) event.getY();
            Log.d("TAG", "point: " + point1);

            for (int i = 0; i < arr.size(); i++) {
                if (arr.get(i).contains(point1.x, point1.y)) {
                    Log.d("TAG", "Touch IN");
                    arr.remove(i);
                    invalidate();
                    break;
                }
            }
            break;
        default:
            return false;
    }
    return true;
}

所以,这就是解决方案。对角线也以同样的方式绘制。

从左上角到右下角的对角线:

 private void drawTBDiagonalLine(int w, int h) {

    float height = h;
    float width = w;
    float slope = (height / width);

    float left = 0;
    float top = 0;
    float bottom = 100;
    float right = bottom / slope;

    int maxNoOfRect = h / 100;

    for (int i = 0; i < maxNoOfRect; i++) {
        arr.add(new RectF(left, top, right, bottom));
        left = right;
        top = bottom;
        bottom = bottom + 100;
        right = bottom / slope;
    }
}

【讨论】:

  • 您的解决方案不可用。
  • 你的 git 中没有可用的解决方案
  • 抱歉回复晚了,我的github账号上还没贴出解决方案。
猜你喜欢
  • 2015-09-28
  • 1970-01-01
  • 2014-01-17
  • 1970-01-01
  • 2018-05-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-14
  • 1970-01-01
相关资源
最近更新 更多