自定义视图仅显示在左上角

时间:2015-05-20 13:33:05

标签: android drag-and-drop android-custom-view

我想要做的是为我的活动添加自定义视图,并随意将其拖放到我想要的任何地方。我创建了一个新课程" Square"。当我构建一个新的Square时,它会显示在Display的左上角。当我设置X和Y坐标时,它会消失。如果我将X和Y设置为30,我会看到Square被显示为partiatelly。因此,我的视图似乎只显示在显示屏左上角的一个小框架中 请帮我在显示屏上移动我的自定义视图。谢谢!

MainActivity:

private Button btnNewSquare;
private RelativeLayout relativeLayout;
private Square[] square = new Square[10];
int squareId = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnNewSquare = (Button)findViewById(R.id.button1);
    relativeLayout = (RelativeLayout)findViewById(R.id.RelativeLayout);

    btnNewSquare.setOnClickListener(this);
}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v == btnNewSquare) {
        squareId++;
        square[squareId] = new Square(this);
        square[squareId].setX(30);
                    square[squareId].setY(30);
        square[squareId].setOnTouchListener(square[squareId]);
        relativeLayout.addView(square[squareId]);
        relativeLayout.invalidate();
        Toast.makeText(getApplication(), "Square" + squareId, Toast.LENGTH_SHORT).show();           
        square[squareId].invalidate();          
    }
}

private int squareWidth = 100;
private Paint paint;

public Square(Context context) {
    super(context);
    initSquare();
    // TODO Auto-generated constructor stub
}

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

private final void initSquare() {
    paint = new Paint();
}   

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

   setMeasuredDimension(squareWidth, squareWidth);
}   

@Override
protected void onDraw(Canvas canvas) {    
    //super.onDraw(canvas);
    paint.setStyle(Style.FILL);
    paint.setColor(Color.BLACK);
    paint.setStrokeWidth(3);
    canvas.drawRect(getX(), getY(), getX() + squareWidth, getY() + squareWidth, paint);  
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP) {
        v.setVisibility(View.VISIBLE);
    }
    return false;
}

我的XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/RelativeLayout"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="38dp"
        android:text="New Square" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

答案很简单。 onDraw()方法中的坐标属于视图,而不属于Screen。因此,如果我通过拖放移动我的视图让我们说(500,500)我必须在onDraw方法中绘制我的矩形仍然在(0,0,100,100)而不是在(500,500,600,600),所以正确代码行是:

canvas.drawRect(0,0,squareWidth,squareWidth,paint);

而不是

canvas.drawRect(getX(),getY(),getX()+ squareWidth,getY()+ squareWidth,paint);