在相親原始碼開發中,如何實現圓角及特殊圓角的使用?
1、xml檔案中shape的方式
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=" xmlns:app=" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:layout_centerInParent="true" android:background="@drawable/shape_circle" /></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="><!-- 圓--> <corners android:radius="8dp" /><!-- 邊框--> <solid android:color="@color/teal_200"/><!-- 漸變--><!-- <gradient />--></shape>
2、cardview
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=" xmlns:app=" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.cardview.widget.CardView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" app:cardCornerRadius="10dp" app:cardElevation="10dp"> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/ic_launcher_background"/> </androidx.cardview.widget.CardView> </RelativeLayout>
3、自定義View
/** * 自定義圓角view */ public class RoundImageView extends AppCompatImageView { private float width, height; // 全部角度 private float radius, leftRadius, rightRadius; // 是否是半圓 private boolean isRound = false; // 底部圓角 private boolean isBottomRound = false; public RoundImageView(Context context) { this(context, null); } public RoundImageView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public RoundImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } /** * 初始化 * * @param context * @param attrs */ private void init(Context context, AttributeSet attrs) { TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView); radius = array.getDimensionPixelOffset(R.styleable.RoundImageView_radius, 0); leftRadius = array.getDimensionPixelOffset(R.styleable.RoundImageView_left_radius, 0); rightRadius = array.getDimensionPixelOffset(R.styleable.RoundImageView_right_radius, 0); if (radius == 0 && leftRadius == 0 && rightRadius == 0) { isRound = true; } if (leftRadius != 0 && rightRadius != 0) { isBottomRound = true; } array.recycle(); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); width = getWidth(); height = getHeight(); } @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { // 抗鋸齒 canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG)); Path path = new Path(); if (isRound) { radius = Math.min(width, height) / 2; } // 設定背景顏色透明 canvas.drawColor(Color.TRANSPARENT); Drawable drawable = getDrawable(); int w = this.getWidth(); int h = this.getHeight(); if (null != drawable && w > 0 && h > 0) { float[] rids; // 圓角的半徑,依次為左上角xy半徑,右上角,右下角,左下角 if (isBottomRound) { rids = new float[]{0, 0, 0, 0, rightRadius, rightRadius, leftRadius, leftRadius}; } else { rids = new float[]{radius, radius, radius, radius, radius, radius, radius, radius}; } path.addRoundRect(new RectF(0, 0, w, h), rids, Path.Direction.CW); canvas.clipPath(path); } super.onDraw(canvas); } public void setRadius(int radius) { this.radius = radius; invalidate(); } }
<!-- RoundImageView --><declare-styleable name="RoundImageView"> <!-- 圓角的半徑 --> <attr name="radius" format="dimension" /> <attr name="left_radius" format="dimension" /> <attr name="right_radius" format="dimension" /></declare-styleable>
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=" xmlns:app=" android:layout_width="match_parent" android:layout_height="match_parent"> <com.jsxr.kotlintest.RoundImageView android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/ic_launcher_background" app:radius="10dp" android:layout_centerInParent="true"/></RelativeLayout>
GradientDrawable
public class MyShape { private static final ConcurrentHashMap<String, GradientDrawable> hashMap = new ConcurrentHashMap(); /** * 設定圓角 * @param context * @param radius * @return */ public static GradientDrawable setMyShape(Context context, int radius) { int realRadius = ImageUtil.dp2px(context, radius); String radiusKey = String.valueOf(radius); if (hashMap.contains(radiusKey)) { return hashMap.get(radiusKey); } else { GradientDrawable drawable = new GradientDrawable(); if (radius != 0) { drawable.setCornerRadius(realRadius); } hashMap.put(radiusKey, drawable); return drawable; } } /** * @param context * @param radius * @param bg * @return 自定義圓角、背景圖形 */ public static GradientDrawable setMyShape(Context context, int radius, int bg) { int realRadius = ImageUtil.dp2px(context, radius); String key = String.valueOf(realRadius + bg); if (hashMap.contains(key)) { return hashMap.get(key); } else { GradientDrawable drawable = new GradientDrawable(); if (radius != 0) { drawable.setCornerRadius(realRadius); } if (bg != 0) { drawable.setColor(bg); } hashMap.put(key, drawable); return drawable; } } /** * 設定圓角、背景、透明度 * * @param radius * @param bg * @param alpha * @return */ public static GradientDrawable setMyShapeWithAlpha(Context activity,int radius, int bg, int alpha) { int realRadius = ImageUtil.dp2px(activity, radius); String key = String.valueOf(realRadius + bg + alpha); if (hashMap.containsKey(key)) { return hashMap.get(key); } else { GradientDrawable drawable = new GradientDrawable(); drawable.setCornerRadius(radius); drawable.setColor(bg); drawable.setAlpha(alpha); hashMap.put(key, drawable); return drawable; } } /** * @param activity * @param topLeft,topRight,bottomLeft,bottomRight * @param bg * @return 自定義圓角、背景佈局 */ public static GradientDrawable setMyShapeRadiusWithBg(Context activity, int topLeft, int topRight, int bottomLeft, int bottomRight, int bg) { topLeft = ImageUtil.dp2px(activity, topLeft); topRight = ImageUtil.dp2px(activity, topRight); bottomLeft = ImageUtil.dp2px(activity, bottomLeft); bottomRight = ImageUtil.dp2px(activity, bottomRight); String key = String.valueOf(topLeft + topRight + bottomLeft + bottomRight + bg); if (hashMap.containsKey(key)) { return hashMap.get(key); } else { GradientDrawable drawable = new GradientDrawable(); drawable.setCornerRadii(new float[]{topLeft, topLeft, topRight, topRight, bottomLeft, bottomLeft, bottomRight, bottomRight}); if (bg != 0) { drawable.setColor(bg); } hashMap.put(key, drawable); return drawable; } } /** * @param activity * @param radius * @param width * @param bg * @return 設定帶邊框的橢圓+圓角+自定義邊框顏色 */ public static GradientDrawable setMyShapeStroke(Context activity, int radius, int width, int bg) { int realRadius = ImageUtil.dp2px(activity, radius); int realWidth = ImageUtil.dp2px(activity, width); String key = String.valueOf(realRadius + realWidth + bg); if (hashMap.containsKey(key)) { return hashMap.get(key); } else { GradientDrawable drawable = new GradientDrawable(); if (radius != 0) { drawable.setCornerRadius(realRadius); } if (width != 0 && bg != 0) { drawable.setStroke(realWidth, bg); } hashMap.put(key, drawable); return drawable; } } /** * @param activity * @param radius * @param width * @param strokeBg * @param bg * @return 設定帶邊框的橢圓,自定義內部顏色+自定義邊框顏色 */ public static GradientDrawable setMyshapeStroke(Context activity, int radius, int width, int strokeBg, int bg) { int realRadius = ImageUtil.dp2px(activity, radius); int realWidth = ImageUtil.dp2px(activity, width); String key = String.valueOf(realRadius + realRadius + strokeBg); if (hashMap.containsKey(key)) { return hashMap.get(key); } else { GradientDrawable drawable = new GradientDrawable(); if (radius != 0) { drawable.setCornerRadius(realRadius); } if (width != 0 && strokeBg != 0) { drawable.setStroke(realWidth, strokeBg); } if (bg != 0) { drawable.setColor(bg); } hashMap.put(key, drawable); return drawable; } } /** * @param activity * @param topLeftRadius * @param topRightRadius * @param bottomRightRadius * @param bottomLeftRadius * @return 設定橢圓,並可以自定義內部顏色 */ public static GradientDrawable setMyShapeRadius(Context activity, int bgColor, int topLeftRadius, int topRightRadius, int bottomRightRadius, int bottomLeftRadius) { GradientDrawable drawable = new GradientDrawable(); if (bgColor != 0) { drawable.setColor(bgColor); } drawable.setCornerRadii(new float[]{ImageUtil.dp2px(activity, topLeftRadius), ImageUtil.dp2px(activity, topLeftRadius), ImageUtil.dp2px(activity, topRightRadius), ImageUtil.dp2px(activity, topRightRadius), ImageUtil.dp2px(activity, bottomRightRadius), ImageUtil.dp2px(activity, bottomRightRadius), ImageUtil.dp2px(activity, bottomLeftRadius), ImageUtil.dp2px(activity, bottomLeftRadius)}); return drawable; } /** * @param startColor * @param endColor * @param angle * @return 設定漸變色 */ @SuppressLint("WrongConstant") public static GradientDrawable setGradient(Context activity, int startColor, int endColor, int topLeftRadius, int topRightRadius, int bottomRightRadius, int bottomLeftRadius, int angle) { int[] colors = {startColor, endColor}; topLeftRadius = ImageUtil.dp2px(activity, topLeftRadius); topRightRadius = ImageUtil.dp2px(activity, topRightRadius); bottomLeftRadius = ImageUtil.dp2px(activity, bottomLeftRadius); bottomRightRadius = ImageUtil.dp2px(activity, bottomRightRadius); String key = String.valueOf(topLeftRadius + topRightRadius + bottomLeftRadius + bottomRightRadius + angle); if (hashMap.containsKey(key)) { return hashMap.get(key); } else { GradientDrawable drawable = null; if (angle == 0) { drawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colors); } else if (angle == 90) { drawable = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, colors); } else if (angle == 180) { drawable = new GradientDrawable(GradientDrawable.Orientation.RIGHT_LEFT, colors); } else if (angle == 270) { drawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors); } else if (angle == 315) { drawable = new GradientDrawable(GradientDrawable.Orientation.TL_BR, colors); } else { drawable = new GradientDrawable(); } drawable.setCornerRadii(new float[]{topLeftRadius, topLeftRadius, topRightRadius, topRightRadius, bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius}); drawable.setGradientType(GradientDrawable.RECTANGLE); hashMap.put(key, drawable); return drawable; } } }
class TestAdapter(val context: Context, val list: MutableList<String>) : RecyclerView.Adapter<TestAdapter.ViewHolder>() { class ViewHolder(item: View) : RecyclerView.ViewHolder(item) { val layout: ConstraintLayout = item.findViewById(R.id.item_layout) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val inflate = LayoutInflater.from(context).inflate(R.layout.item, parent, false) return ViewHolder(inflate) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { when (position) { 1 -> holder.layout.background = MyShape.setMyShape(context, 10, ContextCompat.getColor(context, R.color.design_default_color_error) ) 2 -> holder.layout.background = MyShape.setMyShapeStroke(context, 15, 3, ContextCompat.getColor(context, R.color.design_default_color_primary_dark) ) 3 -> holder.layout.background = MyShape.setMyShapeRadius(context, ContextCompat.getColor(context, R.color.design_default_color_secondary_variant), 0, 10, 0, 10 ) 4 -> holder.layout.background = MyShape.setMyShapeWithAlpha(context, 10, ContextCompat.getColor(context, android.R.color.holo_green_dark), 50 ) 5 -> holder.layout.background = MyShape.setMyShapeRadiusWithBg(context, 15, 0, 15, 0, ContextCompat.getColor(context, android.R.color.holo_red_light)) 6 -> holder.layout.background = MyShape.setGradient(context, ContextCompat.getColor(context, android.R.color.holo_purple), ContextCompat.getColor(context, R.color.black), 15, 15, 15, 15, 180) } } override fun getItemCount(): Int { return list.size } }
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android=" android:id="@+id/item_layout" android:layout_width="50dp" android:layout_height="25dp" android:layout_margin="25dp" android:background="@color/black" ></androidx.constraintlayout.widget.ConstraintLayout>
setCornerRadius 設定圓角 setColor 設定背景顏色 setAlpha 設定透明度 setCornerRadii 設定多角度圓角 setStroke 設定邊框+邊框顏色 setGradientType 設定漸變風格 有四種 GradientDrawable.RECTANGLE 矩形 GradientDrawable.LINE 線 GradientDrawable.RING 圓環 GradientDrawable.OVAL 圓
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69996194/viewspace-2838599/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Android 圓角、圓形 ImageView 實現AndroidView
- CAD倒圓角命令如何使用
- 如何實現css漸變圓角邊框CSS
- Android 開發:glide圓角,圓形,效率問題AndroidIDE
- php短視訊原始碼,按鈕的圓角圖示實現PHP原始碼
- Flutter 彩邊圓角 Container 的實現FlutterAI
- Glide實現圓角圖片,以及圓形圖片IDE
- iOS 實現檢視指定圓角iOS
- CSS不用背景圖片實現優惠券樣式反圓角,凹圓角,反向半圓角,並且背景漸變CSS
- visio圓角矩形怎麼改變圓角大小
- 如何用pscc 2018圓角外掛mac版製作倒圓角Mac
- WPF之小米Logo超圓角的實現Go
- Flutter 圓形/圓角頭像Flutter
- 圓形視訊和圓角視訊的一種實現方式
- AUTOCAD——圓角命令
- 直播app系統原始碼,Flutter MaterialButton 實現圓角邊框按鈕APP原始碼Flutter
- Android 實現一個通用的圓角佈局Android
- 根據SVG Arc求出其開始角、擺動角和橢圓圓心SVG
- Mac OS X下實現矩形部分圓角Mac
- iOS 繪製圓角iOS
- CSS 文字框圓角CSS
- PHP合成圖圓角PHP
- box-shadow圓角效果程式碼
- 筆記-iOS設定圓角方法以及指定位置設圓角筆記iOS
- CSS 奇思妙想 | 巧妙的實現帶圓角的三角形CSS
- 神奇的濾鏡!巧妙實現內凹的平滑圓角
- 相親交友原始碼開發,前端如何實現水印功能?原始碼前端
- iOS開發_繪製圓角矩形虛線環iOS
- cad圓角命令怎麼用 cad圓角命令無效怎麼回事
- canvas 繪製圓角矩形Canvas
- SVG 繪製圓角矩形SVG
- 圓角select下拉選單
- css圓角矩形邊框CSS
- 使用 RoundedBitmapDrawable 建立圓角頭像詳解
- css如何為圖片設定圓角CSS
- UIView 的部分圓角的設定UIView
- 直播平臺原始碼,自定義設定 View 四個角的圓角 以及邊框的設定原始碼View
- PS製作圓角圖片