Android SweepGradient類介紹

來源:互聯網
上載者:User

Android SweepGradient類介紹

Android 顏色處理(八) SweepGradient 掃描/梯度渲染

為什麼什麼叫掃描渲染呢? 相信大家都看過雷達掃描的效果,尤其是在安全軟體中.

 

 

public SweepGradient(float cx, float cy, int[] colors, float[] positions)Parameters:
cx 渲染中心點x 座標
cy 渲染中心y 點座標
colors 圍繞中心渲染的顏色數組,至少要有兩種顏色值
positions 相對位置的顏色數組,可為null, 若為null,可為null,顏色沿漸層線均勻分布

 

public SweepGradient(float cx, float cy, int color0, int color1)Parameters:

 

cx 渲染中心點x 座標
cy 渲染中心點y 座標
color0 起始渲染顏色
color1 結束渲染顏色
 

 

關鍵是怎麼理解這個中心點的意思,其實就是圍繞哪個點開始漸層,下面以執行個體說明:

 

public class MainActivity extends Activity {
SweepGradientView mSweep;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSweep=new SweepGradientView(this, null);
setContentView(mSweep);
}
}

 

public class SweepGradientView extends View {

Paint mPaint = null;
// 梯度渲染
Shader mSweepGradient = null;

public SweepGradientView(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);

mSweepGradient = new SweepGradient(200, 300, new int[] { Color.CYAN,
Color.DKGRAY, Color.GRAY, Color.LTGRAY, Color.MAGENTA,
Color.GREEN, Color.TRANSPARENT, Color.BLUE }, null);
mPaint = new Paint();

mPaint.setShader(mSweepGradient);

canvas.drawCircle(240, 360, 200, mPaint);
}
}

我們看到這裡的中心點給的是200、300,其實就是在螢幕上以X方向為200、Y方向為300的左邊開始漸層,如下:

 

如果以240, 360為中心點開始漸層,這個時候的效果如下:

 

 

切記理解這個中心點的意思,是圍繞哪個點開始漸層

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.