[英]How to change the size (width and height) of progressbar to fit all in linearlayout?
[英]Change the size of a customized ProgressBar
我創建了一個CustomBar,它擴展了ProgressBar,它具有多種自定義外觀的方法。 此功能之一將圖標添加到進度部分。 因為它比ProgressBar大,所以它被裁剪了,這是我不希望的。 因此,我嘗試在添加圖標時更改ProgressBar的大小。
像這樣:
ViewGroup.LayoutParams params = getLayoutParams();
params.height = bitmap.getHeight();
setLayoutParams(params);
requestLayout();
這不能解決問題,因此我嘗試更改Drawable的大小。 這是一個LayerDrawable,我嘗試過setBounds,setLayerInsets,將第一個Layer更改為帶有填充的InsetDrawable以獲取所需的大小。 一切都無濟於事。
那我想念什么呢? 如何在運行時正確更改progressBar的大小?
我自己找到了答案,您必須覆蓋范圍,而且也適用於畫布。
@Override
protected synchronized void onDraw(Canvas canvas)
{
if(bitmap != null && bitmap.getHeight() > getHeight())
{
canvas.clipRect(0, 0, getWidth(), bitmap.getHeight(), Region.Op.REPLACE);
bounds = getProgressDrawable().getBounds();
bounds.bottom = bitmap.getHeight();
}
doSomething();
}
重要的是Region.Op.REPLACE標志,通常剪切僅與Canvas的Bounds相交。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.