Android development: inherit View to implement custom View

Android is very open source, and there are many ways to implement custom Views. Inheriting the View class is one of them.

Inheriting the View class needs to implement the constructor super.() method. The super() method has three overloaded methods. They are MyView(Context context), MyView(Context context, AttributeSet attrs) and MyView(Context context, AttributeSet attrs, int defStyleAttr). Let's talk about the difference between the three overloaded methods.
1. MyView (Context context)
This method can be used directly in MainActivity. For example, mainActivity.setContentView(new MyView(this)); but it cannot be used in xml.
2. MyView (Context context, AttributeSet attrs)
This method allows our custom MyView class to set attributes in xml, such as length and width.
3. MyView (Context context, AttributeSet attrs, int defStyleAttr)
This method means that the MyView class can not only set attributes in xml, but also use resource files such as attrs.xml.

Made a simple MyView example:
Write picture description here

We create a new MyView class:

package com.example.aiden.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Aiden on 2016/3/11.
 */
public class MyView extends View implements View.OnClickListener {
    private int time; // 记录点击的次数
    private Paint paint; // 画笔
    private Rect rect; // 矩形对象

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.paint = new Paint(Paint.ANTI_ALIAS_FLAG); // 设置画笔锯齿
        this.rect = new Rect();
        this.time = 0;
    }

    @Override
    public void onDraw(Canvas canvas) {
        // canvas相当于是画布,可以通过画笔在上面绘制
        super.onDraw(canvas);

        paint.setColor(Color.BLUE);
        // 将这个View设置为矩形
        canvas.drawRect(0, 0, this.getWidth(), this.getHeight(), paint);

        paint.setColor(Color.BLACK);
        paint.setTextSize(300);
        // 将内容绘制到rect对象上,用于得到长度和宽度
        paint.getTextBounds(time + "", 0, (time + "").length(), rect);
        // 画笔设置文字水平居中
        paint.setTextAlign(Paint.Align.CENTER);
        Paint.FontMetrics metrics = paint.getFontMetrics();
        // 得到文字高度
        float fontHeight = metrics.bottom - metrics.top;
        // 得到垂直的位置
        float y = this.getHeight() - (this.getHeight() - fontHeight) / 2 - metrics.bottom;
        float x = this.getWidth() / 2;
        canvas.drawText(time + "", x, y, paint);

        this.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        time++;
        // 刷新布局,相当于调用了onDraw(Canvas canvas)函数
        this.invalidate();
    }
}

In activity_main.xml:

<?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.example.aiden.myapplication.MyView
        android:layout_width="200dp"
        android:layout_height="200dp" />

</LinearLayout>

There is nothing to write in MainActivity. Justthis.setContentView(R.layout.activity_main);

Guess you like

Origin blog.csdn.net/new_Aiden/article/details/50860785