我正在尝试显示一个简单的颜色选择器,以便用户可以选择几个文本的颜色......但到目前为止我找到的每个颜色选择器似乎都太复杂了。如果至少我对如何有所了解,我不介意自己编码。
有人能为我提供一些简单的颜色选择器的代码吗? 或者指出我进一步研究如何编码的方向?
答案 0 :(得分:7)
如果其他人碰到这里是检索值的代码:
seekBarFont.setMax(256*7-1);
seekBarFont.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser){
int r = 0;
int g = 0;
int b = 0;
if(progress < 256){
b = progress;
} else if(progress < 256*2) {
g = progress%256;
b = 256 - progress%256;
} else if(progress < 256*3) {
g = 255;
b = progress%256;
} else if(progress < 256*4) {
r = progress%256;
g = 256 - progress%256;
b = 256 - progress%256;
} else if(progress < 256*5) {
r = 255;
g = 0;
b = progress%256;
} else if(progress < 256*6) {
r = 255;
g = progress%256;
b = 256 - progress%256;
} else if(progress < 256*7) {
r = 255;
g = 255;
b = progress%256;
}
seekBarFont.setBackgroundColor(Color.argb(255, r, g, b));
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
答案 1 :(得分:4)
试试这个:
在xml中使用:
<SeekBar
android:id="@+id/seekbar_font"
android:layout_width="300dip"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:max="100"
android:progress="50"></SeekBar>
活动:
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 );