日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

【Android 应用开发】Paint 渲染 之 BitmapShader 位图渲染 ( 渲染流程 | CLAMP 拉伸最后像素 | REPEAT 重复绘制图片 | MIRROR 绘制反向图片 )

發(fā)布時(shí)間:2025/6/17 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Android 应用开发】Paint 渲染 之 BitmapShader 位图渲染 ( 渲染流程 | CLAMP 拉伸最后像素 | REPEAT 重复绘制图片 | MIRROR 绘制反向图片 ) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • 1. 位圖渲染 BitmapShader 簡介
      • ( 1 ) 位圖渲染綜述 ( ① 三種方式 : Shader.TileMode.CLAMP | Shader.TileMode.REPEAT | Shader.TileMode.MIRROR | ② 流程 : 創(chuàng)建 Shader | 設(shè)置 Shader 到 Paint | 打開抗鋸齒 | 繪制矩形 )
    • 2. 位圖渲染 BitmapShader 三種參數(shù) 及 代碼示例
      • ( 1 ) 位圖渲染 CLAMP 拉伸 代碼示例 及 效果 ( 繪制超出圖片邊界時(shí), 就會(huì)繪制 水平 或 垂直方向 上最后一個(gè)像素, 填充剩余的位置 )
      • ( 2 ) 位圖渲染 REPEAT 拉伸 代碼示例 及 效果 ( 繪制超出圖片邊界時(shí), 就會(huì)繪制 同樣的圖片 填充剩余部分 )
      • ( 3 ) 位圖渲染 MIRROR 拉伸 代碼示例 及 效果 ( 在垂直和水平方向繪制圖片的對應(yīng)方向的反向圖片 )



相關(guān)代碼地址 :

  • 1.GitHub 項(xiàng)目展示地址 : UI_Demos_4_CSDN_Blog
  • 2.本博客相關(guān)代碼地址 :
    • ① CLAMP 渲染 : PaintBitmapShaderClamp.java
    • ② REPEAT 渲染 : PaintBitmapShaderRepeat.java
    • ③ MIRROR 渲染 : PaintBitmapShaderMirror.java



1. 位圖渲染 BitmapShader 簡介



( 1 ) 位圖渲染綜述 ( ① 三種方式 : Shader.TileMode.CLAMP | Shader.TileMode.REPEAT | Shader.TileMode.MIRROR | ② 流程 : 創(chuàng)建 Shader | 設(shè)置 Shader 到 Paint | 打開抗鋸齒 | 繪制矩形 )


位圖渲染 :

  • 1.主要實(shí)現(xiàn)的功能 : 位圖渲染就是 將一個(gè)位圖, 通過特定的方式繪制到指定的矩形區(qū)域中, 解決 Bitmap 位圖的寬高 與 繪制區(qū)域?qū)捀?不一致時(shí)如何進(jìn)行渲染 的 問題;
  • 2.渲染流程 :
    • ① 創(chuàng)建 BitmapShader
    • ② 為 Paint 設(shè)置 著色器 Shader
    • ③ 打開抗鋸齒
    • ④ 繪制一個(gè)矩形區(qū)域
  • 3.創(chuàng)建 BitmapShader : 調(diào)用 BitmapShader 構(gòu)造方法創(chuàng)建著色器, 同時(shí) 設(shè)置 位圖引用, 和 繪制位圖時(shí) 的 X 和 Y 方向的拉伸方式 , 位圖的 拉伸方式 在后面有介紹 , 下面是三個(gè)參數(shù)說明 :
    • ① Bitmap bitmap 參數(shù) : 渲染所用的位圖 ;
    • ② TileMode tileX 參數(shù) : 設(shè)置繪制位圖時(shí)的 x 方向的拉伸方式 ;
    • ③ TileMode tileY : 設(shè)置繪制位圖時(shí)的 y 方向的拉伸方式 ;
/*** 調(diào)用該構(gòu)造函數(shù)創(chuàng)建一個(gè)新的著色器, 用于繪制位圖** @param bitmap 將要繪制的位圖* @param tileX 設(shè)置繪制位圖時(shí)的 x 方向的拉伸方式* @param tileY 設(shè)置繪制位圖時(shí)的 y 方向的拉伸方式*/public BitmapShader(@NonNull Bitmap bitmap, @NonNull TileMode tileX, @NonNull TileMode tileY) {this(bitmap, tileX.nativeInt, tileY.nativeInt);}
  • 4.三種位圖的拉伸方式 :
    • ① Shader.TileMode.CLAMP : 如果繪制的位置超出了圖像的邊界, 那么超出部分 使用最后一個(gè)像素的顏色值繪制 ;
    • ② Shader.TileMode.REPEAT : 繪圖位置超出了邊界, 使用 同樣的位圖進(jìn)行平鋪 剩余繪制的部分;
    • ③ Shader.TileMode.MIRROR : 繪圖位置超出了邊界, 使用 位圖反轉(zhuǎn)鏡像 平鋪剩余繪制部分;
