如何在SeekBar中创建颜色渐变? [英] How to make a color gradient in a SeekBar?
问题描述
我想使用一个SeekBar(即旧学校Java Slider)到一个颜色渐变选择器。我看到了一些这样的例子,但他们都需要做新的类等。必须有一种方法来修改或覆盖原始类。或者只是用渐变替换背景。
I want to use a SeekBar (i.e. old school Java Slider) into a color gradient picker. I have seen some examples like this but they all require making new classes and such. There has got to be a way to modify or override the original classes. Or just replace the background with a gradient.
推荐答案
我想这里是你怎么做的。
I figured it out here is how you do it.
您在XML中创建标准的seekbar。
You create a standard seekbar in your XML.
<SeekBar
android:id="@+id/seekbar_font"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:layout_below="@id/color_font_text"
android:max="100"
android:progress="50"></SeekBar>
然后通过创建一个boxShape在你的onCreate()中自定义seekbar,然后在里面加一个LinearGradient 。
Then you customize the seekbar in your onCreate() by creating a boxShape and then force a LinearGradient inside it.
LinearGradient test = new LinearGradient(0.f, 0.f, 300.f, 0.0f,
new int[] { 0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF,
0xFFFF0000, 0xFFFF00FF, 0xFFFFFF00, 0xFFFFFFFF},
null, TileMode.CLAMP);
ShapeDrawable shape = new ShapeDrawable(new RectShape());
shape.getPaint().setShader(test);
SeekBar seekBarFont = (SeekBar)findViewById(R.id.seekbar_font);
seekBarFont.setProgressDrawable( (Drawable)shape );
以下是 SeekBar颜色渐变
这篇关于如何在SeekBar中创建颜色渐变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!