欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程實現(xiàn)自定義漸變顏色效果詳解

 更新時間:2017年08月02日 11:33:18   作者:davelam1991  
這篇文章主要介紹了Android編程實現(xiàn)自定義漸變顏色效果,結(jié)合具體實例形式分析了Android基于xml及代碼定義來實現(xiàn)顏色漸變的相關(guān)操作技巧與注意事項,需要的朋友可以參考下

本文實例講述了Android編程實現(xiàn)自定義漸變顏色效果。分享給大家供大家參考,具體如下:

你是否已經(jīng)厭惡了純色的背景呢?那好,Android提供給程序員自定義漸變顏色的接口,讓我們的界面炫起來吧。

xml定義漸變顏色

首先,你在drawable目錄下寫一個xml,代碼如下

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" >
  <gradient
    android:angle="270"
    android:endColor="#000000"
    android:startColor="#ffffff" />
  <corners
    android:bottomLeftRadius="5dip"
    android:bottomRightRadius="5dip"
    android:topLeftRadius="5dip"
    android:topRightRadius="5dip" />
</shape>

shape 節(jié)點配置的是圖形的形式,主要包括方形、圓形等,上邊代碼為方形,
gradient 節(jié)點主要配置起點顏色、終點顏色及中間點的顏色、坐標、漸變效果(0,90,180從左到右漸變,270從上到下漸變)默認從左到右
padding 節(jié)點主要配置上下左右的間距
corners 節(jié)點配置四周園腳的半徑

然后,你就可以隨意在代碼中或者xml布局中使用它了。

如此簡單的配置,只要你知道顏色的rgb值,你就可以成為顏色達人。

代碼定義漸變顏色

Android平臺下實現(xiàn)漸變效果。在android.graphics中我們可以找到有關(guān)Gradient字樣的類,比如LinearGradient 線性漸變、RadialGradient徑向漸變和 角度漸變SweepGradient 三種,他們的基類為android.graphics.Shader。為了顯示出效果,使用一個簡單的例子來說明。

一、LinearGradient線性漸變

在android平臺中提供了兩種重載方式來實例化該類分別為,他們的不同之處為參數(shù)中第一種方法可以用顏色數(shù)組,和位置來實現(xiàn)更細膩的過渡效果,比如顏色采樣int[] colors數(shù)組中存放20種顏色,則漸變將會逐一處理。而第二種方法參數(shù)僅為起初顏色color0和最終顏色color1。

LinearGradient(float x0, float y0, float x1, float y1, int[] colors, float[] positions, Shader.TileMode tile)
LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1, Shader.TileMode tile)

使用實例如下:

Paint p=new Paint();
LinearGradient lg=new LinearGradient(0,0,100,100,Color.RED,Color.BLUE,Shader.TileMode.MIRROR);

參數(shù)一為漸變起初點坐標x位置,參數(shù)二為y軸位置,參數(shù)三和四分辨對應(yīng)漸變終點,最后參數(shù)為平鋪方式,這里設(shè)置為鏡像.

剛才Android開發(fā)網(wǎng)已經(jīng)講到Gradient是基于Shader類,所以我們通過Paint的setShader方法來設(shè)置這個漸變,代碼如下:

p.setShader(lg);
canvas.drawCicle(0,0,200,p); //參數(shù)3為畫圓的半徑,類型為float型。

二、 RadialGradient鏡像漸變

有了上面的基礎(chǔ),我們一起來了解下徑向漸變。和上面參數(shù)唯一不同的是,徑向漸變第三個參數(shù)是半徑,其他的和線性漸變相同。

RadialGradient(float x, float y, float radius, int[] colors, float[] positions, Shader.TileMode tile)
RadialGradient(float x, float y, float radius, int color0, int color1, Shader.TileMode tile)

三、 SweepGradient角度漸變

對于一些3D立體效果的漸變可以嘗試用角度漸變來完成一個圓錐形,相對來說比上面更簡單,前兩個參數(shù)為中心點,然后通過載入的顏色來平均的漸變渲染。

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

對于最后一個參數(shù)SDK上的描述為:

May be NULL. The relative position of each corresponding color in the colors array, beginning with 0 and ending with 1.0. If the values are not monotonic, the drawing may produce unexpected results. If positions is NULL, then the colors are automatically spaced evenly.

所以Android123建議使用下面的重載方法,本方法一般為NULL即可。

SweepGradient(float cx, float cy, int color0, int color1)

或者直接創(chuàng)建一個drawable:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE); //設(shè)置沒標題
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN , //全屏
           WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.login);//登錄界面
    GradientDrawable grad = new GradientDrawable(//漸變色
      Orientation.TOP_BOTTOM,
      new int[]{Color.BLACK, Color.WHITE}
    );
    getWindow().setBackgroundDrawable(grad);//設(shè)置漸變顏色
}

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android布局layout技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

最新評論