自定义控件extendView学习

    技术2025-07-10  5

    1.常用方法

    1. Paint:涂料

    //设置画笔 paint = new Paint(); paint.setAntiAlias(true);//设置抗锯齿,这里是圆滑 paint.setColor(0xFFA4C739);//设置画笔颜色

    2. RectF:

    外轮廓矩形,大致描述形状

    @SuppressLint("DrawAllocation") RectF rectF=new RectF(0,0,getWidth(),getWidth());//定制外轮廓矩形 rectF.offset(0,0);//这里设置在哪里开始,左,头 canvas.drawArc(rectF,-10,-160,false,paint);//绘制弧

    参数介绍

    RectF(0,0,getWidth(),getWidth());左上右下 左往右画,上往下画

    从哪里开始画呢?rectF.offset(0,0);//这里设置在哪里开始,左,头,表示:

    x=0 位置开始画 y=0 位置开始画

    3.getWidth,getHeight

    getWidth,getHeight在draw里面有效,在构造方法里无效

    2.自定义画圆

    1.java代码

    import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.RectF; import android.util.AttributeSet; import android.util.Log; import android.view.View; import androidx.annotation.Nullable; import rudra.id.ac.unila.basicknowledge.R; /** * @ClassName View * @Description TODO * @Author ${孙伟豪} * @Date 2020/7/2 9:37 * @Version 1.0 */ public class TestRedButton extends android.view.View implements View.OnClickListener { private static final String TAG ="MyView" ; private Paint mPaint; private Rect mRect; private int mNumber =10;//默认为10 private RectF rectF; //在xml上的属性 private int mBackgroundColor=0x80FF0000; private int mTextSize=20; private int color;//颜色 private int alpha;//透明度 /** * 重写3种构造器 * @param context */ public TestRedButton(Context context) { this(context,null); } public TestRedButton(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public TestRedButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context,attrs);//初始化 } /** * 1.定大小 * @param widthMeasureSpec * @param heightMeasureSpec */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } /** * 2.定位置 * @param changed * @param left * @param top * @param right * @param bottom */ @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); } private void initView(Context context,AttributeSet attrs) { mPaint = new Paint();//涂料 mRect = new Rect();//矩形,用于确定位置 //形状 rectF = new RectF(-100, -100, 100, 100); rectF.offset(getWidth()/2,getHeight()/2);//在哪里开始画 //本身就是view,所以本身可以监听 TestRedButton.this.setOnClickListener(this); /**2. * 加载attrs文件 */ TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.TextAppearance);//参数:属性集,属性集名称 //获取里面的某一个属性 mBackgroundColor=typedArray.getColor(R.styleable.TestRedButton_backgroundColor,Color.RED);//参数:获取属性集里面的颜色,默认颜色 mTextSize=typedArray.getColor(R.styleable.TestRedButton_textSize,30);//参数:获取属性集里面的颜色,默认颜色 //根据值来获取透明度 // mBackgroundColor= } /** * 3.绘制 * @param canvas * 不能再这里new,因为这里会一直刷新,会占空间 */ @Override protected void onDraw(Canvas canvas) {//画布 super.onDraw(canvas); //涂料颜色 mPaint.setColor(Color.RED); mPaint.setTextSize(mTextSize); //getWidth,getHeight手机屏幕的宽高--x,y,radius,笔 canvas.drawCircle(getWidth()/2,getHeight()/2,getWidth()/2,mPaint);//画圆,radius:半径 /** * 中间有一个白色的字 */ mPaint.setColor(Color.WHITE);//涂料设置为白色 mPaint.setTextSize(100); //测量大小 //边界 String text=String.valueOf(mNumber); mPaint.getTextBounds(text,0,text.length(), mRect);//mRect表示文字的边界 int widthText = mRect.width();//文本的宽度 int heightText = mRect.height();//文本的高度 canvas.rotate(90,getWidth()/2,getHeight()/2);//旋转多少度,哪个为旋转点 canvas.drawText(text,getWidth()/2-widthText/2,getHeight()/2+heightText/2,mPaint); } /** * 4.刷新 */ @Override public void invalidate() { super.invalidate(); } @Override public void onClick(View v) { mNumber=mNumber<=0?20:mNumber-1; Log.d(TAG, "onClick: "+mNumber); invalidate(); } }

    xml

    <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <rudra.id.ac.unila.basicknowledge.view.TestRedButton android:layout_width="300dp" android:layout_height="300dp" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>

    attrs

    <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="TestRedButton"> <attr name="backgroundColor" format="color"/> <attr name="textSize" format="dimension"/> </declare-styleable> </resources>

    3.问题

    1.paint.setalpha无效

    解决办法: setColor把它覆盖掉了,所以要写在setColor后面

    2.bitmap为空

    用as系统的图片,那不是图片,不能drawBitmap

    Processed: 0.017, SQL: 9