Android ApiDemos樣本解析(80):Graphics->Sweep

來源:互聯網
上載者:User

SweepGradient 色彩坡形方向非環形,而是以某點為圓心呈扇形掃過。

SweepGradient的建構函式:

public SweepGradient(float cx, float cy, int[] colors, float[] positions)
public SweepGradient(float cx, float cy, int color0, int color1)

其中cx,cy 指定圓心, color1,color0 或 colors 指定漸層的顏色 ,對於使用多於兩種顏色時,還可以通過positions 指定每種顏色的相對位置,positions 設為NULL時表示顏色均勻分布。

LineerGradient, RadialGradient, SweepGradient 等類的基類為Shader (類似於GDI中的Brush類) ,Shader 定義了一個與之對應的Local Matrix ,可以最Shader 做座標變換。本例使用Shader的LocalMatrix 旋轉不同的角度,形成了動畫效果:

[java] 
float x = 160; 
float y = 100; 
mShader = new SweepGradient(x, y, new int[] { Color.GREEN, 
 Color.RED, 
 Color.BLUE, 
 Color.GREEN }, null); 
mPaint.setShader(mShader); 
  
... 
protected void onDraw(Canvas canvas) { 
... 
 mMatrix.setRotate(mRotate, x, y); 
 mShader.setLocalMatrix(mMatrix); 
 mRotate += 3; 
 if (mRotate >= 360) { 
 mRotate = 0; 
 } 
 invalidate(); 
... 
} 

float x = 160;
float y = 100;
mShader = new SweepGradient(x, y, new int[] { Color.GREEN,
 Color.RED,
 Color.BLUE,
 Color.GREEN }, null);
mPaint.setShader(mShader);
 
...
protected void onDraw(Canvas canvas) {
...
 mMatrix.setRotate(mRotate, x, y);
 mShader.setLocalMatrix(mMatrix);
 mRotate += 3;
 if (mRotate >= 360) {
 mRotate = 0;
 }
 invalidate();
...
}本例還可以按”D” ,”T”鍵來顯示Paint 使用Dither (Dither定義可參見Android ApiDemos樣本解析(59):Graphics->ColorFilters ) 以及不同的“掃過速度”時的顯示效果:

 
作者:mapdigit
 


 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.