我對CustomView有問題。我有一個佈局,在屏幕頂部有EditText字段。底部有兩個按鈕。屏幕中間的剩餘空間被ImageView佔用。在ImageView上,我應該可以繪製一個矩形。我在我的佈局中保留了一個CustomView,它也佔用了與ImageView相同的寬度和高度。但我的問題是畫布佔據了整個屏幕的寬度和高度。所以當用戶在我的圖像上畫一個矩形時,它隱藏在我的EditText字段下面。我想限制我的畫布尺寸與ImageSize的相同,並且不要隱藏。設置自定義視圖的畫布寬度和高度Android
我檢查了一些來源,但它沒有幫助我。
<?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;
}
}
請幫助我。由於
你可以發佈你的活動課嗎? – krodmannix
在我的活動中,我只是初始化了我的觀點。 – Lavanya
您是否嘗試過我的解決方案來爲畫布設置寬度和高度? –