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
;
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;
private RectF rectF
;
private int mBackgroundColor
=0x80FF0000;
private int mTextSize
=20;
private int color
;
private int alpha
;
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
);
}
@Override
protected void onMeasure(int widthMeasureSpec
, int heightMeasureSpec
) {
super.onMeasure(widthMeasureSpec
, heightMeasureSpec
);
}
@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);
TestRedButton
.this.setOnClickListener(this);
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);
}
@Override
protected void onDraw(Canvas canvas
) {
super.onDraw(canvas
);
mPaint
.setColor(Color
.RED
);
mPaint
.setTextSize(mTextSize
);
canvas
.drawCircle(getWidth()/2,getHeight()/2,getWidth()/2,mPaint
);
mPaint
.setColor(Color
.WHITE
);
mPaint
.setTextSize(100);
String text
=String
.valueOf(mNumber
);
mPaint
.getTextBounds(text
,0,text
.length(), 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
);
}
@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