在自定义视图中设置 Paint 对象的颜色 [英] Setting color of a Paint object in custom view
问题描述
我正在尝试创建自定义视图并声明了如下样式的属性:-
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>
在 customview 的构造函数中,这些值的获取方式如下:-
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
通过如下声明xml来使用视图:-
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
我没有得到 xml 中定义的颜色黑色"
I dont get the color-"black" defined in the xml
但是当我将颜色设置为
however when i set the color as
thePaintObj.setColor(Color.GRAY)
我在视图中获取颜色
谁能告诉我我做错了什么?
Can someone tell me what would I be doing wrong ?
(注意:-如果你想让我发布更多代码,请告诉我)
(N.B:-If you want me to post more code , please let me know)
- 发布我的colors.xml.看起来在我的代码注释中不清楚:-
- 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>
推荐答案
在colors.xml
In colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black_color">#000000</color>
</resources>
检索
Resources res = getResources();
int color = res.getColor(R.color.black_color);
然后设置颜色来绘制
thePaintObj.setColor(color);
更多信息@
http://developer.android.com/guide/topic/resources/more-resources.html#Color
我的自定义视图
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>
颜色.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black_color">#000000</color>
</resources>
xml 中的 MyCustomView
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"
/>
快照
这篇关于在自定义视图中设置 Paint 对象的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!