在實際的開發工程中,不免想有一箇中間是空洞的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能在緩存中進行正常計算,在釋放鎖後進行渲染,所以請不要關閉硬件加速功能。
顯示效果
上圖的佈局文件是
<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">
<!-- 根據這個子View所在的位置,計算出透明矩形的位置,開發時的所見即所得 -->
<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;
/**
* 說明:支持中間出現透明區域的drawable <br/>
* 通過{@link #setSrcPath(Path)}設定透明區域的形狀 <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);
}
/**
* 設置內部透明的部分
*
* @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來實現下剛纔的圖例中的效果
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/>
* 根據個crop_image_cover_view_hole子View的位置,確定透明區域 <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爲範圍構造需要透明顯示的區域
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);
}
}
}