问题描述
我想重新创建见于冰淇淋三明治切换幻灯片,但低于ICS的Android版本中不可用。我在我的舒服我滑了点,但我目前使用两个平行四边形的图像(一个用于其关闭状态,一个是它的状态)。我想理想的创建绘制在运行时,简单地改变它基于状态的颜色。这将真正帮助定制进行到底。 P>
我是很新的,一般可绘,并想以编程方式创建这一块,因为在我们的框架中,我们不使用XML。
之所以创建这个,是平行四边形是一个单件,使扩展更易于管理,并且定制的。
任何帮助将是很大的AP preciated,并请让我知道如果你需要任何更多的信息!
这是什么机器人拨动的样子,我想我的后他们的模型:
如果您需要任何其他详细信息,请让我知道,我希望我解释了我在后一种有意义的方式。
谢谢!
所以,我能回答这个我...我使用的路径创建可绘制,然后缝合在一起,以形成平行四边形。
市民可绘制createThumbDrawable(布尔检查){
路径path =新路径();
path.moveTo(0,0);
path.lineTo(1,0);
path.lineTo(1,1);
path.lineTo(0,1);
path.close();
PathShape形状=新PathShape(路径,1,1);
ShapeDrawable绘制=新ShapeDrawable(形状);
如果(检查){
。drawable.getPaint()setColor(Color.CYAN);
}
其他
{
。drawable.getPaint()setColor(Color.BLACK);
}
mThumbLeftDrawable = createLeftThumbDrawable(选中);
mThumbRightDrawable = createRightThumbDrawable(选中);
返回绘制;
}
公众可绘制createLeftThumbDrawable(布尔检查){
路径path =新路径();
path.moveTo(0,25);
path.lineTo(25,0);
path.lineTo(25,25);
path.close();
PathShape形状=新PathShape(路径,25,25);
ShapeDrawable绘制=新ShapeDrawable(形状);
如果(检查){
。drawable.getPaint()setColor(Color.CYAN);
}
其他
{
。drawable.getPaint()setColor(Color.BLACK);
}
返回绘制;
}
公众可绘制createRightThumbDrawable(布尔检查){
路径path =新路径();
path.moveTo(0,0);
path.lineTo(25,0);
path.lineTo(0,25);
path.close();
PathShape形状=新PathShape(路径,25,25);
ShapeDrawable绘制=新ShapeDrawable(形状);
如果(检查){
。drawable.getPaint()setColor(Color.CYAN);
}
其他
{
。drawable.getPaint()setColor(Color.BLACK);
}
返回绘制;
}
公共无效setChecked(布尔检查){
//Log.d(TAG,setChecked(+选中+));
布尔LC =检查;
如果(!mTextOnThumb){
!LC =检查;
}
如果(检查){
mThumbDrawable = createThumbDrawable(选中); // this.getContext()getResources()getDrawable(R.drawable.slide_off)。
}
其他 {
mThumbDrawable = createThumbDrawable(选中); // this.getContext()getResources()getDrawable(R.drawable.slide)。
}
super.setChecked(选中);
mThumbPosition = LC? getThumbScrollRange():0;
无效();
}
I am trying to recreate the toggle slide that is seen in Ice Cream Sandwich, but unavailable for Android versions below ICS. I am at the point where I am comfortable with my slider, however I am currently using two parallelogram images (one for its off state, one for its on state). I would like to ideally create the drawable at runtime, and simply change the color of it based on the state. This would really help for customization in the end.
I am quite new to drawables in general, and would like to create this one programmatically, since in our framework we do not use xml.
The reason for creating this, is that parallelogram is one single piece, making scaling much more manageable, and customizable.
Any help would be greatly appreciated, and please let me know if you need any more info!
This is what androids toggle looks like, I would like to model mine after theirs:
If you need any other details please let me know, I hope I explained what I am after in a meaningful way.
Thanks!
So I was able to answer this myself...I used paths to create the drawables and then stitched them together to create the parallelogram.
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();
}
这篇关于在Android的编程方式创建平行四边形绘制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!