1. 程式人生 > >Android 程式碼中設定Color引數

Android 程式碼中設定Color引數

2014-09-25補充:

Color.argb(16, 210, 35, 210)

原來Color類就可以實現,Paint的方案有些複雜了。
</pre><p></p><p></p><p>今天想繪製一條根據引數顏色動態變化的曲線,引數不固定,因此需要根據引數來獲取顏色值。研究了很久,搞了一個通過Paint實現的方案。</p><p><span style="color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px">Paint 中包含了很多方法對其屬性進行設定,主要方法如下: </span></p><span style="color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px">   setAntiAlias: 設定畫筆的鋸齒效果。 </span><br style="margin:0px; padding:0px; color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px" /><span style="color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px">   setColor: 設定畫筆顏色 </span><br style="margin:0px; padding:0px; color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px" /><span style="color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px">   setARGB:  設定畫筆的a,r,p,g值。 </span><br style="margin:0px; padding:0px; color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px" /><span style="color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px">   setAlpha:  設定Alpha值 </span><br style="margin:0px; padding:0px; color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px" /><span style="color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px">   setTextSize: 設定字型尺寸。 </span><br style="margin:0px; padding:0px; color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px" /><span style="color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px">   setStyle:  設定畫筆風格,空心或者實心。 </span><br style="margin:0px; padding:0px; color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px" /><span style="color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px">   setStrokeWidth: 設定空心的邊框寬度。 </span><br style="margin:0px; padding:0px; color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px" /><span style="color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px">   getColor:  得到畫筆的顏色 </span><br style="margin:0px; padding:0px; color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px" /><p><span style="color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px">   getAlpha:  得到畫筆的Alpha值。 </span></p><p><span style="color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px"></span></p><p><span style="color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px">我們需要的就是setARGB和getColor。</span></p><p><span style="color:rgb(17,17,17); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:23.399999618530273px"></span></p><pre name="code" class="java">private int getColor(float yourParam) {
		int alfa = 120;
		int rColor = 255;
		int gColor = 255;
		int bColor = 255;
		/*********
		 * Your code, change alfa/r/g/b by yourParam
		 *********/
		Paint paint = new Paint();
		paint.setARGB(alfa, rColor, gColor, bColor);
		return paint.getColor();
	}