如何在圆形imageView android上添加阴影和边框?

我创建了一个带有以下问题的CircularImageView:在android中创建圆形图像视图


在GitHub上下载项目


1)这是CircularImageView类:


public class CircularImageView extends ImageView {

    public CircularImageView(Context context) {

        super(context);

    }


    public CircularImageView(Context context, AttributeSet attrs) {

        super(context, attrs);

    }


    public CircularImageView(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);

    }


    @Override

    protected void onDraw(Canvas canvas) {

        Drawable drawable = getDrawable();

        if (drawable == null) {

            return;

        }


        if (getWidth() == 0 || getHeight() == 0) {

            return; 

        }

        Bitmap b =  ((BitmapDrawable)drawable).getBitmap() ;

        Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);      


        Bitmap roundBitmap =  getCroppedBitmap(bitmap, getWidth());

        canvas.drawBitmap(roundBitmap, 0, 0, null);

    }


    public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {

        Bitmap sbmp;

        if(bmp.getWidth() != radius || bmp.getHeight() != radius)

            sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);

        else

            sbmp = bmp;


        Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Bitmap.Config.ARGB_8888);

        final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());


        Paint paint = new Paint();

        paint.setAntiAlias(true);

        paint.setFilterBitmap(true);

        paint.setDither(true);      

        paint.setColor(Color.parseColor("#BAB399"));


        Canvas c = new Canvas(output);        

        c.drawARGB(0, 0, 0, 0);

        c.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f, sbmp.getWidth() / 2+0.1f, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

        c.drawBitmap(sbmp, rect, rect, paint);


        return output;

    }

}


慕娘9325324
浏览 1741回答 3
3回答

紫衣仙女

加入  canvas.drawCircle(getWidth() / 2, getWidth() / 2, getWidth() / 2, paint); 之前  canvas.drawBitmap(roundBitmap, 0, 0, null);更改  c.drawCircle(sbmp.getWidth() / 2, sbmp.getHeight() / 2, sbmp.getWidth() / 2, paint); 为  c.drawCircle(sbmp.getWidth() / 2, sbmp.getHeight() / 2, sbmp.getWidth() / 2 - "the border with you prefer", paint);希望能帮助到你。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android
Java