设置一个Paint对象的颜色自定义视图 [英] Setting color of a Paint object in custom view
问题描述
我试图做一个自定义视图,并已宣布类似下面的风格属性: -
<资源>
<申报,设置样式名称=NewCircleView>
< attr指示NAME =半径的格式=整数/>
< attr指示NAME =circlecolor格式=颜色/>
< /申报,设置样式>
< /资源>
在customview的构造函数,这些值象下面这样获得的: -
circleradius = a.getInt(R.styleable.NewCircleView_radius,0); //全局变量
circlecolor = a.getColor(R.styleable.NewCircleView_circlecolor,0); //全局变量和一个是类型数组
该视图用于通过声明的XML如下: -
< com.customviews.NewCircleView
机器人:layout_below =@ ID / thetext
机器人:layout_width =match_parent
机器人:layout_height =FILL_PARENT
应用:半径=10000
应用程序:circlecolor =@彩色/黑白< - 这是colors.xml定义
/>
在当我设置的油漆对象作为自定义视图: -
thePaintObj.setColor(circlecolor); // circlecolor日志到一个整数预期
我不明白的color-黑在XML定义
然而,当我设置颜色为
thePaintObj.setColor(Color.GRAY)
我得到的颜色在视图
谁能告诉我什么我做错了?
(注: - 如果你要我发布更多code,请让我知道)
EDIT1: - 张贴我colors.xml。看起来它并不清楚我的code注释: -
< XML版本=1.0编码=UTF-8&GT?;
<资源>
<颜色名称=红>#7F00< /彩色>
<颜色名称=蓝色>#770000ff< /彩色>
<颜色名称=绿色>#7700ff00< /彩色>
<颜色名称=黄色>#77ffff00< /彩色>
<颜色名称=黑>#000000< /彩色>
< /资源>
在colors.xml
< XML版本=1.0编码=UTF-8&GT?;
<资源>
<颜色名称=black_color>#000000< /彩色>
< /资源>
要检索
资源资源= getResources();
INT颜色= res.getColor(R.color.black_color);
然后设置颜色漆
thePaintObj.setColor(彩色);
更多信息@
<一个href="http://developer.android.com/guide/topics/resources/more-resources.html#Color">http://developer.android.com/guide/topics/resources/more-resources.html#Color
编辑:
MyCustomView
公共类CustomView扩展视图{
涂料P;
INT的颜色;
公共CustomView(上下文的背景下){
这(背景下,NULL);
}
公共CustomView(上下文的背景下,ATTRS的AttributeSet){
这(背景下,ATTRS,0);
}
公共CustomView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
超(背景下,ATTRS,defStyle);
//这里真正的工作
TypedArray A = context.getTheme()。obtainStyledAttributes(
ATTRS,
R.styleable.NewCircleView,
0,0
);
尝试 {
颜色= a.getColor(R.styleable.NewCircleView_circlecolor,0xff000000);
} 最后 {
//释放TypedArray,以便它可以重复使用。
a.recycle();
}
在里面();
}
公共无效的init()
{
P =新的油漆();
p.setColor(颜色);
}
@覆盖
保护无效的OnDraw(帆布油画){
// TODO自动生成方法存根
super.onDraw(画布);
如果(帆布!= NULL)
{
canvas.drawCircle(100,100,30页);
}
}
}
attrs.xml
&LT; XML版本=1.0编码=UTF-8&GT?;
&LT;资源&GT;
&LT;申报,设置样式名称=NewCircleView&GT;
&LT; attr指示NAME =半径的格式=整数/&GT;
&LT; attr指示NAME =circlecolor格式=颜色/&GT;
&LT; /申报,设置样式&GT;
&LT; /资源&GT;
colors.xml
&LT; XML版本=1.0编码=UTF-8&GT?;
&LT;资源&GT;
&LT;颜色名称=black_color&GT;#000000&LT; /彩色&GT;
&LT; /资源&GT;
MyCustomView在XML
&LT; com.example.circleview.CustomView
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res/com.example.circleview
机器人:ID =@ + ID / CV
机器人:layout_width =match_parent
机器人:layout_height =FILL_PARENT
应用:半径=30
应用程序:circlecolor =@色/ black_color
/&GT;
快拍
I am trying to make a custom view and have declared the styled attributes like the below:-
<resources>
<declare-styleable name="NewCircleView">
<attr name="radius" format="integer"/>
<attr name="circlecolor" format="color"/>
</declare-styleable>
</resources>
in the constructor of the customview , these values are obtained like below:-
circleradius=a.getInt(R.styleable.NewCircleView_radius, 0);//global var
circlecolor=a.getColor(R.styleable.NewCircleView_circlecolor, 0);//global var and a is the typed array
The view is used by declaring the xml as below:-
<com.customviews.NewCircleView
android:layout_below="@id/thetext"
android:layout_width="match_parent"
android:layout_height="fill_parent"
app:radius="10000"
app:circlecolor="@color/black"<!--this is defined in colors.xml
/>
In the custom view when i set the paint object as :-
thePaintObj.setColor(circlecolor);//circlecolor logs to an integer as expected
I dont get the color-"black" defined in the xml
however when i set the color as
thePaintObj.setColor(Color.GRAY)
I get the color in the view
Can someone tell me what would I be doing wrong ?
(N.B:-If you want me to post more code , please let me know)
EDIT1:- Posting my colors.xml. Looks like it is not clear in my code comments:-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#7f00</color>
<color name="blue">#770000ff</color>
<color name="green">#7700ff00</color>
<color name="yellow">#77ffff00</color>
<color name="black">#000000</color>
</resources>
In colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black_color">#000000</color>
</resources>
To retrieve
Resources res = getResources();
int color = res.getColor(R.color.black_color);
Then set color to paint
thePaintObj.setColor(color);
More info @
http://developer.android.com/guide/topics/resources/more-resources.html#Color
Edit:
MyCustomView
public class CustomView extends View{
Paint p;
int color ;
public CustomView(Context context) {
this(context, null);
}
public CustomView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// real work here
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.NewCircleView,
0, 0
);
try {
color = a.getColor(R.styleable.NewCircleView_circlecolor, 0xff000000);
} finally {
// release the TypedArray so that it can be reused.
a.recycle();
}
init();
}
public void init()
{
p = new Paint();
p.setColor(color);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if(canvas!=null)
{
canvas.drawCircle(100, 100,30,p );
}
}
}
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="NewCircleView">
<attr name="radius" format="integer"/>
<attr name="circlecolor" format="color" />
</declare-styleable>
</resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black_color">#000000</color>
</resources>
MyCustomView in xml
<com.example.circleview.CustomView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.circleview"
android:id="@+id/cv"
android:layout_width="match_parent"
android:layout_height="fill_parent"
app:radius="30"
app:circlecolor="@color/black_color"
/>
Snap Shot
这篇关于设置一个Paint对象的颜色自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!