簡體   English   中英

為自定義視圖設置Android的畫布寬度和高度

[英]Set Canvas Width and Height Android for Custom View

我在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;
}

}

請幫我。 謝謝

嘗試在您的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的邊框對齊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM