android的shader渲染器

來源:互聯網
上載者:User

標籤:android   渲染器   shader   

android裡邊的渲染器的使用主要是shader的子類,shader繼承自object,他的子類有:

             1、BitMapShader:BitMapShader 是bitmap渲染器,看名字就知道,

BitmapShader是Shader的子類,可以通過Paint.setShader(Shader shader)進行設定、

這裡我們只關注BitmapShader,構造方法:

mBitmapShader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);

參數1:bitmap

參數2,參數3:TileMode;

TileMode的取值有三種:

CLAMP 展開

REPEAT 重複

MIRROR 鏡像

如果大家給電腦螢幕設定屏保的時候,如果圖片太小,可以選擇重複、展開、鏡像;

重複:就是橫向、縱向不斷重複這個bitmap

鏡像:橫向不斷翻轉重複,縱向不斷翻轉重複;

展開:這個和電腦屏保的模式應該有些不同,這個展開的是圖片最後的那一個像素;橫向的最後一個橫行像素,不斷的重複,縱項的那一列像素,不斷的重複;

現在大概明白了,BitmapShader通過設定給mPaint,然後用這個mPaint繪圖時,就會根據你設定的TileMode,對繪製地區進行著色。

這裡需要注意一點:就是BitmapShader是從你的畫布的左上方開始繪製的,不在view的右下角繪製個正方形,它不會在你正方形的左上方開始。


             2、LinearGradient:

線性漸層也是繼承與shader:LinearGradient lg=new LinearGradien(0,0,100,100,Color.RED,Color.BLUE,Shader.TileMode.MIRROR);  
參數一為漸層起初點座標x位置,參數二為y軸位置,參數三和四分辨對應漸層終點,最後參數為平鋪方式,這裡設定為鏡像

Gradient是基於Shader類,所以我們通過Paint的setShader方法來設定這個漸層,代碼如下: mPaint.setShader(lg);
canvas.drawCicle(0,0,200,mPaint); //參數3為畫圓的半徑,類型為float型。

 

它除了定義開始顏色和結束顏色以外還可以定義,多種顏色組成的分段漸層效果
LinearGradient shader = new LinearGradient(0, 0, endX, endY, new int[]{startColor, midleColor, endColor},new float[]{0 , 0.5f, 1.0f}, TileMode.MIRROR);
其中參數new int[]{startColor, midleColor, endColor}是參與漸層效果的顏色集合,
其中參數new float[]{0 , 0.5f, 1.0f}是定義每個顏色處於的漸層相對位置,
這個參數可以為null,如果為null表示所有的顏色按順序均勻的分布


             3、RadialGradient:

環形渲染:與上邊的基本差不多

//建立環形渲染對象,選擇重複模式
         int mColorRadial[] = {Color.GREEN, Color.RED, Color.BLUE, Color.WHITE};
         mRadialGradient = new RadialGradient(350, mBitmap.getHeight()*3/4+75, 75, mColorRadial, null,
                 Shader.TileMode.REPEAT);

             4、ComposeShader:

組合渲染:他與上邊的不同就是他可以包含倆種不同的渲染方式,然後將其組合。

  mComposeShader = new ComposeShader(mLinearGradient, mRadialGradient,
                 PorterDuff.Mode.DARKEN);

             5、SweepGradient:

梯形渲染:   //建立梯形渲染對象
         int mColorSweep[] = {Color.GREEN, Color.RED, Color.BLUE, Color.YELLOW, Color.GREEN};
         mSweepGradient = new SweepGradient(540, 750, mColorSweep, null);    //第一個參數是說你要     從x軸的什麼位置開始漸層,  第二個是y軸 ,第三個是漸層顏數組,第四個是位置,可以指定漸層的絕對位置


還是看看代碼吧:

