国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android自定義Drawable之在Drawable中部指定透明區(qū)域方法示例

2019-10-21 21:44:55
字體:
供稿:網(wǎng)友

前言

Drawable是什么?

  • 一種可以在Canvas上進行繪制的抽象的概念
  • 顏色、圖片等都可以是一個Drawable
  • Drawable可以通過XML定義,或者通過代碼創(chuàng)建
  • Android中Drawable是一個抽象類,每個具體的Drawable都是其子類

Drawable的優(yōu)點

  • 使用簡單,比自定義View成本低
  • 非圖片類的Drawable所占空間小,能減小apk大小

在實際的開發(fā)工程中,不免想有一個中間是空洞的Drawable,也就是中間是透明的,而其他區(qū)域正常顯示的Drawable。

  • 主要用到的技術是PorterDuffXfermode的PorterDuff.Mode.XOR模式
  • 核心思想是先正常繪制出整個drawable,然后將指定的區(qū)域混合成透明色

看下主要代碼代碼

public void draw(@NonNull Canvas canvas) { //將繪制操作保存到新的圖層,因為圖像合成是很昂貴的操作,將用到硬件加速,這里將圖像合成的處理放到離屏緩存中進行 int saveCount = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), srcPaint, Canvas.ALL_SAVE_FLAG); //dst 繪制目標圖層 innerDrawable.draw(canvas); //設置混合模式 srcPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); //src 繪制源圖 canvas.drawPath(srcPath, srcPaint); //清除混合模式 srcPaint.setXfermode(null); //還原畫布 canvas.restoreToCount(saveCount);}

在上面的代碼中,有的人可能認為需要關閉硬件加速,即

setLayerType(View.LAYER_TYPE_SOFTWARE, null);

但在實際的操作中,采用鎖定canvas的方式能有效的避免硬件加速同步所造成的不正常顯示,而且鎖定的canvas能在緩存中進行正常計算,在釋放鎖后進行渲染,所以請不要關閉硬件加速功能。

顯示效果

Android,Drawable,透明區(qū)域

上圖的布局文件是

<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView  android:layout_width="match_parent"  android:layout_height="match_parent"  android:background="@drawable/crop_image_bg" /> <com.jian.cropimage.CoverView  android:layout_width="match_parent"  android:layout_height="match_parent"  android:background="@color/crop_image_cover_view_bg">  <!-- 根據(jù)這個子View所在的位置,計算出透明矩形的位置,開發(fā)時的所見即所得 -->  <ImageView   android:id="@+id/crop_image_cover_view_hole"   android:layout_width="250dp"   android:layout_height="250dp"   android:layout_gravity="center" /> </com.jian.cropimage.CoverView></FrameLayout>

完整HoleDrawable代碼

import android.graphics.Canvas;import android.graphics.ColorFilter;import android.graphics.Paint;import android.graphics.Path;import android.graphics.PorterDuff;import android.graphics.PorterDuffXfermode;import android.support.annotation.NonNull;import android.support.annotation.Nullable;/** * 說明:支持中間出現(xiàn)透明區(qū)域的drawable <br/> * 通過{@link #setSrcPath(Path)}設定透明區(qū)域的形狀 <br/> * 作者:楊健 * 時間:2017/9/4. */public class HoleDrawable extends Drawable { private Paint srcPaint; private Path srcPath = new Path(); private Drawable innerDrawable; public HoleDrawable(Drawable innerDrawable) {  this.innerDrawable = innerDrawable;  srcPath.addRect(100, 100, 200, 200, Path.Direction.CW);  srcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  srcPaint.setColor(0xffffffff); } /**  * 設置內(nèi)部透明的部分  *  * @param srcPath  */ public void setSrcPath(Path srcPath) {  this.srcPath = srcPath; } @Override public void draw(@NonNull Canvas canvas) {  innerDrawable.setBounds(getBounds());  if (srcPath == null || srcPath.isEmpty()) {   innerDrawable.draw(canvas);  } else {   //將繪制操作保存到新的圖層,因為圖像合成是很昂貴的操作,將用到硬件加速,這里將圖像合成的處理放到離屏緩存中進行   int saveCount = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), srcPaint, Canvas.ALL_SAVE_FLAG);   //dst 繪制目標圖   innerDrawable.draw(canvas);   //設置混合模式   srcPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));   //src 繪制源圖   canvas.drawPath(srcPath, srcPaint);   //清除混合模式   srcPaint.setXfermode(null);   //還原畫布   canvas.restoreToCount(saveCount);  } } @Override public void setAlpha(int alpha) {  innerDrawable.setAlpha(alpha); } @Override public void setColorFilter(@Nullable ColorFilter colorFilter) {  innerDrawable.setColorFilter(colorFilter); } @Override public int getOpacity() {  return innerDrawable.getOpacity(); }}

光有HoleDrawable是沒有意義的,寫個自定義View來實現(xiàn)下剛才的圖例中的效果

import android.content.Context;import android.graphics.Path;import android.graphics.drawable.HoleDrawable;import android.support.annotation.NonNull;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.view.View;import android.widget.FrameLayout;/** * 能夠局部透明的layout,也就是將background處理成帶洞洞的效果 <br/> * 當然了,形狀要你自己指定,目前想不到好的思路自動處理各種形狀,有的話就直接完善了 <br/> * 根據(jù)個crop_image_cover_view_hole子View的位置,確定透明區(qū)域 <br/> * 作者:楊健 * 時間:2017/9/4. */public class HoleBackgroundLayout extends FrameLayout { private HoleDrawable background; public HoleBackgroundLayout(@NonNull Context context) {  super(context);  initView(context, null, 0); } public HoleBackgroundLayout(@NonNull Context context, @Nullable AttributeSet attrs) {  super(context, attrs);  initView(context, attrs, 0); } public HoleBackgroundLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {  super(context, attrs, defStyleAttr);  initView(context, attrs, defStyleAttr); } private void initView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {  background = new HoleDrawable(getBackground());  setBackground(background); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) {  super.onLayout(changed, left, top, right, bottom);  resetBackgroundHoleArea(); } private void resetBackgroundHoleArea() {  Path path = null;  // 以crop_image_cover_view_hole子View為范圍構造需要透明顯示的區(qū)域  View v0 = findViewById(R.id.crop_image_cover_view_hole);  if (v0 != null) {   path = new Path();   // 矩形透明區(qū)域   path.addRect(v0.getLeft(), v0.getTop(), v0.getRight(), v0.getBottom(), Path.Direction.CW);  }  if (path != null) {   background.setSrcPath(path);  } }}

總結

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網(wǎng)的支持。


注:相關教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 栖霞市| 黄陵县| 永宁县| 东光县| 墨竹工卡县| 高邑县| 曲沃县| 黑龙江省| 泽州县| 大冶市| 宁乡县| 饶河县| 昌都县| 女性| 襄垣县| 台湾省| 大关县| 荣昌县| 上虞市| 朝阳市| 外汇| 饶平县| 徐闻县| 昂仁县| 涪陵区| 泰顺县| 华容县| 巩留县| 克什克腾旗| 尚志市| 靖江市| 邓州市| 株洲县| 修水县| 洪江市| 三门峡市| 霍林郭勒市| 梅州市| 武山县| 巴中市| 桃园市|