public class Shader {...public enum TileMode {/*** replicate the edge color if the shader draws outside of its* original bounds*/CLAMP (0),/*** repeat the shader's image horizontally and vertically*/REPEAT (1),/*** repeat the shader's image horizontally and vertically, alternating* mirror images so that adjacent images always seam*/MIRROR (2);TileMode(int nativeInt) {this.nativeInt = nativeInt;}final int nativeInt;}... }
  • 5.為 畫筆 設(shè)置 著色器 : 調(diào)用 Paint 對象 的 setShader 方法為 畫筆設(shè)置 著色器;
  • 6.打開抗鋸齒 : 調(diào)用 Paint 對象的 setAntiAlias 方法, 打開抗鋸齒, 這樣 位圖的邊界會(huì)更平滑, paint.setAntiAlias(true) ;
  • 7.繪制矩形 : 調(diào)用 Canvas 的 drawRect 的方法, 繪制矩形, 位圖在該矩形中繪制; canvas.drawRect(new Rect(0,0 , 100, 100),mPaint); ;
  • 8.使用示例 : 下面是位圖渲染 的簡單示例;
@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);canvas.drawColor(Color.WHITE);Paint mPaint = new Paint();Bitmap mBitmap = ((BitmapDrawable)getResources().getDrawable(R.mipmap.aodesai)).getBitmap();//1. 創(chuàng)建位圖渲染對象, 并設(shè)置拉伸方式, 此處設(shè)置Shader.TileMode.CLAMP,// 如果繪制的位置超出了圖像的邊界, 那么超出部分 使用最后一個(gè)像素的顏色值繪制BitmapShader bitmapShader = new BitmapShader(mBitmap,Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);//2. 設(shè)置渲染到 Paint 對象mPaint.setShader(bitmapShader);//3. 打開抗鋸齒mPaint.setAntiAlias(true);//4. 繪制指定的矩形區(qū)域canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);}



2. 位圖渲染 BitmapShader 三種參數(shù) 及 代碼示例



( 1 ) 位圖渲染 CLAMP 拉伸 代碼示例 及 效果 ( 繪制超出圖片邊界時(shí), 就會(huì)繪制 水平 或 垂直方向 上最后一個(gè)像素, 填充剩余的位置 )


CLAMP 拉伸 :

  • 1.CLAMP 說明 : 在創(chuàng)建 BitmapShader 的時(shí)候, 設(shè)置其 水平 和 垂直方向的 拉伸方式為 Shader.TileMode.CLAMP , 則在繪制超出圖片邊界時(shí), 就會(huì)繪制 水平 或 垂直方向 上最后一個(gè)像素, 填充剩余的位置 ;
  • 2.展示效果 :
  • 2.代碼示例 :
