美文网首页Android SAPT
仿微信聊天气泡 图片尖角 按下变暗

仿微信聊天气泡 图片尖角 按下变暗

作者: GuoBangbang | 来源:发表于2016-10-25 14:06 被阅读250次

    实现微信气泡图片尖角

    //-------------gen corner bitmap flow------------------------
    //load the bg: .9.png which is with cornor.
    Bitmap bg = BitmapFactory.decodeResource(getResources(), R.drawable.chat_bg);
    //combine bg and  the in bitmap ,size can be resized by custom.
    public Bitmap getRoundCornerImage(Bitmap bg, Bitmap in) {
        Bitmap roundConcerImage = Bitmap.createBitmap(200, 200, Config.ARGB_8888);
        Canvas canvas = new Canvas(roundConcerImage);
        Paint paint = new Paint();
        Rect rect = new Rect(0, 0, 200, 200);
        Rect rectF = new Rect(0, 0, in.getWidth(), in.getHeight());
        paint.setAntiAlias(true);
        NinePatch patch = new NinePatch(bg, bg.getNinePatchChunk(), null);
        patch.draw(canvas, rect);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(in, rectF, rect, paint);
        return roundConcerImage;
    }
    //load the image to be shown as bitmap in.
    Bitmap bp = getRoundCornerImage(bg,in);
    //set bitmap to the imageview.
    iv.setImageBitmap(bp);
    //----------------finish---------------------
    

    图片按下变暗效果:

    /**
     * this is just a sample.
     * u can overide the imageview's onTouchEvent() method.
     **/
    iv.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View arg0, MotionEvent arg1) {
                    // TODO Auto-generated method stub
                    if (arg1.getActionMasked() == MotionEvent.ACTION_DOWN) {
                        Drawable drawable = iv.getDrawable();
                        drawable.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
                    }
                    if (arg1.getActionMasked() == MotionEvent.ACTION_UP) {
                        Drawable drawable = iv.getDrawable();
                        drawable.clearColorFilter();
                    }
                    return false;
                }
            });
    

    simple and practical.

    相关文章

      网友评论

        本文标题:仿微信聊天气泡 图片尖角 按下变暗

        本文链接:https://www.haomeiwen.com/subject/iguruttx.html