如何使一个可绘制对象,在code我选择的颜色 [英] How to make a Drawable object with my selected color in code

查看:138
本文介绍了如何使一个可绘制对象,在code我选择的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设置选定颜色的图标为preference:

  preference PRF =(preference)找到preference(SelectColor preF)​​;
 

prf.setIcon(Drawbale图标);

有关这方面,我需要选择颜色的物体绘制对象

这是可能使在Java中code进行绘制对象图标?请指导我..

问候,

/////////////////////////////////////////////// ////////////

在以下阿莱克斯G的概念,它解决的是:

  preference TextClr preF =(preference)找到preference(text_color_ preference);

位图BM = Bitmap.createBitmap(30,30,Bitmap.Config.ARGB_8888);
帆布CNV =新的Canvas(BM);
INT红色= 0xFFFF0000地址;
cnv.drawColor(红色);
可绘制绘制=新BitmapDrawable(BM);

TextClr preF .setIcon(绘制);
 

解决方案

您应该可以使用code与此类似,以建立一个坚实的颜色绘制对象:

 位图BM = BitmapFactory.createBitmap(50,50,Bitmap.Config.ARGB_8888);
帆布CNV =新的Canvas(BM);
INT红色=为0xFF0000;
cnv.drawColor(红色);
可绘制绘制=新BitmapDrawable(BM);
 

这将创建一个包含一个50×50像素的红色正方形的可绘制。

(请注意,我没有测试此code,但我用我的code类似的东西。)

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

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

prf.setIcon(Drawbale icon);

For this i need an object Drawable of selected color.

Is this possible to make a Drawable icon in java code? Please guide me..

Regards,

///////////////////////////////////////////////////////////

After following Aleks G's concept it solved as:

Preference TextClrPref = (Preference) findPreference("text_color_preference");

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

TextClrPref .setIcon(drawable);

解决方案

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.)

这篇关于如何使一个可绘制对象,在code我选择的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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