package com.hanshuliang.shader.widget;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.Paint; import android.graphics.PorterDuff; import android.graphics.Shader; import android.graphics.drawable.BitmapDrawable; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View;import com.hanshuliang.shader.R;public class PaintBitmapShaderClamp extends View {public PaintBitmapShaderClamp(Context context) {super(context);}public PaintBitmapShaderClamp(Context context, @Nullable AttributeSet attrs) {super(context, attrs);}public PaintBitmapShaderClamp(Context context, @Nullable AttributeSet attrs,int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);canvas.drawColor(Color.WHITE);Paint mPaint = new Paint();Bitmap mBitmap = ((BitmapDrawable)getResources().getDrawable(R.mipmap.aodesai)).getBitmap();//1. 創(chuàng)建位圖渲染對象, 并設(shè)置拉伸方式, 此處設(shè)置Shader.TileMode.CLAMP,// 如果繪制的位置超出了圖像的邊界, 那么超出部分 使用最后一個(gè)像素的顏色值繪制BitmapShader bitmapShader = new BitmapShader(mBitmap,Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);//2. 設(shè)置渲染到 Paint 對象mPaint.setShader(bitmapShader);//3. 打開抗鋸齒mPaint.setAntiAlias(true);//4. 繪制指定的矩形區(qū)域canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);} }


( 2 ) 位圖渲染 REPEAT 拉伸 代碼示例 及 效果 ( 繪制超出圖片邊界時(shí), 就會(huì)繪制 同樣的圖片 填充剩余部分 )


REPEAT 拉伸 :

  • 1.REPEAT 說明 : 在創(chuàng)建 BitmapShader 的時(shí)候, 設(shè)置其 水平 和 垂直方向的 拉伸方式為 Shader.TileMode.REPEAT , 則在繪制超出圖片邊界時(shí), 就會(huì)繪制 同樣的圖片 填充剩余部分 ;
  • 2.展示效果 :
  • 2.代碼示例 :
package com.hanshuliang.shader.widget;import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Shader; import android.graphics.drawable.BitmapDrawable; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View;import com.hanshuliang.shader.R;public class PaintBitmapShaderRepeat extends View {public PaintBitmapShaderRepeat(Context context) {super(context);}public PaintBitmapShaderRepeat(Context context, @Nullable AttributeSet attrs) {super(context, attrs);}public PaintBitmapShaderRepeat(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);canvas.drawColor(Color.WHITE);Paint mPaint = new Paint();Bitmap mBitmap = ((BitmapDrawable)getResources().getDrawable(R.mipmap.aodesai)).getBitmap();//1. 創(chuàng)建位圖渲染對象, 并設(shè)置拉伸方式, 此處設(shè)置Shader.TileMode.CLAMP, // 如果繪制的位置超出了圖像的邊界, 使用平鋪方式填充BitmapShader bitmapShader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);//2. 設(shè)置渲染到 Paint 對象mPaint.setShader(bitmapShader);//3. 打開抗鋸齒mPaint.setAntiAlias(true);//4. 繪制指定的矩形區(qū)域canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);} }


( 3 ) 位圖渲染 MIRROR 拉伸 代碼示例 及 效果 ( 在垂直和水平方向繪制圖片的對應(yīng)方向的反向圖片 )


MIRROR 拉伸 :

  • 1.MIRROR 說明 : 在創(chuàng)建 BitmapShader 的時(shí)候, 設(shè)置其 水平 和 垂直方向的 拉伸方式為 Shader.TileMode.MIRROR , 則在繪制超出圖片邊界時(shí), 就會(huì)繪制 圖片的 鏡像翻轉(zhuǎn)方式 鋪滿剩余部分;
  • 2.展示效果 :
  • 2.代碼示例 :
package com.hanshuliang.shader.widget;import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Shader; import android.graphics.drawable.BitmapDrawable; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View;import com.hanshuliang.shader.R;public class PaintBitmapShaderMirror extends View {public PaintBitmapShaderMirror(Context context) {super(context);}public PaintBitmapShaderMirror(Context context, @Nullable AttributeSet attrs) {super(context, attrs);}public PaintBitmapShaderMirror(Context context, @Nullable AttributeSet attrs,int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);canvas.drawColor(Color.WHITE);Paint mPaint = new Paint();Bitmap mBitmap = ((BitmapDrawable)getResources().getDrawable(R.mipmap.aodesai)).getBitmap();//1. 創(chuàng)建位圖渲染對象, 并設(shè)置拉伸方式, 此處設(shè)置Shader.TileMode.CLAMP,// 如果繪制的位置超出了圖像的邊界, 那么超出部分 使用鏡像平鋪方式填充BitmapShader bitmapShader = new BitmapShader(mBitmap,Shader.TileMode.MIRROR, Shader.TileMode.MIRROR);//2. 設(shè)置渲染到 Paint 對象mPaint.setShader(bitmapShader);//3. 打開抗鋸齒mPaint.setAntiAlias(true);//4. 繪制指定的矩形區(qū)域canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);} }

相關(guān)代碼地址 :

  • 1.GitHub 項(xiàng)目展示地址 : UI_Demos_4_CSDN_Blog
  • 2.本博客相關(guān)代碼地址 :
    • ① CLAMP 渲染 : PaintBitmapShaderClamp.java
    • ② REPEAT 渲染 : PaintBitmapShaderRepeat.java
    • ③ MIRROR 渲染 : PaintBitmapShaderMirror.java

總結(jié)

以上是生活随笔為你收集整理的【Android 应用开发】Paint 渲染 之 BitmapShader 位图渲染 ( 渲染流程 | CLAMP 拉伸最后像素 | REPEAT 重复绘制图片 | MIRROR 绘制反向图片 )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。