2016-11-22 37 views
1

我的自定义视图正在被剪裁我的问题是,如果我将我的视图的Y轴设置为除0之外的任何东西,它的绘制区域将从顶部剪切。使用日志痕迹我发现视图边界(及其可绘制边界)总是正确的位置。我试过clipChildren = false,但是这给了我一个陌生的行为,其中绘制区域和视图边界不同步。这里是所有我认为相关的代码如果我的自定义视图位置不是(0,0)

//onMeasure 
    @Override 
    public void onMeasure(int w, int h){ 
    int width = MeasureSpec.getSize(w); 
    //minHeight is 48dp scaled for density 
    setMeasuredDimension(width, minHeight); 
    } 


//onDraw 
//Note that i've ommited the log statements, however they return the correct 
//coordinates for the view Rect 
    @Override 
    public void onDraw(Canvas c){ 
    c.drawRect(trackRect, tempPaint); 
    } 

//onLayout 
@Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
    trackRect.set(left, top, right, bottom); 
    mTrack.setBounds(trackRect); //irrelevant atm 
} 

//XML CODE 

//BELOW draws a perfect rectangle with a width of match_parent and a height  
//of 48dp 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clipChildren="false" > 


<com.bryanstudios.bryan.clock.LocationSwitch 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

</RelativeLayout> 

//BELOW will cause clipping 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clipChildren="false"> 

<com.bryanstudios.bryan.clock.LocationSwitch 
    android:layout_marginTop="10dp" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

</RelativeLayout> 

//BELOW causes the view to disappear entirely 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clipChildren="false"> 

<com.bryanstudios.bryan.clock.LocationSwitch 
    android:align_parent_bottom="true" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

</RelativeLayout> 

我最好的猜测是我的onMeasure没有做好。还要再次注意,无论我在哪里定位视图(通过w /边距或LayoutParam属性),视图的逻辑定位都是准确的,但绘制的区域不是。

回答

0
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
    trackRect.set(left, top, right, bottom); 
    ... 

这里的left/top/etc参数是相对于父项的。如果您的视图的y位置== 0,那么顶部也会== 0。如果您将视图的Y位置相对于其父位进行更改,则顶部会设置为相同的值。然后,您将这个顶部分配给您绘制的矩形。

public void onDraw(Canvas c){ 
    c.drawRect(trackRect, tempPaint); 
    ... 

这将绘制一个矩形,其中您在onLayout中指定了相同的偏移量。这意味着它将从视图顶部开始绘制trackRect.top

如果您只是想要视图的实际大小,view.getWidth()view.getHeight()可能只是需要的;如果要在视图的宽度和高度已知时初始化某些内容(如路径),请覆盖onSizeChanged()并在其中执行此操作,而不是在onLayout()中执行此操作。

+0

这很有趣,因为我实际上是关于定位是相对于父母,但没有做很多大声笑。谢谢你的帮助,汤姆! – whompum

+0

不客气。 :)如果您对我的答案满意,请点击'回答'。 – Tom

+0

哪里是适当的地方来初始化视图Rect然后呢? – whompum

相关问题