为自定义视图设置画布宽度和高度Android

时间:2014-06-10 13:56:22

标签: android canvas imageview paint

我的CustomView有问题。我有一个布局,我在屏幕上有EditText字段。底部有两个按钮。屏幕中间的剩余空间被ImageView占用。在ImageView上我应该能够绘制一个矩形。我在我的布局中保留了一个CustomView,它也占用了与ImageView相同的宽度和高度。但我的问题是画布占据了屏幕的整个宽度和高度。因此,当用户在我的图像上绘制一个矩形时,它隐藏在我的EditText字段下面。我想限制我的画布大小与ImageSize相同,而不是隐藏。

我检查了一些消息来源,但它没有帮助我。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white" >

    <RelativeLayout
        android:id="@+id/headerLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:background="@color/header_red" >

        <TextView
            android:id="@+id/tvAddName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp"
            android:text="Enter Name"
            android:textColor="@color/white"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/tvAddDimens"
            android:layout_marginTop="5dp"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:text="Name:"
            android:textColor="@color/white"
            android:textSize="16sp" />
        <EditText
            android:id="@+id/etNameValue"
            android:layout_width="80dp"
            android:layout_height="20dp"
            android:layout_marginLeft="10dp"
            android:layout_below="@id/tvAddDimens"
            android:layout_marginTop="5dp"
            android:layout_toRightOf="@id/tvWidth"
            android:paddingLeft="5dp"
            android:paddingTop="2dp"
            android:singleLine="true"
            android:paddingBottom="2dp"
            android:background="#FFFFFF"
            android:maxLength="8"
            android:textColor="@color/black"
            android:textSize="16sp" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/bottomLayout"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:background="@color/dark_gray" >

        <ImageView
            android:id="@+id/ivDelete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="50dp"
            android:src="@drawable/delete" />

        <ImageView
            android:id="@+id/ivOk"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="50dp"
            android:src="@drawable/tick" />
    </RelativeLayout>

    <RelativeLayout android:id="@+id/imgLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottomLayout"
        android:layout_below="@id/headerLayout"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp" >

        <ImageView
            android:id="@+id/ivCapturedImg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:scaleType="fitXY" />

        <com.ibee.macromedia.utils.DrawingView
            android:id="@+id/drawRect"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp" />
    </RelativeLayout>
</RelativeLayout>

我的DrawingView类是:

public class DrawingView extends View {

/** Need to track this so the dirty region can accommodate the stroke. **/
private static final float STROKE_WIDTH = 5f;

public Paint paint;
/**
 * Optimizes painting by invalidating the smallest possible area.
 */
private int mStartX = 0;
private int mStartY = 0;
private int mEndX = 0;
private int mEndY = 0;
private boolean isDraw=false;
private Context mContext;

public DrawingView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.mContext=context;
    init();

}

public void init(){
    paint=new Paint();
    paint.setAntiAlias(true);
    paint.setColor(mContext.getResources().getColor(R.color.fluor));
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(STROKE_WIDTH);
}

/**
 * Erases the signature.
 */
public void clear() {
    isDraw=true;
    paint=new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.TRANSPARENT);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(STROKE_WIDTH);
    invalidate();
}
@Override
protected void onDraw(Canvas canvas) {

    Log.e("MACROMEDIA", " DRAWING VIEW CANVAS WIDTH " + canvas.getWidth() + " HEIGTH " + canvas.getHeight());
    if(isDraw==true){
        paint = new Paint();
        paint.setColor(Color.TRANSPARENT);
        canvas.drawRect(Math.min(mStartX, mEndX), Math.min(mStartY, mEndY),
                Math.max(mEndX, mStartX), Math.max(mEndY, mStartY), paint);
    } else{
        paint=new Paint();
        paint.setAntiAlias(true);
        paint.setColor(mContext.getResources().getColor(R.color.fluor));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(STROKE_WIDTH);
        canvas.drawRect(Math.min(mStartX, mEndX), Math.min(mStartY, mEndY),
                Math.max(mEndX, mStartX), Math.max(mEndY, mStartY), paint);
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float eventX = event.getX();
    float eventY = event.getY();
            isDraw=false;
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mStartX = (int) event.getX();
        mStartY = (int) event.getY();
        return true;

    case MotionEvent.ACTION_MOVE:
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        if (Math.abs(x - mEndX) > 5 || Math.abs(y - mEndY) > 5) {
            mEndX = x;
            mEndY = y;
            invalidate();
        }
        break;
    case MotionEvent.ACTION_UP:
        break;

    default:
        return false;
    }

    invalidate();

    return true;
}

}

请帮帮我。感谢

1 个答案:

答案 0 :(得分:0)

尝试使用xml:

<com.ibee.macromedia.utils.DrawingView
    android:id="@+id/drawRect"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/ivCapturedImg"
    android:layout_alignBottom="@+id/ivCapturedImg"
    android:layout_alignLeft="@+id/ivCapturedImg"
    android:layout_alignRight="@+id/ivCapturedImg" />

这应该将自定义视图的所有边框与ImageView的边框对齐。

相关问题