• Android 圆角研究


    1.通过xml中shape标签来实现,这种实现方式可用于纯色的背景,多用于Button,TextView,EditText等控件:

    示例代码

    <?xml version="1.0" encoding="UTF-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape>
                <solid android:color="#ffffff"/>
                <corners
                    android:bottomRightRadius="10dp"
                    android:bottomLeftRadius="10dp"
                    android:topLeftRadius="10dp"
                    android:topRightRadius="10dp"/>
            </shape>
        </item>
    </layer-list>

    2.通过Canvas的drawRoundRect(rectF, roundPx, roundPx, paint); 方法可以直接设置圆角,但是只能同时设置四角。

    3.Imageview,自定义View等可通过以下方式实现,此方式可以设置左上、右上、左下、右下,可随意设置一个到四个圆角:

     

    BitmapShader渲染器,通过Paint的setShader(Shader shader)方法设置渲染器,通过canvas画出,以下为代码片段:

    1  BitmapShader bitmapShader = new BitmapShade(bitmap,Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    2  RectF rectF = new RectF(0,0,bitmap.getScaledWidth(getResources().getDisplayMetrics()),bitmap.getScaledHeight(getResources().getDisplayMetrics()));
    3  path.addRoundRect(rectF,mRadii,Path.Direction.CW);//通过此方法得到圆角
    4  canvas.drawPath(path, paint);
     1     /**
     2      * Add a closed round-rectangle contour to the path. Each corner receives
     3      * two radius values [X, Y]. The corners are ordered top-left, top-right,
     4      * bottom-right, bottom-left
     5      *
     6      * @param rect The bounds of a round-rectangle to add to the path
     7      * @param radii Array of 8 values, 4 pairs of [X,Y] radii
     8      * @param dir  The direction to wind the round-rectangle's contour
     9      */
    10     public void addRoundRect(RectF rect, float[] radii, Direction dir) {
    11         if (rect == null) {
    12             throw new NullPointerException("need rect parameter");
    13         }
    14         addRoundRect(rect.left, rect.top, rect.right, rect.bottom, radii, dir);
    15     }


    这样就可以随意设置图片的圆角了,无论是左上,右下,左下,右下。

    在万能的GitHub有个SelectableRoundeImageView就是关于图片圆角View,有兴趣的可以去看看。

    Shader详解,请参考AigeStudio 自定义控件其实很简单1/3 。

  • 相关阅读:
    第22课对象的销毁
    第21课对象的构造顺序
    第20课 初始化列表的使用
    第19课构造函数(下)
    第18课构造函数(中)
    第17课构造函数(上)
    第16课类的真正形态
    调试环境的搭建
    Hello,DTOS!(下)
    Hello,DTOS!(中)
  • 原文地址:https://www.cnblogs.com/D-Luffy/p/5434980.html
Copyright © 2020-2023  润新知