在Android的编程方式创建平行四边形绘制对象 [英] Programmatically create Parallelogram Drawable in Android

查看:2328
本文介绍了在Android的编程方式创建平行四边形绘制对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重新创建见于冰淇淋三明治切换幻灯片,但低于ICS的Andr​​oid版本中不可用。我在我的舒服我滑了点,但我目前使用两个平行四边形的图像(一个用于其关闭状态,一个是它的状态)。我想理想的创建绘制在运行时,简单地改变它基于状态的颜色。这将真正帮助定制进行到底。

我是很新的,一般可绘,并想以编程方式创建这一块,因为在我们的框架中,我们不使用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的编程方式创建平行四边形绘制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