2016-11-22 38 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

相關問題