【问题标题】:Setting color of a Paint object in custom view在自定义视图中设置 Paint 对象的颜色
【发布时间】:2013-09-08 08:43:14
【问题描述】:

我正在尝试制作自定义视图并声明了如下样式属性:-

  <resources>
 <declare-styleable name="NewCircleView">
    <attr name="radius" format="integer"/>
    <attr name="circlecolor" format="color"/>
</declare-styleable>

 </resources> 

在 customview 的构造函数中,这些值的获取方式如下:-

    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来使用视图:-

 <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
      />

在自定义视图中,当我将绘制对象设置为:-

thePaintObj.setColor(circlecolor);//circlecolor logs to an integer as expected

我没有得到 xml 中定义的颜色-“黑色”

但是当我将颜色设置为

thePaintObj.setColor(Color.GRAY)

我得到了视图中的颜色

谁能告诉我我做错了什么?

(注:-如果您希望我发布更多代码,请告诉我)

EDIT1:- 发布我的 colors.xml。看起来我的代码 cmets 中并不清楚:-

<?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中定义颜色

标签: android android-custom-view


【解决方案1】:

在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/topics/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>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="black_color">#000000</color>
</resources>

xml 中的 MyCustomView

<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"
      />

快照

【讨论】:

  • 我已经在我的 cmets 中针对代码中提到的 colors.xml 中声明了颜色,我正在通过 xml 检索它,其中自定义视图用作 app:circlecolor="@color/black"我想和上面的一样。如果我错了,请告诉我
  • @itamecodes 我刚刚尝试过,它对我有用。我会发布同样的内容。
  • 感谢您为此付出的努力,但如果您替换 color = a.getColor(R.styleable.NewCircleView_circlecolor, 0xff000000); 是否仍然有效with color = a.getColor(R.styleable.NewCircleView_circlecolor);
  • @itamecodes 这是默认值developer.android.com/reference/android/content/res/…。检查getColor 方法的文档。另外,如果有帮助请接受答案
【解决方案2】:

如果我理解正确,常数 0x000000 会导致透明黑色,因为没有指定 Alpha 分量。 Alpha 值是四字节颜色值的第一个字节。不透明(纯色)黑色的常数为 0xff000000。换句话说,与 0x00000000 相同的颜色 0x000000 会导致您完全透明地绘制。 Red 的常量看起来也有问题,导致透明的绿色。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 2017-03-04
    • 2019-03-11
    • 1970-01-01
    • 2020-04-21
    • 2019-05-16
    相关资源
    最近更新 更多