package com.example.colorselect;
 import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
 
 @SuppressLint({ "DrawAllocation", "DrawAllocation", "DrawAllocation" })
 public class BitMapShaderView extends View  {
     
    Bitmap mBitmap = null;                              //Bitmap對象    
     Shader mBitmapShader = null;               //Bitmap渲染對象
    Shader mLinearGradient = null;             //線性漸層渲染對象
   Shader mComposeShader = null;           //混合渲染對象
    Shader mRadialGradient = null;             //環形渲染對象
     Shader mSweepGradient = null;             //梯度渲染對象
           
     
  /*   TileMode的取值有三種:

     CLAMP 展開

     REPEAT 重複

     MIRROR 鏡像*/
    public BitMapShaderView(Context context) {
         super(context);
        
        //載入映像資源,擷取到bitmap對象
         mBitmap = ((BitmapDrawable) getResources().
                 getDrawable(R.drawable.mate5)).getBitmap();        
        //建立Bitmap渲染對象,選取了平鋪 x軸 ,y軸重複        倆個屬性
        mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.MIRROR,
                 Shader.TileMode.REPEAT);       
             //建立線性渲染對象,選擇了平鋪
        int mColorLinear[] = {Color.RED, Color.GREEN, Color.BLUE, Color.WHITE};  //初始化漸層顏色的數組
         mLinearGradient = new LinearGradient(0, 0, 100, 100, mColorLinear, null,
                 Shader.TileMode.MIRROR);    
                     
         //建立環形渲染對象,選擇重複模式
         int mColorRadial[] = {Color.GREEN, Color.RED, Color.BLUE, Color.WHITE};
         mRadialGradient = new RadialGradient(350, mBitmap.getHeight()*3/4+75, 75, mColorRadial, null,
                 Shader.TileMode.REPEAT);
          Shader LinearGradient = new LinearGradient(430, 800, 600, 1000, mColorLinear, null,
                 Shader.TileMode.MIRROR);    
         
         Shader RadialGradient = new RadialGradient(430, mBitmap.getHeight()*3/4+75, 75, mColorRadial, null,
                 Shader.TileMode.REPEAT);
     
         //建立混合渲染對象
         mComposeShader = new ComposeShader(mLinearGradient, mRadialGradient,
                 PorterDuff.Mode.DARKEN);
         
         
         //建立梯形渲染對象
         int mColorSweep[] = {Color.GREEN, Color.RED, Color.BLUE, Color.YELLOW, Color.GREEN};
         mSweepGradient = new SweepGradient(540, 750, mColorSweep, null);    //第一個參數是說你要     從x軸的什麼位置開始漸層,  第二個是y軸 ,第三個是漸層顏數組,第四個是位置,可以指定漸層的絕對位置
     }
 
     public void onDraw(Canvas canvas) {
         super.onDraw(canvas);
         
         Paint mPaint = new Paint();
         canvas.drawColor(Color.WHITE);      //背景置為灰色
         float scale = 1.0f;
         Matrix matrix =new Matrix();
      // 拿到bitmap寬或高的小值
         int bSize = Math.min(mBitmap.getWidth(), mBitmap.getHeight());
         scale = 500 * 1.0f / bSize;
          
         matrix.setScale(scale, scale);
         mBitmapShader.setLocalMatrix(matrix);
         //繪製Bitmap渲染的橢圓
         mPaint.setShader(mBitmapShader);
       
         canvas.drawRoundRect(new RectF(20, 20, mBitmap.getWidth()*3/4,
                mBitmap.getHeight()*3/4),200,200, mPaint);
 
         //繪製線性漸層的矩形
         mPaint.setShader(mLinearGradient);
         canvas.drawRect(10, mBitmap.getHeight()*3/4, 250,mBitmap.getHeight()*3/4+100, mPaint);
         
        //繪製環形漸層的圓
        mPaint.setShader(mRadialGradient);
         canvas.drawCircle(350, mBitmap.getHeight()*3/4+75, 75, mPaint);
        
         //繪製混合漸層(線性與環形混合)的矩形
         mPaint.setShader(mComposeShader);
        canvas.drawRect(430, 800, 600, 1000, mPaint);
//                 
         //繪製梯形漸層的矩形
         mPaint.setShader(mSweepGradient);
         canvas.drawRect(430, 700, 630,800, mPaint);
     }    
 }

demo:http://download.csdn.net/detail/u012808234/8597197

android的shader渲染器

聯繫我們

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