How to make a Drawable object with my selected color in code

后端 未结 2 893
眼角桃花
眼角桃花 2021-01-26 03:08

I am trying to set icon of selected color to a preference:

Preference prf = (Preference) findPreference(\"SelectColorPref\");

prf.setIcon

相关标签:
2条回答
  • 2021-01-26 03:53

    the easiest way using ShapeDrawable

     ShapeDrawable sdrawable = new ShapeDrawable(new RectShape());
     sdrawable.paint.Color = color.RED;
     sdrawable.setIntrinsicWidth(10);
     sdrawable.setIntrinsicHeight(40);
    
    0 讨论(0)
  • 2021-01-26 04:02

    You should be able to create a solid-colour Drawable using code similar to this:

    Bitmap bm = BitmapFactory.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
    Canvas cnv = new Canvas(bm);
    int red = 0xff0000;
    cnv.drawColor(red);
    Drawable drawable = new BitmapDrawable(bm);
    

    This will create a Drawable containing a 50x50 pixels red square.

    (Please note that I haven't tested this code, but I use something similar in my code.)

    0 讨论(0)
提交回复
热议问题