首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在android中的手指触摸活动中在两个图像按钮之间画直线。

在android中的手指触摸活动中在两个图像按钮之间画直线。
EN

Stack Overflow用户
提问于 2016-07-01 10:36:49
回答 1查看 97关注 0票数 0

我正在建立应用程序,像匹配相同的东西在机器人,我想画直线之间的两个图像按钮,如果他们是相同的用户手指触摸。我尝试了许多解决方案,但没有人是working.Help我通过它。

这是我的密码

MainActivity.java

代码语言:javascript
复制
public TableLayout t1;
MotionEvent simulationEvent;
public TableRow tr1;
public TableRow tr2;
public TableRow tr3;
public ImageButton i11;
public ImageButton i21;
public ImageButton i12;
public ImageButton i22;
public ImageButton i31;
public ImageButton i32;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    t1 = (TableLayout) findViewById(R.id.tableLayout1);
    tr1 = (TableRow) findViewById(R.id.tableRow1);
    tr2 = (TableRow) findViewById(R.id.tableRow2);
    tr3 = (TableRow) findViewById(R.id.tableRow3);
    i11 = (ImageButton) findViewById(R.id.ibACol1);
    i12 = (ImageButton) findViewById(R.id.ibCCol3);
    i21 = (ImageButton) findViewById(R.id.ibBCol1);
    i22 = (ImageButton) findViewById(R.id.ibACol3);
    i31 = (ImageButton) findViewById(R.id.ibCCol1);
    i32 = (ImageButton) findViewById(R.id.ibBCol3);

    RelativeLayout rl = (RelativeLayout) findViewById(R.id.RelativeLayout1);
    DrawPanel drawingPanel = new DrawPanel(getApplicationContext());
    rl.addView(drawingPanel);
    rl.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
               if (event == simulationEvent)
                    return false;
                int action = event.getAction();
                int x = (int)event.getX();
                int y = (int)event.getY();
                Log.e("onTouchListener", "User touch at X:" + x + " Y:" + y);
            return true;
        }
    });

}
public class DrawPanel extends View {
    private Paint paint;
    private Paint paint1;
    private Paint paint2;
    // Canvas c;
    MainActivity m1 = new MainActivity();
    int count = 0;
    int ans = 0, ansPrev;
    int temp1, temp2;
    int color = 0;
    String str = "";
    public boolean tmp;
    @SuppressWarnings("rawtypes")
    private ArrayList points;

    @SuppressWarnings("rawtypes")
    private ArrayList strokes;

    @SuppressWarnings("rawtypes")
    public DrawPanel(Context context) {
        super(context);

        points = new ArrayList();
        // points1 = new ArrayList();
        // points2 = new ArrayList();
        strokes = new ArrayList();
        paint = createPaint(Color.BLUE, 11);
        paint1 = createPaint(Color.GREEN, 11);
        paint2 = createPaint(Color.RED, 11);

    }

    @SuppressWarnings("rawtypes")
    public void onDraw(Canvas c) {
        super.onDraw(c);

        // this.setBackgroundColor(Color.WHITE);
        for (Object obj : strokes) {
            drawStroke((ArrayList) obj, c, color);
        }

        drawStroke(points, c, color);
        color = 0;

    }

