Android canvas drawRect颜色总是显示为灰色?

时间:2016-10-02 18:27:16

标签: android canvas android-canvas android-custom-view

我在Android上创建自定义视图,但无论我如何尝试更改它,渲染的颜色始终为灰色。

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

1 个答案:

答案 0 :(得分:0)

我似乎通过更改

获得了我所需的结果
<label>

<!DOCTYPE html>
<html>
<head>
  <title>JS Bin</title>
  	  <script type="text/javascript">
	    function performClick(elemId) {
	    var elem = document.getElementById(elemId);
	    if(elem && document.createEvent) {
	    var evt = document.createEvent("MouseEvents");
	    evt.initEvent("click", true, false);
	    elem.dispatchEvent(evt);
	    }
	    }
        
        function handleFile(elem) {
          console.log(elem.files[0].name, elem.value);
          document.querySelector("[for='file']")
          .innerHTML = elem.files[0].webkitRelativePath 
                       || elem.files[0].name
                       || elem.value.slice(
                           elem.value.lastIndexOf("\\") + 1
                          );
        }
	  </script>
</head>
<body>
  <form action="uploadfile.php" method="post" enctype="multipart/form-data">
    
    <!-- http://stackoverflow.com/a/16015086/702977 -->
    <a href="#" class="btn btn-primary" type="button" onclick="document.getElementById('file').click(); return false;">Browse</a> <input id="file" name="file" type="file" style="visibility: hidden; display: none;" onchange="handleFile(this)" />
    
    <!-- http://stackoverflow.com/a/6463467/702977 -->
    <!-- <a href="#" class="btn btn-primary" type="button" onclick="performClick('file');">Choose File</a><input type="file" name="file" id="file" style="position: fixed; top: -100em" />-->

	<input type="submit" id="u_button" name="u_button" value="Upload the file"><label for="file"></label>
  </form>
</body>
</html>

甚至更好

    mRectPaintPrimary.setColor(mPrimaryColor);