2014-06-10 182 views
0

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

}

请帮助我。由于

+0

你可以发布你的活动课吗? – krodmannix

+0

在我的活动中,我只是初始化了我的观点。 – Lavanya

+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的边界。

+0

这不适合我。向左和向右罚款。但是它的顶部和底部仍然会低于我的顶部布局和底部布局 – Lavanya

+0

这种修复方法是左或右,还是已经工作? – krodmannix

+0

它已经在工作。这个答案目前还没有解决我的问题 – Lavanya

相关问题