不规则的Imageview的实现

效果

QQ截图20170213182929.png

核心思路:Xfermode的使用,两个图层一个为实际图片。一个为需要显示部分透明的图。
开始想可能得自定义view,感觉蛮复杂的。最后发现可以简单点。用UI切个图。简单继承下ImageView就行了。
算了不说了贴代码了

 /**Created by lps on 2017/2/13. */

 public class IrregularImageView extends ImageView {
 private Context mContext;
 private Paint mPaint;
 Bitmap dst;
private int mWidth;
 private int mHeight;

public IrregularImageView(Context context) {
   this(context, null);
}

public IrregularImageView(Context context, AttributeSet attrs) {
   this(context, attrs, 0);
}

public IrregularImageView(Context context, AttributeSet attrs, int defStyleAttr) {
   super(context, attrs, defStyleAttr);
    mContext = context;
  mPaint = new Paint();
   mPaint.setAntiAlias(true);
}

 @Override
protected void onDraw(Canvas canvas) {
    if (getDrawable() == null || dst == null) return;//如果没有设置图片就不往下绘制了
     mWidth = getWidth();
     mHeight = getHeight();
   int sc = canvas.saveLayer(0, 0, mWidth, mHeight, null, Canvas.MATRIX_SAVE_FLAG |
             Canvas.CLIP_SAVE_FLAG |
            Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
            Canvas.FULL_COLOR_LAYER_SAVE_FLAG |
            Canvas.CLIP_TO_LAYER_SAVE_FLAG);


    // 先画一个图片
    Bitmap mask = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.irregular);
    mask = calculateMask(mask, mWidth, mHeight);
    canvas.drawBitmap(dst, 0, 0, mPaint);
    // 设置模式
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
    canvas.drawBitmap(mask, 0, 0, mPaint);
    // 还原混合模式
    mPaint.setXfermode(null);
   // 还原画布
    canvas.restoreToCount(sc);
}

private Bitmap calculateMask(Bitmap mMask, int mWidth, int mHeight) {
   Matrix mMatrix = new Matrix();
  float scaleX = mWidth * 1.0f / (mMask.getWidth() * 1.0f);
   float scaleY = mHeight * 1.0f / (mMask.getHeight() * 1.0f);
   mMatrix.postScale(scaleX, scaleY);
   Bitmap mBitmap = Bitmap.createBitmap(mMask, 0, 0, mMask.getWidth(), mMask.getHeight(), mMatrix, true);
     return mBitmap;
 }


 /**     * 以下四个函数都是
 * 复写ImageView的setImageXxx()方法
* 注意这个函数先于构造函数调用之前调用
* @param bm*/

@Override
public void setImageBitmap(Bitmap bm) {
     super.setImageBitmap(bm);
     dst = bm;
     setup();

 }


@Override
 public void setImageDrawable(Drawable drawable) {
     super.setImageDrawable(drawable);
     dst = getBitmapFromDrawable(drawable);
     System.out.println("setImageDrawable -- setup");
     setup();
 }

 @Override
 public void setImageResource(@DrawableRes int resId) {
     super.setImageResource(resId);
     dst = getBitmapFromDrawable(getDrawable());
     setup();
 }

 @Override
 public void setImageURI(Uri uri) {
     super.setImageURI(uri);
     dst = getBitmapFromDrawable(getDrawable());
     setup();
 }

 /**
  * Drawable转Bitmap
  *
  * @param drawable 因为用Glide做的图片加载。有一些Glide影响的因素,这个方法可能还有待完善
  * @return
  */
 private Bitmap getBitmapFromDrawable(Drawable drawable) {
     if (drawable == null) {
         return null;
     }

     if (drawable instanceof BitmapDrawable) {
         
         return ((BitmapDrawable) drawable).getBitmap();
    }
     if (drawable instanceof TransitionDrawable) {
         GlideBitmapDrawable mDrawableByLayerId = (GlideBitmapDrawable) ((TransitionDrawable) (drawable)).findDrawableByLayerId(((TransitionDrawable) drawable).getId(1));
        return mDrawableByLayerId.getBitmap();
     }
    if (drawable instanceof GlideBitmapDrawable) {
         return ((GlideBitmapDrawable) drawable).getBitmap();
     }

     return null;
 }

 private void setup() {
     if (dst == null) return;
     invalidate();
 }}

最后附上UI妹子给的图。

QQ截图20170213211325.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 178,584评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,927评论 4 61
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,257评论 5 13
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,685评论 6 30
  • 周回顾
    星子CX阅读 162评论 0 0

友情链接更多精彩内容