日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

万能圆角

發(fā)布時間:2024/1/1 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 万能圆角 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言:我們項目當中經(jīng)常碰到要對某個圖片控件做成圓角,或者動態(tài)改變它的圓角率,一般的做法如下

/*** 獲得圓角圖片* @param bitmap* @param roundPx* @return* Bitmap.createBitmap 很容易出現(xiàn)oom,盡量不要使用*/public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);Canvas canvas = new Canvas(output);final Paint paint = new Paint();paint.setAntiAlias(true);final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());final RectF rectF = new RectF(rect);canvas.drawRoundRect(rectF, roundPx, roundPx, paint);paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));canvas.drawBitmap(bitmap, rect, rect, paint);return output;}

但是此種方式一直在createBitmap容易出現(xiàn)性能問題,于是我就在想,有沒有不會出現(xiàn)性能問題而且還可以通用的方式呢,基于此,下面的萬能圓角類誕生了
萬能圓角類:
1.支持靜態(tài)文件配置屬性,
2.支持動態(tài)代碼改圓角率,
3.支持所有的控件而不是圖片控件
完整的代碼如下:

import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RectF; import android.os.Build; import android.util.AttributeSet; import android.widget.TextView; import com.xxx.demo.R;/*** Created by xxx on 2019/1/27.*/ public class RoundTextView extends TextView {Paint outPaint;int paintColor=Color.WHITE,tvRadio=21;Path path1,path2;RectF outRect;public RoundTextView(Context context) {super(context);initPaint(context, null);}public RoundTextView(Context context, AttributeSet attrs) {super(context, attrs);initPaint(context, attrs);}public RoundTextView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs);initPaint(context, attrs);}private void initPaint(Context context, AttributeSet attrs) {//自定義屬性//outPaint的顏色值 ,圓角率if (attrs!=null) {TypedArray ta= context.obtainStyledAttributes(attrs, R.styleable.RoundTextView);paintColor = ta.getColor(R.styleable.RoundTextView_paintColor, Color.WHITE);tvRadio= ta.getDimensionPixelSize(R.styleable.RoundTextView_tvRadio,21);ta.recycle();}if(outPaint==null){outPaint = new Paint();outPaint.setStyle(Paint.Style.FILL_AND_STROKE);outPaint.setAntiAlias(true);outPaint.setColor(paintColor);}if (path1==null) {path1 = new Path();path2 = new Path();}}@Overrideprotected void onLayout(boolean changed, int left, int top, int right, int bottom) {super.onLayout(changed, left, top, right, bottom);if (changed) {outRect=new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight());}}@Overrideprotected void onDraw(Canvas canvas) {canvas.save();path1.reset();path2.reset();path1.addRect(outRect,Path.Direction.CW);path2.addRoundRect(outRect,tvRadio,tvRadio,Path.Direction.CW);if (Build.VERSION.SDK_INT >= 19) {path1.op(path2, Path.Op.DIFFERENCE );}canvas.drawPath(path1, outPaint);canvas.restore();super.onDraw(canvas);}public void setRadio(int radio) {tvRadio = radio;requestLayout();}public void setColor(int color){if (outPaint!=null) {outPaint.setColor(color);}requestLayout();}/*** @param color;#ff0000*/public void setColor(String color){if (outPaint!=null) {outPaint.setColor(Color. parseColor(color));}requestLayout();} }

在res–>values–>attrs自定義屬性的代碼如下:

<?xml version="1.0" encoding="utf-8"?> <resources><!--萬能圓角的自定義屬性--><declare-styleable name="RoundTextView"><attr name="paintColor" format="color"/><attr name="tvRadio" format="dimension"/></declare-styleable> </resources>

在布局文件中使用如下:

<RelativeLayoutandroid:layout_width="wrap_content"app:layout_constraintTop_toBottomOf="@+id/et"app:layout_constraintRight_toRightOf="parent"app:layout_constraintLeft_toLeftOf="parent"android:layout_marginTop="20dp"android:layout_height="200dp"><!--目標圓角控件--><TextViewapp:layout_constraintTop_toTopOf="parent"android:layout_width="200dp"android:layout_height="200dp"android:textSize="15sp"android:gravity="center"android:text="代替圖片圓角的控件"android:background="#00ff00"/><!--蓋在目標圓角控件上的萬能圓角類--><com.xxx.demo.myview.RoundTextViewandroid:id="@+id/rtv"android:layout_width="200dp"android:layout_height="200dp"android:clickable="true"app:paintColor="#ff0000"app:tvRadio="100dp"/> </RelativeLayout>

注意這里使用了app命名空間,記得加這句話
xmlns:app=“http://schemas.android.com/apk/res-auto”
實現(xiàn)效果如下:

說明:
1,把紅色設置成圓角控件的背景顏色,就可以看起來是圓角
2,實現(xiàn)的原理是:在要實現(xiàn)圓角控件的上面放一個遮住4個角的控件,而4個角的控件是通過path.addXXX然后取2個path的交集或者并集而得到的(其實利用path.op可以得到很多好玩的圖形 path1.op(path2, Path.Op.DIFFERENCE,具體可自行百度 );

總結

以上是生活随笔為你收集整理的万能圆角的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。