0

在这里,在将 myview 添加到布局之前,我在 2 个 imageviews 中有一个布局,我的 imagview onclicks 运行良好,如果假设我将 myview 添加到主布局意味着仅绘制工作..但是我的 imgview on click 不工作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;
}
  }

主要的.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" />

4

1 回答 1

1

onTouch有两条if(v==i1)线。更改else if(v==i1)else if(v==i2)。看起来像复制/粘贴错误。

于 2013-08-12T07:28:14.723 回答