【问题标题】:Possible to do rounded corners in custom Progressbar progressDrawable?可以在自定义 Progressbar progressDrawable 中做圆角吗?
【发布时间】:2011-04-14 21:03:14
【问题描述】:

我有一个进度条,应该看起来像所附图片:

我已经走了很长一段路。我非常接近唯一不起作用的部分是progressDrawable的圆角。这是我的样子。 (注意,红圈,白色轮廓内的填充没有圆角):

所以,当进度条用形状、渐变或颜色着色时,我找到了几种方法来完成这项工作。但是,我无法将图像作为progressDrawable。

这是我扩展 ProgressBar 的类

public class RoundedProgressBar extends ProgressBar{
private Paint paint;

public RoundedProgressBar(Context context) {
    super(context);
    setup();
}

public RoundedProgressBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    setup();
}

public RoundedProgressBar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setup();    ;
}

protected void setup()
{
    paint = new Paint();
}

@Override
protected synchronized void onDraw(Canvas canvas) {
    // First draw the regular progress bar, then custom draw our text
    super.onDraw(canvas);
    paint.setColor(Color.WHITE);
    paint.setStyle(Paint.Style.STROKE);
    RectF r = new RectF(0,0,getWidth()-1,getHeight()-1);
    canvas.drawRoundRect(r,getHeight()/2,getHeight()/2, paint);
}
 }

这是我的选择器:

<layer-list
 xmlns:android="http://schemas.android.com/apk/res/android"
 >
 <item
 android:id="@android:id/background"
 android:drawable="@drawable/slider_track" />
 <item
 android:id="@android:id/secondaryProgress"
 android:drawable="@drawable/slider_track" />
 <item
 android:id="@android:id/progress"
 android:drawable="@drawable/slider_track_progress" />
 </layer-list>

以下是选择器中使用的图像:

slider_track->
slider_track_progress->

这是我在我的活动布局中嵌入进度条的地方

<com.android.component.RoundedProgressBar
    android:id="@+id/player_hp_bar"
    android:layout_width="fill_parent"
    android:layout_height="36dip"
    android:layout_marginLeft="30dip"
    android:layout_marginRight="30dip"
    android:max="100"
    style="?android:attr/progressBarStyleHorizontal"
    android:progressDrawable="@drawable/slider_layer_list"
    android:progress="20"
    android:maxHeight="12dip"
    android:minHeight="12dip"
/>

有人知道怎么做吗?

【问题讨论】:

标签: android layout progress-bar rounded-corners skinning


【解决方案1】:

不确定您是否真的可以通过 API 修整边缘,但您可以很容易地在白色轮廓和实际进度对象之间添加一个新层?该图层可以是白色轮廓的精确剪切,因此进度条不会显示在轮廓之外。

【讨论】:

  • 感谢您的快速反馈!你所说的“层”是什么意思?如何遮罩/剪辑progressDrawable?
  • 我想我知道你在说什么,但我认为这种方法行不通,因为我将把它放在各种背景下。所以我需要一些东西来实际剪辑/掩盖progressDrawable
  • @b-ryce 我只是说我已经读过elsewhere,由于某种原因,不可能使用标准设置实际舍入它们。最好的办法是继续尝试自己剪裁边缘,我目前正在进行自己的项目,但如果我有时间,我会四处摆弄,看看我能想出什么。如果您反过来找到解决方案,请务必回帖,因为这绝对是我有兴趣知道的事情,我相信其他人也会有兴趣。
【解决方案2】:

试试这个,你可以通过在xml中修改拇指和seekbar的颜色和宽高来做你想做的事。

you can see the image here

这是我能做到这一点的唯一方法。 希望对您有所帮助

这是我的搜索栏:

         <SeekBar
            android:id="@+id/simpleProgressBar"
            android:layout_width="fill_parent"
            android:layout_height="12dp"
            android:max="100"
            android:progress="0"
            android:background="@null"
            android:shape="oval"
            android:splitTrack="false"
            android:progressDrawable="@drawable/progress_background"
            android:secondaryProgress="0"
            android:thumbOffset="10dp"
            android:thumb="@drawable/oval_seekbar_thumb" />

这是(progress_background.xml):

<item android:id="@android:id/background">
    <shape android:shape="rectangle">
        <corners android:radius="5dp" />

        <gradient
            android:angle="270"
            android:endColor="@color/grey_line"
            android:startColor="@color/grey_line" />
    </shape>
</item>
<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape android:shape="oval">
            <corners android:radius="5dp" />
            <gradient
                android:angle="270"
                android:endColor="@color/blue"
                android:startColor="@color/blue" />
        </shape>
    </clip>
</item>
<item android:id="@android:id/progress">
    <clip>
        <shape android:shape="rectangle">
            <corners android:radius="90dp" />

            <gradient
                android:angle="90"
                android:endColor="@color/colorPrimaryDark"
                android:startColor="@color/colorPrimary" />
        </shape>
    </clip>
</item>

这是拇指(oval_seekbar_thumb.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <solid android:color="#ffffff" />
            <stroke android:color="@color/colorPrimaryDark" android:width="3dp"/>
            <size android:height="12dp" android:width="12dp"/>
        </shape>
    </item>
</selector>

拇指的高度应与搜索栏的高度相同才能正常查看。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多