Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何以编程方式设置Android SeekBar progress drawable_Android_Seekbar - Fatal编程技术网

如何以编程方式设置Android SeekBar progress drawable

如何以编程方式设置Android SeekBar progress drawable,android,seekbar,Android,Seekbar,我在编程设置SeekBar的可绘制进度时遇到问题。 当我在.xml文件中设置它时,一切正常 <SeekBar android:id="@+id/sb" android:layout_width="fill_parent" android:layout_height="wrap_content" ..... android:progressDrawable="@drawable/seek_bar"/> 背景可绘制然后占据整个搜索栏,我以后根本无法修改进度-拇指移动,但进度可

我在编程设置SeekBar的可绘制进度时遇到问题。 当我在.xml文件中设置它时,一切正常

<SeekBar
 android:id="@+id/sb"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 .....
 android:progressDrawable="@drawable/seek_bar"/>
背景可绘制然后占据整个搜索栏,我以后根本无法修改进度-拇指移动,但进度可绘制仍然填充整个搜索栏。此外,seekbar会松开圆角。似乎进度可绘制在seekbar之上


我尝试了上提供的解决方案,但它对我无效。

我以前在更改代码中的搜索和进度条时遇到问题。有一次,我实际上不得不加载两次牵引装置,才能使其正常生效。我认为这与更改图像后的填充有关

我只是在这里猜测,但是试着在之后用

 getResources().getDrawable(R.drawable.seek_bar) // once
 seekBar.setProgressDrawable(getResources().getDrawable(R.drawable.seek_bar)); //twice
 seekBar.setPadding(int,int,int,int)
也不想让它失效

 seekBar.postInvalidate()

非常粗糙,我不喜欢它,但它为我解决了类似的问题,之前我使用.xml shape作为SeekBar的背景解决了这个问题。 可通过setProgressDrawable()方法使用的完整SeekBar解决方案如下:

seekBar.setProgressDrawable(getResources().getDrawable(R.drawable.seek_bar));
//seek_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@android:id/background"
    android:drawable="@drawable/seek_bar_background"/>
<item android:id="@android:id/progress"
    android:drawable="@drawable/seek_bar_progress" />
</layer-list>

//seek_bar_background.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<gradient
    android:angle="270"
    android:startColor="#8a8c8f"
    android:endColor="#58595b" />

<corners android:radius="5dip" />

</shape>

//seek_bar_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@android:id/background"
    android:drawable="@drawable/seek_bar_background"/>
<item android:id="@android:id/progress">
    <clip android:drawable="@drawable/seek_bar_progress_fill" />
</item>

</layer-list>

//seek_bar_progress_fill.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<gradient
    android:startColor="#b3d27e"       
    android:endColor="#93c044"
    android:angle="270"
 />
 <corners android:radius="5dip" />  

</shape>
progressBar.setProgressDrawable(getResources().getDrawable(R.drawable.seek_bar));

上面给出的答案是关于使用xml的,但为了防止有人想通过编程实现这一点,我在下面给出了答案

public class SeekBarBackgroundDrawable extends Drawable {

private Paint mPaint = new Paint();
private float dy;

public SeekBarBackgroundDrawable(Context ctx) {
    mPaint.setColor(Color.WHITE);
    dy = ctx.getResources().getDimension(R.dimen.one_dp);
}

@Override
public void draw(Canvas canvas) {
    canvas.drawRect(getBounds().left,getBounds().centerY()-dy/2,getBounds().right,getBounds().centerY()+dy/2,mPaint);
}

@Override
public void setAlpha(int i) {
    mPaint.setAlpha(i);
}

@Override
public void setColorFilter(ColorFilter colorFilter) {

}

@Override
public int getOpacity() {
    return PixelFormat.TRANSLUCENT;
}
}

上面的类是seekbar的背景(始终存在于可绘制进度下的部分)

}

以上是可提取的进度。请注意,这是一个剪辑绘制。这里很酷的一点是,我正在将边界设置为我想要的任何颜色。这使得您可以对您的可拉丝面料进行微调定制

//Custom background drawable allows you to draw how you want it to look if needed
    SeekBarBackgroundDrawable backgroundDrawable = new SeekBarBackgroundDrawable(mContext);
    ColorDrawable progressDrawable = new ColorDrawable(Color.BLUE);
    //Custom seek bar progress drawable. Also allows you to modify appearance.
    SeekBarProgressDrawable clipProgressDrawable = new SeekBarProgressDrawable(progressDrawable,Gravity.LEFT,ClipDrawable.HORIZONTAL,mContext);
    Drawable[] drawables = new Drawable[]{backgroundDrawable,clipProgressDrawable};

    //Create layer drawables with android pre-defined ids
    LayerDrawable layerDrawable = new LayerDrawable(drawables);
    layerDrawable.setId(0,android.R.id.background);
    layerDrawable.setId(1,android.R.id.progress);

    //Set to seek bar
    seekBar.setProgressDrawable(layerDrawable);
上面的代码使用自定义绘图来编辑搜索栏。我这样做的主要原因是我将编辑背景可绘制的外观,因此它具有“凹痕”(尽管尚未实现)。使用xml定义的可绘制文件无法做到这一点(至少不容易做到)


我注意到的另一件事是,这一过程防止了SeekBar的宽度达到拇指可拖动的宽度。它设置了可绘制图形的边界,因此它们永远不会变高。

您可以在XML中使用android:maxHeight来限制背景高度

<SeekBar
    android:id="@+id/original_volume"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:maxHeight="2dp"/>


而且它不会夹住拇指

我已经尝试过你的解决方案,但是你能调整它使角变圆吗?更复杂的是它需要在左边取整,但不能在右边取整(除非它是一个完整的进度条),尽管我不知道它实际上是如何工作的。如果你指的是背景绘图,是的,你可以。使用canvas.drawRoundRect绘制左侧,而不是seebackgroundDrawable.draw()中的canvas.drawRect。然后执行另一个canvas.drawRect以绘制与圆形矩形的圆形右侧重叠的右半边。这将为您提供一个矩形,左侧为圆角,右侧为锐边。
<SeekBar
    android:id="@+id/original_volume"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:maxHeight="2dp"/>