Android 顏色渲染(四) BitmapShader位元影像渲染

來源:互聯網
上載者:User

標籤:

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

Android 顏色處理(四) BitmapShader位元影像渲染

 

 

public   BitmapShader(Bitmap bitmap,Shader.TileMode tileX,Shader.TileMode tileY)

調用這個方法來產生一個畫有一個位元影像的渲染器(Shader)。

bitmap   在渲染器內使用的位元影像

tileX      The tiling mode for x to draw the bitmap in.   在位元影像上X方向渲染器平鋪模式

tileY     The tiling mode for y to draw the bitmap in.    在位元影像上Y方向渲染器平鋪模式

TileMode:

CLAMP  :如果渲染器超出原始邊界範圍,會複製範圍內邊緣染色。

REPEAT :橫向和縱向的重複渲染器圖片,平鋪。

MIRROR :橫向和縱向的重複渲染器圖片,這個和REPEAT重複方式不一樣,他是以鏡像方式平鋪。

 

首先看一下:

 

                                                                                                           

還是直接上代碼:

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 BitmapShaderView shaderView;  
  9.       
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.   
  14.         shaderView = new BitmapShaderView(this);  
  15.         setContentView(shaderView);  
  16.   
  17.     }  
  18.   
  19. }  


BitmapShaderView:

[java] view plain copy
    1. package com.tony.shader;  
    2.   
    3. import android.content.Context;  
    4. import android.graphics.Bitmap;  
    5. import android.graphics.BitmapShader;  
    6. import android.graphics.Canvas;  
    7. import android.graphics.Paint;  
    8. import android.graphics.Shader;  
    9. import android.graphics.drawable.BitmapDrawable;  
    10. import android.graphics.drawable.ShapeDrawable;  
    11. import android.graphics.drawable.shapes.OvalShape;  
    12. import android.util.AttributeSet;  
    13. import android.view.View;  
    14.   
    15. public class BitmapShaderView extends View {  
    16.   
    17.     private BitmapShader bitmapShader = null;  
    18.     private Bitmap bitmap = null;  
    19.     private Paint paint = null;  
    20.     private ShapeDrawable shapeDrawable = null;  
    21.     private int BitmapWidth = 0;  
    22.     private int BitmapHeight = 0;  
    23.   
    24.     public BitmapShaderView(Context context) {  
    25.         super(context);  
    26.   
    27.         // 得到映像  
    28.         bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.cat))  
    29.                 .getBitmap();  
    30.         BitmapWidth = bitmap.getWidth();  
    31.         BitmapHeight = bitmap.getHeight();  
    32.         // 構造渲染器BitmapShader  
    33.         bitmapShader = new BitmapShader(bitmap, Shader.TileMode.MIRROR,Shader.TileMode.REPEAT);  
    34.     }  
    35.       
    36.     public BitmapShaderView(Context context,AttributeSet set) {  
    37.         super(context, set);  
    38.     }  
    39.       
    40.       
    41.     @Override  
    42.     protected void onDraw(Canvas canvas) {  
    43.         // TODO Auto-generated method stub  
    44.     super.onDraw(canvas);  
    45.     //將圖片裁剪為橢圓形      
    46.         //構建ShapeDrawable對象並定義形狀為橢圓      
    47.         shapeDrawable = new ShapeDrawable(new OvalShape());    
    48.         //得到畫筆並設定渲染器    
    49.         shapeDrawable.getPaint().setShader(bitmapShader);    
    50.         //設定顯示地區    
    51.         shapeDrawable.setBounds(20, 20,BitmapWidth-140,BitmapHeight);    
    52.         //繪製shapeDrawable    
    53.         shapeDrawable.draw(canvas);    
    54.     }  
    55.   

Android 顏色渲染(四) BitmapShader位元影像渲染

聯繫我們

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