    @SuppressLint("NewApi")
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
            points.add(new Point((int) event.getX(), (int) event.getY()));
            color = getColor();
            invalidate();
        }

        if (event.getActionMasked() == MotionEvent.ACTION_UP) {

            try {
                ansPrev = ans;
                for (int r = 0; r < t1.getChildCount(); r++) {
                    int y = (int) t1.getChildAt(r).getY();
                    if ((int) event.getY() >= y
                            && (int) event.getY() < (y + t1.getChildAt(r)
                                    .getHeight())) {
                        // View v = t1.getChildAt(r);
                        for (int i = 0; i < ((TableRow) t1.getChildAt(r))
                                .getChildCount(); i += 2) {
                            int x1 = (int) tr1.getChildAt(i).getX();
                            if ((int) event.getX() >= x1
                                    && (int) event.getX() < (x1 + tr1
                                            .getChildAt(i).getWidth())) {
                                // Toast.makeText(getApplicationContext(),
                                // "Row "+(r+1)+", Column "+(i+1)+"Selected.",
                                // Toast.LENGTH_SHORT).show();
                                if (count == 1) {
                                    // str =
                                    // (String)((ImageButton)((TableRow)t1.getChildAt(r)).getChildAt(i)).getContentDescription();

                                    int len1 = (int) ((ImageButton) ((TableRow) t1
                                            .getChildAt(temp1))
                                            .getChildAt(temp2))
                                            .getContentDescription()
                                            .length();
                                    int len2 = (int) ((ImageButton) ((TableRow) t1
                                            .getChildAt(r)).getChildAt(i))
                                            .getContentDescription()
                                            .length();
                                    String str1 = (String) ((ImageButton) ((TableRow) t1
                                            .getChildAt(r)).getChildAt(i))
                                            .getContentDescription()
                                            .subSequence(0, 1);
                                    if (str1.equals(str) && len1 != len2) {
                                        ans = ans + 1;
                                        tmp = true;
                                        color = 1;
                                        // drawStroke(points, c, color);
                                        ((ImageButton) ((TableRow) t1
                                                .getChildAt(temp1))
                                                .getChildAt(temp2))
                                                .setSelected(false);
                                        Toast.makeText(
                                                getApplicationContext(),
                                                "Answer Matched. \n Your score is: "
                                                        + ans,
                                                Toast.LENGTH_SHORT).show();
                                        count = 0;

                                    } else {
                                        ans = ans - 1;
                                        tmp = false;
                                        color = 2;
                                        // drawStroke(points, c, color);
                                        ((ImageButton) ((TableRow) t1
                                                .getChildAt(temp1))
                                                .getChildAt(temp2))
                                                .setSelected(false);
                                        Toast.makeText(
                                                getApplicationContext(),
                                                "Answer not Matched. \n Your score is: "
                                                        + ans,
                                                Toast.LENGTH_SHORT).show();
                                        count = 0;

                                    }

                                    // Toast.makeText(getApplicationContext(),
                                    // "Image "+(i+r+1)+" : "+str+((ImageButton)((TableRow)t1.getChildAt(r)).getChildAt(i)).isSelected(),
                                    // Toast.LENGTH_SHORT).show();
                                    // count=0;

                                }
                            }
                        }
                    }
                    // Toast.makeText(getApplicationContext(),
                    // "Color: "+color, Toast.LENGTH_SHORT).show();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            // points2.clear();
            // points = new ArrayList();
            // invalidate();
            this.strokes.add(points);
            points = new ArrayList();
            invalidate();
        }

        return true;
    }

    public int getColor() {
        return color;
    }

    private void drawStroke(@SuppressWarnings("rawtypes") ArrayList stroke,
            Canvas c, int i1) {

        if (stroke.size() > 0 && i1 == 0) {
            Point p0 = (Point) stroke.get(0);
            for (int i = 1; i < stroke.size(); i++) {
                Point p1 = (Point) stroke.get(i);
                c.drawLine(p0.x, p0.y, p1.x, p1.y, paint);
                p0 = p1;
            }
        } else if (stroke.size() > 0 && i1 == 1) {
            Point p0 = (Point) stroke.get(0);
            for (int i = 1; i < stroke.size(); i++) {
                Point p1 = (Point) stroke.get(i);
                c.drawLine(p0.x, p0.y, p1.x, p1.y, paint1);
                p0 = p1;
            }
        } else if (stroke.size() > 0 && i1 == 2) {
            Point p0 = (Point) stroke.get(0);
            for (int i = 1; i < stroke.size(); i++) {
                Point p1 = (Point) stroke.get(i);
                c.drawLine(p0.x, p0.y, p1.x, p1.y, paint2);
                p0 = p1;
            }
        }

    }

    private Paint createPaint(int color, float width) {
        Paint temp = new Paint();
        temp.setStyle(Paint.Style.STROKE);
        temp.setAntiAlias(true);
        temp.setColor(color);
        temp.setStrokeWidth(width);
        temp.setStrokeCap(Cap.ROUND);

        return temp;
    }

}

activity_main.xml

代码语言:javascript
复制
<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="20dp" >

        <ImageButton
            android:id="@+id/ibACol1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="" />

        <ImageButton
            android:id="@+id/ibCCol3"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/ic_launcher" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="20dp" >

        <ImageButton
            android:id="@+id/ibBCol1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="" />

        <ImageButton
            android:id="@+id/ibACol3"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/ic_launcher" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="20dp" >

        <ImageButton
            android:id="@+id/ibCCol1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="" />

        <ImageButton
            android:id="@+id/ibBCol3"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/ic_launcher" />
    </TableRow>
</TableLayout>

我还提供了布局的示例,看起来that..Images只是不同的。

在这里输入图像描述

我想画两幅图像为same.It的线,可能是交叉线,但应该是直线。

EN

回答 1

Stack Overflow用户

发布于 2016-07-01 10:51:45

使用自定义网格视图,通过选择第2列的编号,添加边距5dp,希望这将对您有所帮助..

在google上搜索带有图像的自定义网格视图,您将得到示例。

我想在这里你不需要表格布局和桌子排。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38143252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档