1 CircleView类,实现在View上画一个圆形背景,圆形是空心还是实心, 圆的边的粗细以及圆的背景都可以在布局文件中指出
public class CircleView extends View { private Context mContext; private Paint mPaint; private int mColor;//background of the circle private int border;//border width of the circle private boolean isStroke;//whether the circle is stroke public CircleView(Context context) { super(context, null); } public CircleView(Context context, AttributeSet attrs) { super(context, attrs); this.mContext = context; //set attr that assigned in the XML TypedArray attributes = mContext.obtainStyledAttributes(attrs, R.styleable.CircleView); mColor = attributes.getColor(R.styleable.CircleView_circle_Color, Color.GREEN); border = attributes.getInt(R.styleable.CircleView_circle_border, 1); isStroke = attributes.getBoolean(R.styleable.CircleView_circle_border_stroke, false); init(); } private void init() { mPaint = new Paint(); mPaint.setColor(mColor); mPaint.setStyle(isStroke ? Paint.Style.STROKE : Paint.Style.FILL); mPaint.setStrokeWidth(border); } @Override public void layout(int l, int t, int r, int b) { super.layout(l, t, r, b); } @Override public void draw(Canvas canvas) { super.draw(canvas); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); //create a real dip dimension int widthDip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, mContext.getResources().getDisplayMetrics()); int heightDip = widthDip; if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) { setMeasuredDimension(widthDip, heightDip); } else if (widthSpecMode == MeasureSpec.AT_MOST) { setMeasuredDimension(widthDip, heightSpecSize); } else if (heightSpecMode == MeasureSpec.AT_MOST) { setMeasuredDimension(widthSpecSize, heightDip); } } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //handle the padding, if not handle, the padding in the XML won't effect int paddingLeft = getPaddingLeft(); int paddingRight = getPaddingRight(); int paddingTop = getPaddingTop(); int paddingBottom = getPaddingBottom(); //calculator the real size int width = getWidth() - (paddingLeft + paddingRight); int height = getHeight() - (paddingTop + paddingBottom); int radius = Math.min(width, height) / 2; //calculator the absolute position int xPoint = paddingLeft + width / 2; int yPoint = paddingTop + height / 2; canvas.drawCircle(xPoint, yPoint, radius, mPaint); } }2 定义styleable在XML中使用<declare-styleable name="CircleView"> <attr name="circle_Color" format="color"/> <attr name="circle_border_stroke" format="boolean"></attr> <attr name="circle_border" format="integer"/> </declare-styleable>3 布局文件<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app = "http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:gravity="center" android:background="@android:color/holo_blue_light" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/holo_red_light" android:orientation="vertical" android:gravity="center" > <com.example.venn.costumeview.ui.customer.CircleView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="20dp" app:circle_Color="@android:color/holo_orange_light" app:circle_border_stroke="false" app:circle_border="10" /> </LinearLayout> </RelativeLayout>
自定义View-继承系统View
最新推荐文章于 2022-07-01 10:03:37 发布