画圆

来源:互联网 发布:软件测试的基本原则 编辑:程序博客网 时间:2024/06/01 08:20

布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout    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="com.example.a1129xia.MainActivity"><com.example.a1129xia.Bao.Myclass    android:layout_width="wrap_content"    android:layout_height="wrap_content" /></LinearLayout>
新建class
package com.example.a1129xia.Bao;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.view.View;/** * author:Created by WangZhiQiang on 2017/11/29. */public class Myclass extends View {    //写一个画笔    private Paint aints=new Paint();    //重写三个方法    public Myclass(Context context) {        super(context);        init();    }    public Myclass(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);        init();    }    public Myclass(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init();    }//设置属性    private void init(){        //给画笔设定一个颜色        aints.setColor(Color.YELLOW);        aints.setStrokeWidth(50);    }    //写一个布局画图行    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);  canvas.drawCircle(getWidth()/2,getHeight()/2,200,aints);        aints.setColor(Color.BLUE);        canvas.drawLine(100,50,200,50,aints);    }}


原创粉丝点击