2016-10-02 174 views
0

我在Android上创建了一个自定义视图,但无论我如何改变它,呈现的颜色总是灰色的。Android画布drawRect颜色总是显示为灰色?

private void init() { 
     Resources res = mContext.getResources(); 
     float density = res.getDisplayMetrics().density; 

     mBackgroundWidth = (int)(DEFAULT_WIDTH * density); // default to 20dp 
     mPrimaryColor = gaugeColour; 
     mPrimaryWidth = (int)(DEFAULT_WIDTH * density); // default to 20dp 

     x_Corner=30*density; 
     y_Corner=30*density; 

     mRegularTextSize = (int)(mBackgroundWidth * 0.75); //Double the size of the width; 

     mRectPaintPrimary = new Paint() { 
      { 
       setDither(true); 
       setStyle(Style.FILL); 
       setStrokeCap(Cap.ROUND); 
       setAntiAlias(true); 
      } 
     }; 
     mRectPaintPrimary.setColor(mPrimaryColor); 

//code for text formatting followed 

    } 

这是OnDraw函数

@Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas);// bound our drawable Rect to stay fully within our canvas 

     float left=0,top=0,right=mDrawingRect.right,bottom=mDrawingRect.bottom; 

     mProgressRect=new RectF(left,top,(mProgressPercent/100)*right,bottom); 

     canvas.drawRoundRect(mProgressRect, x_Corner, y_Corner, mRectPaintPrimary); 

     //noinspection ResourceType 
     String newColor = getResources().getString(mRectPaintPrimary.getColor()); 
     Log.d(TAG,"Rect colour while drawing is "+newColor); 

     String valueString=((int)mProgressPercent)+"%"; 
     if(mProgressPercent<10) 
      valueString=""; 
     canvas.drawText(valueString,mProgressRect.centerX(),mProgressRect.centerY()*1.5f,mRegularText); 

    } 

我的日志竟然说,编程的颜色已被修改。所以我得到

d/GaugeView的线的消息:矩形颜色,而绘画是#ffe64a19

但我看到了Android的屏幕上始终是相同的灰色......不管怎么样我改变颜色为:

enter image description here

回答

0

我似乎改变

有我需要的结果3210

mRectPaintPrimary.setColor(getResources().getColor(mPrimaryColor)); 

甚至更​​好

mRectPaintPrimary.setColor(ContextCompat.getColor(mContext,mPrimaryColor));