无法在线性布局中添加自定义视图-Android

无法在线性布局中添加自定义视图-Android,android,android-linearlayout,custom-view,Android,Android Linearlayout,Custom View,我在线性布局中多次添加myCustomView,对于customView的第一个实例来说,它工作正常,但无法多次添加 以下是自定义视图类: public class MultiTouchView extends View { private float x, y; Bitmap image; public MultiTouchView(Context context, Bitmap image) { super(context);

我在线性布局中多次添加
myCustomView
,对于customView的第一个实例来说,它工作正常,但无法多次添加

以下是自定义视图类:

public class MultiTouchView extends View {

      private float x, y;
      Bitmap image;

      public MultiTouchView(Context context, Bitmap image) {
       super(context);
       this.image = image;

       // TODO Auto-generated constructor stub
      }

      public MultiTouchView(Context context, AttributeSet attrs) {
            super(context, attrs);
          }

          public MultiTouchView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
          }

      @Override
      protected void onDraw(Canvas canvas) {
       // TODO Auto-generated method stub       
        canvas.drawBitmap(image, x, y, null);
      }

      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
       // TODO Auto-generated method stub

          super.onMeasure(widthMeasureSpec, heightMeasureSpec);
          setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
             MeasureSpec.getSize(heightMeasureSpec));   
      }

      @Override
      public boolean onTouchEvent(MotionEvent event) {
       // TODO Auto-generated method stub

       int action = event.getAction();
       switch(action){
       case MotionEvent.ACTION_MOVE:
        x = event.getX();
        y = event.getY();

        break;
       case MotionEvent.ACTION_DOWN:

        x = event.getX();
        y = event.getY();

        break;
       case MotionEvent.ACTION_UP:
        break;

       }

       invalidate();
       return true;
      } 
     }
这是我正在添加自定义视图的类:

public class AndroidTouch extends Activity {

    LinearLayout linear;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        linear = (LinearLayout)findViewById(R.id.linear);

          Bitmap backgroundCard = BitmapFactory.decodeResource(
                    getResources(), R.drawable.ic_launcher);

        MultiTouchView mt1 = new MultiTouchView(this, backgroundCard);
        linear.addView(mt1);

         Bitmap backgroundCard2 = BitmapFactory.decodeResource(
        getResources(), R.drawable.icon);
         MultiTouchView mt2 = new MultiTouchView(this, backgroundCard2);
         linear.addView(mt2);
    }
}

如果为多点触控视图设置背景色,则会发现整个屏幕的颜色与添加的第二个视图的颜色相同

我为子视图指定了布局参数,并且能够在屏幕上绘制视图。修改后的代码:

public class AndroidTouch extends Activity {

    LinearLayout linear;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_c);

        linear = (LinearLayout)findViewById(R.id.main);

        Bitmap backgroundCard = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
        int w = backgroundCard.getWidth();
        int h = backgroundCard.getHeight();
        LayoutParams params = new LayoutParams(w, h);
        MultiTouchView mt1 = new MultiTouchView(getApplicationContext(), backgroundCard);
        //mt1.setBackgroundColor(Color.RED);//try uncommenting this wihtout layout params change
        linear.addView(mt1, params);


        Bitmap backgroundCard2 = BitmapFactory.decodeResource(getResources(), R.drawable.image2);
        params = new LayoutParams(backgroundCard2.getWidth(), backgroundCard2.getHeight());
        MultiTouchView mt2 = new MultiTouchView(getApplicationContext(), backgroundCard2);
        //mt1.setBackgroundColor(Color.BLUE);<-- Entire screen turns blue
        linear.addView(mt2, params);
    }
}
公共类AndroidTouch扩展活动{
线性布局线性;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_c);
线性=(线性布局)findViewById(R.id.main);
位图背景卡=BitmapFactory.decodeResource(getResources(),R.drawable.image1);
int w=backgroundCard.getWidth();
int h=backgroundCard.getHeight();
LayoutParams params=新的LayoutParams(w,h);
MultiTouchView mt1=新的MultiTouchView(getApplicationContext(),背景卡);
//mt1.setBackgroundColor(Color.RED);//尝试取消对此布局参数更改的注释
线性。添加视图(mt1,参数);
位图backgroundCard2=BitmapFactory.decodeResource(getResources(),R.drawable.image2);
params=新布局参数(backgroundCard2.getWidth(),backgroundCard2.getHeight());
MultiTouchView mt2=新的MultiTouchView(getApplicationContext(),backgroundCard2);

//mt1.setBackgroundColor(Color.BLUE);您可能需要在线性
Linearlayout
android:orientation=“vertical”上将方向设置为垂直
。您指定的布局参数是什么?发布包含线性布局的布局xmllayout@Luksprog它是垂直的already@Atrix1987这是xml;是的,对,两个图像都显示了,但由于固定的高度和宽度,它们只能在特定区域内绘制。我必须动态绘制图像,然后必须应用在每个图像上进行快速移动、旋转和缩放,我认为这在单个自定义视图中是不可能的。你能帮我实现这一点吗?