Android 顏色渲染(五) LinearGradient線性渲染

來源:互聯網
上載者:User

標籤:

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Android 顏色處理(五) LinearGradient線性渲染

        相信很多人都看過歌詞同步的效果, 一是豎直方向的滾動,另一方面是水平方面的歌詞色彩坡形點亮效果,這種效果怎麼做呢? 這就需要用到LinearGradient線性渲染,下面還是先看具體的使用:

 

 

LinearGradient有兩個建構函式;

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

參數:

float x0: 漸層起始點x座標

float y0:漸層起始點y座標

float x1:漸層結束點x座標

float y1:漸層結束點y座標

int[] colors:顏色 的int 數組

float[] positions: 相對位置的顏色數組,可為null,  若為null,可為null,顏色沿漸層線均勻分布

Shader.TileMode tile: 渲染器平鋪模式

 

public LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1,Shader.TileMode tile)

float x0: 漸層起始點x座標

float y0:漸層起始點y座標

float x1:漸層結束點x座標

float y1:漸層結束點y座標

int color0: 起始漸層色

int color1: 結束漸層色

Shader.TileMode tile: 渲染器平鋪模式

 

:

                                  

然後看一下具體怎麼實現這種效果;

MainActivity:

[java] view plain copy
  1. package com.tony.shader;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5.   
  6. public class MainActivity extends Activity {  
  7.   
  8.     private LinearGradientView linearGradientView;  
  9.       
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.   
  14.         linearGradientView = new LinearGradientView(this);  
  15.         setContentView(linearGradientView);  
  16.     }  
  17.   
  18. }  


LinearGradientView;

[java] view plain copy
    1. package com.tony.shader;  
    2.   
    3. import android.content.Context;  
    4. import android.graphics.Canvas;  
    5. import android.graphics.Color;  
    6. import android.graphics.LinearGradient;  
    7. import android.graphics.Paint;  
    8. import android.util.AttributeSet;  
    9. import android.graphics.Shader;  
    10. import android.view.View;  
    11.   
    12. public class LinearGradientView extends View {  
    13.   
    14.     private LinearGradient linearGradient = null;    
    15.     private Paint paint = null;    
    16.       
    17.     public LinearGradientView(Context context)    
    18.     {    
    19.         super(context);    
    20.         linearGradient = new LinearGradient(0, 0, 100, 100, new int[] {    
    21.                 Color.YELLOW, Color.GREEN, Color.TRANSPARENT, Color.WHITE }, null,    
    22.                 Shader.TileMode.REPEAT);    
    23.         paint = new Paint();    
    24.     }    
    25.       
    26.     public LinearGradientView(Context context, AttributeSet attrs) {  
    27.         super(context, attrs);  
    28.     }  
    29.       
    30.     @Override  
    31.     protected void onDraw(Canvas canvas) {  
    32.         // TODO Auto-generated method stub  
    33.         super.onDraw(canvas);  
    34.         //設定渲染器  
    35.         paint.setShader(linearGradient);    
    36.                 //繪製圓環  
    37.         canvas.drawCircle(240, 360, 200, paint);   
    38.     }  
    39.   

Android 顏色渲染(五) LinearGradient線性渲染

聯繫我們

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