Android MaskImage 微信发送图片效果

</pre><pre code_snippet_id="411661" snippet_file_name="blog_20140630_1_1574839" name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif;">public class MaskImage extends ImageView {</span>
    int screenWidth = 0;
    int screenHeight = 0;
    Bitmap original = null;
    TypedArray a;
    boolean sendMessage = false;
    int maskSoure = 0;
    int maskClickSource = 0;
    private boolean clickImage = false; // 是否点击图片
    int defaultImgRes = 0;

    public MaskImage(Context context) {
        super(context);
    }

    public MaskImage(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MaskImage(Context context, AttributeSet attrs) {
        super(context, attrs);

        a = context.getTheme().obtainStyledAttributes(
                attrs,
                AndroidUtil.getResourceDeclareStyleableIntArray(context,
                        "ImImageView"), 0, 0);
        sendMessage = a.getBoolean(AndroidUtil.getStyleableResourceInt(context,
                "ImImageView_sendMessage"), false);
        defaultImgRes = a.getResourceId(AndroidUtil.getStyleableResourceInt(
                context, "ImImageView_defaultImgRes"), 0);
    }

    public void sendOrFrom() {
        if (sendMessage) {
            maskSoure = R.drawable.bg_msg_pop_orange;
            maskClickSource = R.drawable.jkt_chat_mytextbg_hig_active;
        } else {
            maskSoure = R.drawable.bg_msg_pop_gray;
            maskClickSource = R.drawable.jkt_chat_textbg_hig_active;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            clickImage = true;
            invalidate();
            Log.d("debug", "on down");
        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
            Log.d("debug", "on up");
            clickImage = false;
            invalidate();
        }
        if (event.getAction() == MotionEvent.ACTION_CANCEL) {
            Log.d("debug", "on cancel");
            clickImage = false;
            invalidate();
        }

        return super.onTouchEvent(event);
    }
    
    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        // 判断图片是否为空
        if (drawable != null && drawable instanceof BitmapDrawable) {
            original = ((BitmapDrawable) drawable).getBitmap();
            // 转换为Bitmap的是否为Null
            
            if (original != null) {
                if (original.getWidth() != this.getWidth()
                        || original.getHeight() != this.getHeight()) {
                    original = zoomImg(original, this.getWidth(), this.getHeight());
                }
                int x = 0;
                int y = 0;
                int sc = canvas.saveLayer(x, y, x + this.getWidth(),
                        y + this.getHeight(), 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);
                Rect rf = new Rect(0, 0, this.getWidth(), this.getHeight());
                // 显示模型样式 发送遮罩 接受遮罩
                sendOrFrom();
                // .9图片改变大小
                NinePatchDrawable npd = (NinePatchDrawable) getResources()
                        .getDrawable(maskSoure);
                npd.setBounds(rf);
                // 将遮罩图片绘制到画板
                npd.draw(canvas);
                Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
                // 图片交接处显示之前的样式
                paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
                canvas.drawBitmap(original, 0, 0, paint);
                // 图片点击 添加一层图片 实现点击图片切换样式
                if (clickImage) {
                    paint.setXfermode(new PorterDuffXfermode(
                            PorterDuff.Mode.XOR));
                    NinePatchDrawable click = (NinePatchDrawable) getResources()
                            .getDrawable(maskClickSource);
                    click.setBounds(rf);
                    click.draw(canvas);
                }
                canvas.restoreToCount(sc);

            }
        }

        if (a != null) {
            a.recycle();
        }
    }

    // 缩放图片
    public static Bitmap zoomImg(Bitmap bm, int newWidth, int newHeight) {
        // 获得图片的宽高
        int width = bm.getWidth();
        int height = bm.getHeight();
        // 计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
                true);
        return newbm;
    }
}

Demo 源码地址:https://github.com/zhaocheng19940105/MaskImage

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值