【问题标题】:My custom view is being clipped if its position is not (0,0)如果我的自定义视图的位置不是 (0,0)
【发布时间】:2017-04-05 12:54:59
【问题描述】:

我的问题是,如果我将视图的 Y 轴设置为除 0 以外的任何值,则它的绘制区域会从顶部被剪裁。使用日志跟踪我发现视图边界(及其可绘制的边界)始终是正确的位置。我试过clipChildren=false,但这给了我一个奇怪的行为,即绘制区域和视图边界不同步。这是我认为的所有相关代码

//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 属性),视图的逻辑定位都是准确的,但绘制的区域却不是。

【问题讨论】:

    标签: android view


    【解决方案1】:
    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() 中执行。

    【讨论】:

    • 这很有趣,因为我实际上是关于定位相对于父级的,但并没有充分考虑它,哈哈。谢谢你的帮助,汤姆!
    • 不客气,伙计。 :) 如果您对我的回答感到满意,请点击“已回答”。
    • 那么初始化视图 Rect 的合适位置在哪里?
    • 这取决于你想要达到的目标——如果你只是想知道视图的大小来初始化一个矩形,你可以在onSizeChanged() 中得到它。将矩形的topleft 设置为0,将其rightbottom 分别设置为widthheight
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多