Setting color of a Paint object in custom view

前端 未结 2 1709
清酒与你
清酒与你 2020-12-06 07:04

I am trying to make a custom view and have declared the styled attributes like the below:-

  
 

        
相关标签:
2条回答
  • 2020-12-06 07:55

    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

    enter image description here

    0 讨论(0)
  • 2020-12-06 07:58

    If I understand correctly, the constant 0x000000 results in transparent black since there is no Alpha component specified. The Alpha value is the first byte of a four byte color value. The constant for opaque (solid) black is 0xff000000. In other words, the color 0x000000, which is the same as 0x00000000, results in you drawing completely transparently. The constant for Red also looks wrong, resulting in transparent green.

    0 讨论(0)
提交回复
热议问题