2016-07-01 6 views
0

Ich baue App wie Match die gleichen Dinge in android.Ich möchte gerade Linie zwischen zwei Bild-Taste, wenn sie gleiche auf Benutzer Finger Touch sind. Ich versuchte viele Lösung, aber niemand arbeitet nicht. Hilf mir dabei.zeichne gerade Linie zwischen zwei imagebutton Anzeige in einer Aktivität auf finger berühren in android

Hier ist mein Code

MainActivity.java

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

<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> 

ich auch Probe meiner Layout liefern Es ist wie es aussehen wird .. Bilder sind nur unterschiedlich ..

enter image description here

Ich möchte Linien zeichnen, wo zwei Bilder gleich sind.Es kann sein, Cross-Linie aber es sollte gerade sein.

+1

Mögliche Duplikate von [Wie zeichne ich eine Linie zwischen zwei ImageButtons in Android?] (Http://stackoverflow.com/questions/23925847/How-Do-ich-zeichne-eine-Linie-zwischen-zwei-Bild-Buttons-in-android) –

Antwort

0

Verwendung benutzerdefinierte Raster anzeigen, nach der Anzahl der Spaltenauswahl 2, fügen Marge 5dp, ich hoffe, dies wird Ihnen helfen ..

Suche nach benutzerdefinierten Rasteransicht mit Bild auf Google, die Sie erhalten Beispiel ..

Ich denke, hier brauchen Sie nicht Tabellenlayout und Tabellenzeile ich denke ..

+0

Ich möchte nicht zeichnen.Ich möchte die Bild-Schaltflächen wie Mathe die gleichen Alphabete verbinden. – KrishnaJ