我正在尝试重新创建在Ice Cream Sandwich中看到的切换幻灯片,但不适用于ICS以下的Android版本。我正处于我对滑块感到舒服的地步,但我目前正在使用两个平行四边形图像(一个用于关闭状态,一个用于其开启状态)。我想在运行时理想地创建drawable,并根据状态简单地改变它的颜色。这最终会有助于定制。
我对一般的drawable很新,并且想以编程方式创建这个,因为在我们的框架中我们不使用xml。
创建它的原因是平行四边形是一个单一的部分,使得缩放更易于管理和定制。
非常感谢任何帮助,如果您需要更多信息,请告诉我们!
这是androids toggle的样子,我想在他们之后建模:
如果您需要任何其他详细信息,请告诉我,我希望我能以有意义的方式解释我所追求的内容。
谢谢!
答案 0 :(得分:5)
所以我自己能够回答这个问题......我用路径创建了drawables然后将它们拼接在一起以创建平行四边形。
public Drawable createThumbDrawable(boolean checked){
Path path = new Path();
path.moveTo(0, 0);
path.lineTo(1, 0);
path.lineTo(1, 1);
path.lineTo(0, 1);
path.close();
PathShape shape = new PathShape(path, 1, 1);
ShapeDrawable drawable = new ShapeDrawable(shape);
if (checked){
drawable.getPaint().setColor(Color.CYAN);
}
else
{
drawable.getPaint().setColor(Color.BLACK);
}
mThumbLeftDrawable = createLeftThumbDrawable(checked);
mThumbRightDrawable = createRightThumbDrawable(checked);
return drawable;
}
public Drawable createLeftThumbDrawable(boolean checked){
Path path = new Path();
path.moveTo(0, 25);
path.lineTo(25, 0);
path.lineTo(25, 25);
path.close();
PathShape shape = new PathShape(path, 25, 25);
ShapeDrawable drawable = new ShapeDrawable(shape);
if (checked){
drawable.getPaint().setColor(Color.CYAN);
}
else
{
drawable.getPaint().setColor(Color.BLACK);
}
return drawable;
}
public Drawable createRightThumbDrawable(boolean checked){
Path path = new Path();
path.moveTo(0,0);
path.lineTo(25, 0);
path.lineTo(0, 25);
path.close();
PathShape shape = new PathShape(path, 25, 25);
ShapeDrawable drawable = new ShapeDrawable(shape);
if (checked){
drawable.getPaint().setColor(Color.CYAN);
}
else
{
drawable.getPaint().setColor(Color.BLACK);
}
return drawable;
}
public void setChecked(boolean checked) {
//Log.d(TAG, "setChecked("+checked+")");
boolean lc = checked;
if (!mTextOnThumb) {
lc = !checked;
}
if (checked){
mThumbDrawable = createThumbDrawable(checked);//this.getContext().getResources().getDrawable(R.drawable.slide_off);
}
else {
mThumbDrawable = createThumbDrawable(checked);//this.getContext().getResources().getDrawable(R.drawable.slide);
}
super.setChecked(checked);
mThumbPosition = lc ? getThumbScrollRange() : 0;
invalidate();
}