一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Android自定义Drawable之在Drawable中部指定透明区域方法示例

时间:2018-07-31 编辑:猪哥 来源:一聚教程网

前言

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能在缓存中进行正常计算,在释放锁后进行渲染,所以请不要关闭硬件加速功能。

显示效果

上图的布局文件是



 

 

  
  

 

完整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 
* 通过{@link #setSrcPath(Path)}设定透明区域的形状
* 作者:杨健 * 时间: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处理成带洞洞的效果 
* 当然了,形状要你自己指定,目前想不到好的思路自动处理各种形状,有的话就直接完善了
* 根据个crop_image_cover_view_hole子View的位置,确定透明区域
* 作者:杨健 * 时间: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); } } }

热门栏目