如何实现textview右下角角标变成圆角呢

冰冰的冻结
  • 519
private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(strokeColor);
        mPaint.setStrokeWidth(strokeWidth);

        mPaintLabel = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintLabel.setStyle(Paint.Style.STROKE);

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_support_select);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = getWidth();
        int height = getHeight();

        //http://blog.csdn.net/kuaiguixs/article/details/78753149
        float offset = strokeWidth / 2;

        canvas.drawColor(solidColor);
        RectF rec = new RectF(offset, offset, width - offset, height - offset);
        if (isSelect) {

            canvas.drawRoundRect(rec, cornersRadius, cornersRadius, mPaint);

            canvas.drawBitmap(bitmap, getWidth() - labelWidth, getHeight() - labelHeight, mPaintLabel);

        } else {
            canvas.drawRoundRect(rec, cornersRadius, cornersRadius, mPaint);
        }
    }

clipboard.png

回复
阅读 4.7k
4 个回答
private Bitmap roundBitmapByXfermode(Bitmap bitmap, int outWidth, int outHeight, int radius) {
        if(bitmap == null) {
            throw new NullPointerException("Bitmap can't be null");
        }

        // 等比例缩放拉伸
        float widthScale = outWidth * 1.0f / bitmap.getWidth();
        float heightScale = outHeight * 1.0f / bitmap.getHeight();
        Matrix matrix = new Matrix();
        matrix.setScale(widthScale, heightScale);
        Bitmap newBt = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

        // 初始化目标bitmap
        Bitmap targetBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(targetBitmap);
        canvas.drawARGB(0, 0, 0, 0);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        // 利用画笔绘制底部圆角
        canvas.drawRoundRect(new RectF(0, outHeight - 2 * radius, outWidth, outHeight), radius, radius, paint);

        // 利用画笔绘制左侧矩形
        canvas.drawRect(new RectF(0, 0, radius, outHeight), paint);
        // 利用画笔绘制顶部上面直角部分
        canvas.drawRect(new RectF(0, 0, outWidth, outHeight - radius), paint);
        // 设置叠加模式
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        // 在画布上绘制原图片
        Rect ret = new Rect(0, 0, outWidth, outHeight);
        canvas.drawBitmap(newBt, ret, ret, paint);


        bitmap.recycle();
        newBt.recycle();

        return targetBitmap;
    }

谢谢大家的回答我也写了一个方法,目前已经实现效果,

clipboard.png

当 isSelect = false, 圆角矩形 没有显示出来效果,大家知道原因吗

如果一定不想重新切图(就是将图片切成圆角图片),那么可以考虑采用bitmap转圆角的方案来实现

    public static Bitmap toRoundCorner(Bitmap bitmap, int pixels, boolean lt, boolean rt, boolean lb,
    boolean rb) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Paint paint1 = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;
        paint.setAntiAlias(true);
        paint1.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        paint1.setColor(0xaaaaaaaa);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        if(lt){
        canvas.drawRect(0, 0, bitmap.getWidth()-pixels, bitmap.getHeight()-pixels, paint);
        }
        if(rt){
        canvas.drawRect(pixels, 0, bitmap.getWidth(), bitmap.getHeight()-pixels, paint);
        }
        if(lb){
        canvas.drawRect(0, pixels, bitmap.getWidth()-pixels, bitmap.getHeight(), paint);
        }
        if(rb){
        canvas.drawRect(pixels, pixels, bitmap.getWidth(), bitmap.getHeight(), paint);
        }
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
    }

将bitmap自己画一个圆角即可
网上抄的代码,没实测,不过目测应该是可以

lt = left top
rt = right Top
lb = left Bottom
rb = right Bottom

因为你图片覆盖了倒角,唯一能做的就是对图片也做倒角处理才行,图片不换就用代码坐下倒角,但看你这个图片感觉倒角处理了也没法用,那个勾太靠下了

NujNa
  • 134

换个图吧 clippath能做到但是有点麻烦

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