添加视图到布局imageview onclick后无法正常工作

时间:2013-08-12 07:24:36

标签: android android-emulator

这里我在i 2图像视图中有一个布局,然后将myview添加到布局我的imagview onclicks运行良好,如果假设我将myview添加到主布局意味着只绘制工作..但我的imgview点击不工作o想要两个绘图,图片点击功能在下面的一个建议是我的代码..

 public class B extends Activity implements OnTouchListener {
RelativeLayout.LayoutParams lp6;
  int x1, y1, x2, y2;
   LineDrawerView   myview;
 RelativeLayout layout;
 ImageView i1,i2;
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
   i1=(ImageView)findViewById(R.id.image1);
 i1.setOnTouchListener(this);
 i2=(ImageView)findViewById(R.id.image2);
 i2.setOnTouchListener(this);
   layout=(RelativeLayout)findViewById(R.id.layoutmiddle);

           myview = new LineDrawerView(this);
            myview.setId(004);
             lp6 = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
             layout.addView(myview, lp6);

  }


public class LineDrawerView extends View
{
    public LineDrawerView(Context context) {
        super(context);
    }   



    ArrayList<Rect> lines= new ArrayList<Rect>();

    boolean drawing=false;

    protected void onDraw(Canvas canvas) 
    {
        // TODO Auto-generated method stub
        super.onDraw(canvas);



        Paint p= new Paint();
        p.setColor(Color.BLACK);

        if (drawing)
        {
            canvas.drawLine(x1, y1, x2, y2, p);


        }
        for (int i=0; i<lines.size(); i++)
       {
           Rect currline= lines.get(i);
           canvas.drawLine(currline.left, currline.top, currline.right, currline.bottom, p);
       }
    }

    public boolean onTouchEvent(MotionEvent event) 
    {
        boolean result=false;
        switch (event.getAction()) 
        {
            case MotionEvent.ACTION_DOWN:
                x1=x2= (int)event.getX();
                y1=y2= (int)event.getY();

                drawing=true;
                result=true;
                Log.i("start x1------", ""+x1);
                Log.i("start y1------", ""+y1);



                break;
            case MotionEvent.ACTION_MOVE:
                x2= (int)event.getX();
                y2= (int)event.getY();
                result=true;
                break;
            case MotionEvent.ACTION_UP:
                x2= (int)event.getX();
                y2= (int)event.getY();

                Log.i("end x2------", ""+x2);
                Log.i("end y2------", ""+y2);

                if(x1 > 300 && x1 < 400 && x2 >700 && x2 < 800 && y1 >100 && y1 <200 && y2 >100 && y2 <200)
                {
                      drawing=true;

                      x1=406;
                      x2=621;
                      y1=125;
                      y2=125;
                      lines.add(new Rect(x1, y1, x2, y2));
                }
                else  if(x1 > 300 && x1 < 400 && x2 >700 && x2 < 800 && y1 >200 && y1 <300 && y2 >200 && y2 <300)
                {
                    drawing=true;
                      x1=406;
                      x2=621;
                      y1=226;
                      y2=226;
                      lines.add(new Rect(x1, y1, x2, y2));
                }
                else
                {
                     drawing=false;
                }


                result=true;
                break;
        }

        if (result) invalidate();
        return result;
    }


}


      @Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub


      if(v==i1)
      {
      Toast.makeText(B.this, ""+"i am i1", Toast.LENGTH_LONG).show();
      }
      else if(v==i2)
          {
          Toast.makeText(B.this, ""+"i am i2", Toast.LENGTH_LONG).show();
          }

    return false;
}
  }

main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layoutmiddle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >

<ImageView
    android:id="@+id/image1"
    android:layout_marginLeft="300dp"
      android:layout_marginTop="100dp"
    android:layout_width="wrap_content"
    android:background="@drawable/apple_50"
    android:layout_height="wrap_content"/>

  <ImageView
      android:id="@+id/image2"
      android:layout_width="15dp"
      android:layout_height="15dp"
        android:layout_marginLeft="10dp"
      android:layout_marginTop="120dp"
     android:background="@drawable/checkbox_empty" />

1 个答案:

答案 0 :(得分:1)

onTouch上,您有两条if(v==i1)行。将else if(v==i1)更改为else if(v==i2)。看起来像是一个复制/粘贴错误。