本文介绍了我怎样才能插入超出了我的圆形图像的边界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我可以插入一个灰色边框的圆形图像外。

I would like to know how can I insert a grey border outside an circular image.

我使用这个code创建圆形imageviews:

I'm using this code to create circular imageviews:

public Bitmap transform(Bitmap source) {
        int size = Math.min(source.getWidth(), source.getHeight());

        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;

        Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
        if (squaredBitmap != source) {
            source.recycle();
        } 

        Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        BitmapShader shader = new BitmapShader(squaredBitmap,
        BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
        paint.setShader(shader);
        paint.setAntiAlias(true);

        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);

        squaredBitmap.recycle();
        return bitmap;
    }

我试着用这些行插入边框:

I tried to insert the border using these lines:

paint.setStrokeWidth(4);
paint.setStyle(Style.STROKE);

但它没有工作,边境出现了,但图像消失了。

But it didn't worked, the border appeared, but the image disappeared.

推荐答案

这可以实现如下:

   //put your bitmap here 
             Bitmap mIcon11=    MainActivity.drawableToBitmap(getResources().getDrawable(R.drawable.crow)); 

              Bitmap output = Bitmap.createBitmap(mIcon11.getWidth(),
                        mIcon11.getHeight(), Config.ARGB_8888);
                Canvas canvas = new Canvas(output);

                final Rect itemRect = new Rect(0, 0, mIcon11.getWidth(), mIcon11.getHeight());


             RectF roundRect = new RectF(itemRect);
             Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  //your bitmap here  
             Bitmap bitmap = MainActivity.drawableToBitmap(getResources().getDrawable(R.drawable.crow));

             mPaint.setStyle(Paint.Style.FILL);
             mPaint.setColor(Color.WHITE);
             canvas.drawRoundRect(roundRect, 100, 100, mPaint);

             roundRect.set(itemRect.left + 5, itemRect.top + 5, itemRect.right - 5, itemRect.bottom - 5);
             Path clipPath = new Path();
            //roundness of border
             clipPath.addRoundRect(roundRect, 100, 100, Path.Direction.CW);
             canvas.save(Canvas.CLIP_SAVE_FLAG);
             canvas.clipPath(clipPath);
             canvas.drawBitmap(bitmap, null, roundRect, mPaint);
             canvas.restore();
             myImageView.setImageBitmap(output);

这篇关于我怎样才能插入超出了我的圆形图像的边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 10:27