我正在使用自定义SeekBar显示一张图表。我已经完成了这个部分,现在我是通过将背景Drawable应用于SeekBar来显示这个图表。我的问题是,我需要将蓝色的内容设置为进度Drawable,并将SeekBar的背景设置为红色的图表。这样当进度发生时,拇指移动到红色区域上时,拇指经过的区域应该变成蓝色,就像遮罩效果一样。请问是否有最佳实现方式呢?下面是我的图片:![Secondary progress](https://istack.dev59.com/TWONJ.webp)
![Secondary progress](https://istack.dev59.com/TWONJ.webp)
![Background](https://istack.dev59.com/6VQ9m.webp)
阅读所有问题和答案后,我希望这应该是您完成任务的情况...
1.根据您的逻辑创建两个图表。
2.从特定位图生成两个可绘制对象...
Drawable G_bg = new BitmapDrawable(Red graph bitmap);
Drawable G_pg = new BitmapDrawable(Blue graph bitmap);
3.接下来,通过Java代码创建的图层列表来定制您的进度条。
ClipDrawable c=new ClipDrawable(G_pg, Gravity.LEFT,ClipDrawable.HORIZONTAL);
LayerDrawable ld =new LayerDrawable (new Drawable[]{G_bg,c});
4. 将这个图层列表应用到你的 seekbar 上。
Graphbar.setProgressDrawable(ld);
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@android:id/background"
android:drawable="@drawable/red_graph"/>
<item android:id="@android:id/progress">
<clip android:drawable="@drawable/blue_graph" />
</item>
</layer-list>
在Fragment或Activity布局中:
<com.example.seekbaroverlay.MySeekBar
android:id="@+id/mySeekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100" />
MySeekBar.java:
public class MySeekBar extends SeekBar {
public MySeekBar(Context context) {
this(context, null);
}
public MySeekBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MySeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setProgressDrawable(context.getResources().getDrawable(R.drawable.my_seek_bar_progress));
setThumb(context.getResources().getDrawable(R.drawable.my_thumb));
}
}
对于您的SeekBar,您应该使用自定义的progressDrawable
。请参阅这篇博客文章以获得一个很棒的教程。
您可以创建自定义视图。覆盖其onTouch()
方法以更改拇指的位置。还要覆盖其onDraw()
方法,并首先将红色图形绘制为视图的背景,然后从与拇指位置相对应的位置绘制蓝色图形。