android自定义View用法

自定义View用法

    • 效果图
    • 布局文件
    • 重写view
    • Java代码
    • 效果图二
    • 布局文件
    • 重写view
    • Java代码
    • 效果图三
    • 布局文件
    • 重写view
    • Java代码

效果图

android自定义View用法_第1张图片

布局文件

// An highlighted block
<?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"
    android:orientation="vertical"
    tools:context="bw.com.Unit4.User_DefinedActivity">

    <bw.com.Unit4.CoustomView
        android:id="@+id/my_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

重写view

// An highlighted block
package bw.com.Unit4;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class CoustomView extends View {

    Paint paint;

    public CoustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        initPaint();

    }
    //初始化 画笔
    private void initPaint() {
        paint = new Paint();

        //设置样式
        /*
        * STROKE: 描边不填充
        * FILL:填充
        * FILL_AND_STROKE: 描边且填充
        * */
        paint.setStyle(Paint.Style.STROKE);
        //设置颜色
        paint.setColor(Color.RED);
        //抗锯齿
        paint.setAntiAlias(true);
        //设置大小   如果设置为0时  默认1px
        paint.setStrokeWidth(5);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //划线
        canvas.drawLine(0,0,100,100,paint);

        //矩形
        RectF rectF1 = new RectF(100,100,200,200);
        canvas.drawRect(rectF1,paint);

        //圆形  属性一:圆心点的x轴 属性二:圆心点的y轴 属性三:半径  属性4:画笔
        canvas.drawCircle(250,250,70,paint);

        //扇形
        RectF rectF2 = new RectF(350,350,400,400);
        canvas.drawArc(rectF2,0,100,true,paint);

        //
        RectF rectF3 = new RectF(100,200,500,500);
        canvas.drawOval(rectF3,paint);

    }
}

Java代码

// An highlighted block
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import bw.com.Unit1.R;

    public class User_DefinedActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user__defined);

        CoustomView view = findViewById(R.id.my_view);
        //开启线程
//        new Thread(view).start();

    }
}

效果图二

android自定义View用法_第2张图片

布局文件

// An highlighted block
<?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"
    android:orientation="vertical"
    tools:context="bw.com.Unit4.User_DefinedActivity">

    <bw.com.Unit4.User_View
        android:id="@+id/my_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

重写view

// An highlighted block
package bw.com.Unit4;

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.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

public class User_View extends View implements Runnable{

    private Context context;
    private Paint paint;

    private int height;
    private int width;

    private int radiu;

    public User_View(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        this.context = context;

        initPaint();

    }
    //初始化画笔和获取屏幕的宽高
    private void initPaint() {


        //获取屏幕管理
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        //获取屏幕度量
        DisplayMetrics displayMetrics = new DisplayMetrics();
        //关联
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        //获取屏幕的宽高
        height = displayMetrics.heightPixels;
        width = displayMetrics.widthPixels;

        //初始化画笔
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(20);
        paint.setColor(Color.BLUE);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawCircle(width/2,height/2,radiu,paint);

    }

    @Override
    public void run() {
        while (true){
            //重绘的函数
//            invalidate();
            if (radiu < 500){
                radiu +=20;
            }else{
                radiu = 20;
            }
            //异步线程重绘的函数
            postInvalidate();

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}

Java代码

// An highlighted block
package bw.com.Unit4;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import bw.com.Unit1.R;

    public class User_DefinedActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user__defined);

        User_View view = findViewById(R.id.my_view);
        //开启线程
        new Thread(view).start();

    }
}

效果图三

android自定义View用法_第3张图片

布局文件

// An highlighted block
<?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"
    android:orientation="vertical"
    tools:context="bw.com.Unit4.User_DefinedActivity">

    <bw.com.Unit4.ElectricFanView
        android:id="@+id/my_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

重写view

// An highlighted block
package bw.com.Unit4;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class ElectricFanView extends View implements Runnable{

    private Paint paintR;
    private Paint paintB;
    private Paint paintY;
    private Paint paintG;

    private int height;
    private int width;

    //扇形的起始角度
    private int sweepAngleB;
    private int sweepAngleR;
    private int sweepAngleY;


    public ElectricFanView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        initPaint();

    }
    //初始化画笔
    private void initPaint() {
        //初始化画笔
        paintB = new Paint();
        paintB.setAntiAlias(true);
        paintB.setStyle(Paint.Style.FILL);
        paintB.setStrokeWidth(20);
        paintB.setColor(Color.BLUE);

        paintR = new Paint();
        paintR.setAntiAlias(true);
        paintR.setStyle(Paint.Style.FILL);
        paintR.setStrokeWidth(20);
        paintR.setColor(Color.RED);

        paintY = new Paint();
        paintY.setAntiAlias(true);
        paintY.setStyle(Paint.Style.FILL);
        paintY.setStrokeWidth(20);
        paintY.setColor(Color.YELLOW);

        paintG = new Paint();
        paintG.setAntiAlias(true);
        paintG.setStyle(Paint.Style.FILL);
        paintG.setStrokeWidth(10);
        paintG.setColor(Color.BLACK);

    }
    //初始化圆点
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        this.height = h;
        this.width = w;


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //画椭圆
        RectF rectFG = new RectF(width/2-50,height/2-125,width/2+50,height/2-75);
        canvas.drawOval(rectFG,paintG);

        //画线
        canvas.drawLine(width/2,height/2,width/2,height/2-100,paintG);

        //设置扇形
        RectF rectFB = new RectF(-(width/4),-(width/4),(width/4),(width/4));
        //                                   sweepAngle扇形扫过的弧度
        canvas.translate(width/2,height/2);
        canvas.drawArc(rectFB,sweepAngleB,30,true,paintB);
        canvas.drawArc(rectFB,sweepAngleR,30,true,paintR);
        canvas.drawArc(rectFB,sweepAngleY,30,true,paintY);



    }

    @Override
    public void run() {
        while (true){
            sweepAngleB+=1;
            sweepAngleR = sweepAngleB+120;
            sweepAngleY = sweepAngleR+120;
            //重绘构造方法
            postInvalidate();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

Java代码

// An highlighted block
package bw.com.Unit4;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import bw.com.Unit1.R;

    public class User_DefinedActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user__defined);

        ElectricFanView view = findViewById(R.id.my_view);
        //开启线程
        new Thread(view).start();

    }
}

你可能感兴趣的:(Android)