Unable to get exact circle shape when using card view

前端 未结 11 1897
旧时难觅i
旧时难觅i 2020-12-25 10:12

I\'m using card view for floating action button in android material design. I\'m using following code for get the circle



        
11条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-25 11:08

    card_layout.xml

    
    
        
    
    

    MainActivity.java

     ImageView imageView = (ImageView) findViewById(R.id.card_thumbnail_image);
        Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.rose);
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            //Default 
            imageView.setBackgroundResource(R.drawable.rose);
        } else {
            //RoundCorners 
            RoundCornersDrawable round = new RoundCornersDrawable(mBitmap,
                    getResources().getDimension(R.dimen.cardview_default_radius), 0); //or your custom radius
    
            CardView cardView = (CardView) findViewById(R.id.card_view);
            cardView.setPreventCornerOverlap(false); //it is very important
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
                imageView.setBackground(round);
            else
                imageView.setBackgroundDrawable(round);
        }
    
    RoundCornersDrawable.java
    
        public class RoundCornersDrawable extends Drawable {
    
        private final float mCornerRadius;
        private final RectF mRect = new RectF();
        //private final RectF mRectBottomR = new RectF();
        //private final RectF mRectBottomL = new RectF();
        private final BitmapShader mBitmapShader;
        private final Paint mPaint;
        private final int mMargin;
    
        public RoundCornersDrawable(Bitmap bitmap, float cornerRadius, int margin) {
            mCornerRadius = cornerRadius;
    
            mBitmapShader = new BitmapShader(bitmap,
                    Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setShader(mBitmapShader);
    
            mMargin = margin;
        }
    
        @Override
        protected void onBoundsChange(Rect bounds) {
            super.onBoundsChange(bounds);
            mRect.set(mMargin, mMargin, bounds.width() - mMargin, bounds.height() - mMargin);
            //mRectBottomR.set( (bounds.width() -mMargin) / 2, (bounds.height() -mMargin)/ 2,bounds.width() - mMargin, bounds.height() - mMargin);
           // mRectBottomL.set( 0,  (bounds.height() -mMargin) / 2, (bounds.width() -mMargin)/ 2, bounds.height() - mMargin);
        }
    
        @Override
        public void draw(Canvas canvas) {
            canvas.drawRoundRect(mRect, mCornerRadius, mCornerRadius, mPaint);
            //canvas.drawRect(mRectBottomR, mPaint); //only bottom-right corner not rounded
            //canvas.drawRect(mRectBottomL, mPaint); //only bottom-left corner not rounded
    
        }
    
        @Override
        public int getOpacity() {
            return PixelFormat.TRANSLUCENT;
        }
    
        @Override
        public void setAlpha(int alpha) {
            mPaint.setAlpha(alpha);
        }
    
        @Override
        public void setColorFilter(ColorFilter cf) {
            mPaint.setColorFilter(cf);
        }
    
    
    }
    

提交回复
热议问题