Android 各种View设置圆角

在项目开发中,经常遇到圆角展示的效果,例如有ImageView圆角、Banner条圆角、TextView圆角、Relative/LinearLayout整体圆角等,其实就是在外层加一层父容器,对父容器进行裁剪。

                    <com.star.film.sdk.vr.view.RoundRectLayout
                        android:id="@+id/roundRectLayout"
                        android:layout_width="match_parent"
                        android:layout_height="200dp"
                        android:layout_alignParentBottom="true"
                        android:layout_marginBottom="38dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp">

                        <..view />
                    </com.star.film.sdk.vr.view.RoundRectLayout>
public class RoundRectLayout extends RelativeLayout {

    private Path mPath;
    private int mRadius;

    private int mWidth;
    private int mHeight;
    private int mLastRadius;

    public static final int MODE_NONE = 0;
    public static final int MODE_ALL = 1;
    public static final int MODE_LEFT = 2;
    public static final int MODE_TOP = 3;
    public static final int MODE_RIGHT = 4;
    public static final int MODE_BOTTOM = 5;

    private int mRoundMode = MODE_ALL;

    public RoundRectLayout(Context context) {
        super(context);
        init();
    }

    public RoundRectLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init(){
        setBackgroundDrawable(new ColorDrawable(0x33ff0000));
        mPath = new Path();
        mPath.setFillType(Path.FillType.EVEN_ODD);
        setCornerRadius(15);
    }

    /**
     * 设置是否圆角裁边
     * @param roundMode
     */
    public void setRoundMode(int roundMode){
        mRoundMode = roundMode;
    }

    /**
     * 设置圆角半径
     * @param radius
     */
    public void setCornerRadius(int radius){
        mRadius = radius;
    }

    private void checkPathChanged(){
        if(getWidth() == mWidth && getHeight() == mHeight && mLastRadius == mRadius){
            return;
        }
        mWidth = getWidth();
        mHeight = getHeight();
        mLastRadius = mRadius;
        mPath.reset();
        switch (mRoundMode){
            case MODE_ALL:
                mPath.addRoundRect(new RectF(0, 0, mWidth, mHeight), mRadius, mRadius, Path.Direction.CW);
                break;
            case MODE_LEFT:
                mPath.addRoundRect(new RectF(0, 0, mWidth, mHeight),
                        new float[]{mRadius, mRadius, 0, 0, 0, 0, mRadius, mRadius},
                        Path.Direction.CW);
                break;
            case MODE_TOP:
                mPath.addRoundRect(new RectF(0, 0, mWidth, mHeight),
                        new float[]{mRadius, mRadius, mRadius, mRadius, 0, 0, 0, 0},
                        Path.Direction.CW);
                break;
            case MODE_RIGHT:
                mPath.addRoundRect(new RectF(0, 0, mWidth, mHeight),
                        new float[]{0, 0, mRadius, mRadius, mRadius, mRadius, 0, 0},
                        Path.Direction.CW);
                break;
            case MODE_BOTTOM:
                mPath.addRoundRect(new RectF(0, 0, mWidth, mHeight),
                        new float[]{0, 0, 0, 0, mRadius, mRadius, mRadius, mRadius},
                        Path.Direction.CW);
                break;
        }
    }

    @Override
    public void draw(Canvas canvas) {
        if(mRoundMode != MODE_NONE){
            int saveCount = canvas.save();
            checkPathChanged();
            canvas.clipPath(mPath);
            super.draw(canvas);
            canvas.restoreToCount(saveCount);
        }else {
            super.draw(canvas);
        }
    }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中设置圆角的方法有两种:使用xml文件设置和使用Java代码动态设置。 1. 使用xml文件设置圆角 在drawable文件夹下新建一个shape.xml文件,然后在文件中添加如下代码: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:radius="10dp"> <solid android:color="#ffffff" /> <corners android:radius="10dp" /> </shape> ``` 其中,android:radius属性表示整个矩形的圆角半径,corners标签中的android:radius属性表示四个角的圆角半径。如果需要单独控制某一个角的显示样式,可单独设置四个角的值,例如: ```xml <corners android:radius="10dp" android:topLeftRadius="0dp" android:topRightRadius="10dp" android:bottomRightRadius="0dp" android:bottomLeftRadius="10dp" /> ``` 2. 使用Java代码动态设置圆角 ```java // 加载图片 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); // 创建一个空的Bitmap Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); // 创建一个画布 Canvas canvas = new Canvas(output); // 创建一个画笔 Paint paint = new Paint(); // 创建一个矩形 Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); // 创建一个圆角矩形 RectF rectF = new RectF(rect); // 设置圆角半径 float roundPx = 20; // 绘制圆角矩形 paint.setAntiAlias(true); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); // 设置图像的叠加模式 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); // 绘制图像 canvas.drawBitmap(bitmap, rect, rect, paint); // 显示图像 ImageView imageView = (ImageView) findViewById(R.id.image_view); imageView.setImageBitmap(output); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值