源码方式
- public class BanView extends View {
- Paint paint = new Paint();
- public BanView(Context context) {
- super(context);
- }
- public BanView(Context context, @Nullable AttributeSet attrs) {
- super(context, attrs);
- }
- public BanView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- }
- @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
- public BanView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
- super(context, attrs, defStyleAttr, defStyleRes);
- }
- @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- /**
- Canvas 类下的所有 draw- 打头的方法,例如 drawCircle() drawBitmap()。
- Paint 类的几个最常用的方法。具体是:
- Paint.setStyle(Style style) 设置绘制模式
- Paint.setColor(int color) 设置颜色
- Paint.setStrokeWidth(float width) 设置线条宽度
- Paint.setTextSize(float textSize) 设置文字大小
- Paint.setAntiAlias(boolean aa) 设置抗锯齿开关
- */
- /*-------------------------------圆-------------------------------------------*/
- // 基本简单使用 绘制一个圆
- canvas.drawCircle(300, 300, 200, paint);
- //1、设置颜色 Paint.setColor(int color)
- //Paint.setColor(int color) 是 Paint 最常用的方法之一,用来设置绘制内容的颜色。
- // 你不止可以用它画红色的圆,也可以用它来画红色的矩形、红色的五角星、红色的文字。
- paint.setColor(getResources().getColor(R.color.colorAccent));
- //2、设置样式 Paint.setStyle(Paint.Style style)
- //Style 具体来说有三种: FILL, STROKE 和 FILL_AND_STROKE 。
- // FILL 是填充模式
- // STROKE 是画线模式(即勾边模式)
- // FILL_AND_STROKE 是两种模式一并使用:既画线又填充。它的默认值是 FILL,填充模式。
- paint.setStyle(Paint.Style.STROKE);
- //3、Paint.setStrokeWidth(float width)
- // 在 STROKE 和 FILL_AND_STROKE 下,还可以使用 paint.setStrokeWidth(float width) 来设置线条的宽度:
- paint.setStyle(Paint.Style.STROKE);
- paint.setStrokeWidth(20); // 线条宽度为 20 像素
- canvas.drawCircle(300, 300, 200, paint);
- //4、抗锯齿
- // 在绘制的时候,往往需要开启抗锯齿来让图形和文字的边缘更加平滑。
- // 1)开启抗锯齿很简单,只要在 new Paint() 的时候加上一个 ANTI_ALIAS_FLAG 参数就行:
- // 2)也可以使用 Paint.setAntiAlias(boolean aa) 来动态开关抗锯齿。
- Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
- //drawOval(float left, float top, float right, float bottom, Paint paint) 画椭圆
- paint.setStyle(Paint.Style.STROKE);
- paint.setStrokeWidth(20); // 线条宽度为 20 像素
- canvas.drawOval(100, 800, 500, 1000, paint);
- /*-------------------------------圆-------------------------------------------*/
- /*-------------------------------矩形-------------------------------------------*/
- //drawRect(float left, float top, float right, float bottom, Paint paint) 画矩形
- paint.setStyle(Paint.Style.FILL);
- canvas.drawRect(100, 100, 500, 500, paint);
- paint.setStyle(Paint.Style.STROKE);
- canvas.drawRect(700, 100, 1000, 500, paint);
- //另外,它还有两个重载方法 drawRect(RectF rect, Paint paint) 和 drawRect(Rect rect, Paint paint)
- // ,让你可以直接填写 RectF 或 Rect 对象来绘制矩形。
- //drawRoundRect(float left, float top, float right, float bottom,
- //float rx, float ry, Paint paint) 画圆角矩形
- canvas.drawRoundRect(100, 1100, 500, 1300, 50, 50, paint);
- /*-------------------------------矩形-------------------------------------------*/
- /*-------------------------------点-------------------------------------------*/
- //drawPoint(float x, float y, Paint paint) 画点
- Paint paint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
- paint1.setStrokeWidth(20);
- paint1.setStrokeCap(Paint.Cap.SQUARE);
- canvas.drawPoint(50, 50, paint1);
- //drawPoints(float[] pts, int offset, int count, Paint paint)
- //drawPoints(float[] pts, Paint paint) 画点(批量)
- float[] points = {0, 0, 50, 50, 50, 100, 100, 50, 100, 100, 150, 50, 150, 100};
- //绘制四个点:(50, 50) (50, 100) (100, 50) (100, 100)
- canvas.drawPoints(points, 2 /* 跳过两个数,即前两个 0 */,
- 8 /* 一共绘制 8 个数(4 个点)*/, paint1);
- /*-------------------------------点-------------------------------------------*/
- /*-------------------------------线-------------------------------------------*/
- //drawLine(float startX, float startY, float stopX, float stopY, Paint paint) 画线
- canvas.drawLine(400, 600, 800, 800, paint);
- //drawLines(float[] pts, int offset, int count, Paint paint)
- //drawLines(float[] pts, Paint paint) 画线(批量)
- float[] pts = {50, 50, 400, 50,
- 400, 50, 400, 600,
- 400, 600, 50, 600,
- 60, 600, 50, 50};//数据
- paint1.setStrokeWidth((float) 5.0);//线宽
- canvas.drawLines(pts, paint1);
- /*-------------------------------线-------------------------------------------*/
- /*-------------------------------特殊-------------------------------------------*/
- //drawArc(float left, float top, float right, float bottom,
- // float startAngle, float sweepAngle, boolean useCenter, Paint paint) 绘制弧形或扇形
- paint.setStyle(Paint.Style.FILL); // 填充模式
- // 绘制扇形
- canvas.drawArc(200, 1400, 800, 1600, -110, 100, true, paint);
- // 绘制弧形
- canvas.drawArc(200, 1400, 800, 1600, 20, 140, false, paint);
- paint.setStyle(Paint.Style.STROKE); // 画线模式
- // 绘制不封口的弧形
- canvas.drawArc(200, 1400, 800, 1600, 180, 60, false, paint);
- /*-------------------------------特殊-------------------------------------------*/
- }
- }
布局
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <com.templates.ban.diy.BanView
- android:layout_width="wrap_content"
- android:layout_height="0dp"
- android:layout_weight="1"
- />
- </LinearLayout>
效果