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

后端 未结 2 894
眼角桃花
眼角桃花 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 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.)

提交回复
热议问题