2015-09-23 21 views
0

所以目前我有一個可繪製的對象,我創建它本質上是一個圓,我唯一的問題是,我無法阻止它超過其父容器,並超越我的意思超出其父視圖,以便其邊緣不再可見。Android - 停止繪製背景從超出視圖

我繪製:

public class ProgressArc extends Drawable { 

private int color; 
private float progress; 
private int strokeWidth; 

public ProgressArc(int color, float progress, int strokeWidth) { 

    this.progress = progress * (float)3.6; 
    this.color = color; 
    this.strokeWidth = strokeWidth; 
} 

@Override 
public void draw(Canvas canvas) { 
    canvas.save(); 

    Rect bounds = getBounds(); 
    Paint paint = new Paint(); 
    RectF oval = new RectF(); 

    paint.setColor(this.color); 
    paint.setStrokeCap(Paint.Cap.ROUND); 
    paint.setStyle(Paint.Style.STROKE); 
    paint.setStrokeWidth(strokeWidth); 
    paint.setAntiAlias(true); 

    oval.set(bounds); 
    canvas.drawArc(oval, 90, this.progress, false, paint); 
} 

@Override 
public void setAlpha(int alpha) { 
    // Has no effect 
} 

@Override 
public void setColorFilter(ColorFilter cf) { 
    // Has no effect 
} 

@Override 
public int getOpacity() { 
    // Not Implemented 
    return 0; 
} 

} 

XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/progress_container" 
    android:layout_width="150dp" 
    android:layout_height="150dp"> 

     <View 
      android:id="@+id/bill_progress_partial_bg" 
      android:scaleType="fitXY" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:gravity="center_vertical" /> 

</FrameLayout> 

我如何設置背景:

progressViewPartialArc.setBackground(new ProgressArc(color, getPercent(), 12)); 

截圖我的意思:

screeny

圓應該是一個完美的圓圈,但我只是不能解決它爲什麼不會?

回答

1

看起來你需要採取的筆畫寬度考慮的範圍:

oval.set(bounds); 
oval.inset(strokeWidth / 2, strokeWidth/2); 
+0

傳說,我是想減去實際邊界筆畫寬度,不知道插圖財產。謝謝 –