前言
Drawable是什麼?
一種可以在Canvas上進行繪制的抽象的概念
顔色、圖檔等都可以是一個Drawable
Drawable可以通過XML定義,或者通過代碼建立
Android中Drawable是一個抽象類,每個具體的Drawable都是其子類
Drawable的優點
使用簡單,比自定義View成本低
非圖檔類的Drawable所占空間小,能減小apk大小
在實際的開發工程中,不免想有一個中間是空洞的Drawable,也就是中間是透明的,而其他區域正常顯示的Drawable。
主要用到的技術是PorterDuffXfermode的PorterDuff.Mode.XOR模式
核心思想是先正常繪制出整個drawable,然後将指定的區域混合成透明色
看下主要代碼代碼
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:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/crop_image_bg" />
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/crop_image_cover_view_bg">
android:id="@+id/crop_image_cover_view_hole"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center" />
完整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;
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);
}
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來實作下剛才的圖例中的效果
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;
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為範圍構造需要透明顯示的區域
View v0 = findViewById(R.id.crop_image_cover_view_hole);
if (v0 != null) {
path = new Path();
// 矩形透明區域
path.addRect(v0.getLeft(), v0.getTop(), v0.getRight(), v0.getBottom(), Path.Direction.CW);
}
if (path != null) {
background.setSrcPath(path);
}
}
}
總結
以上就是這篇文章的全部内容了,希望本文的内容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支援。