Android自定义View基础

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39424143/article/details/89683560
  • 就像平时一样画图需要纸和笔

    • android中Paint类就是画笔,而Canvas类就是纸(画布)
    • 所以可以得知
      • 笔的属性就是在paint类中设置
      • 画出的形状就会在 canvas中进行绘制
  • 首先实现一个自定义动画–画圆:

package com.example.adminstator.myviewdesign.PaintBasic;


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


/**
* Created with Android Studio.
* Description:
* User: wjx
* Date: 2019-04-29
* Time: 20:44
*/
public class BasicView extends View {
    public BasicView(Context context){
        super(context);
    }
    public BasicView(Context context, AttributeSet attributeSet){
        super(context, attributeSet);
    }
    public BasicView(Context context, AttributeSet attributeSet, int defStyle){
        super(context, attributeSet, defStyle);
    }


    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        //设置画笔的基本属性
        Paint paint = new Paint();
        paint.setColor(Color.RED);//笔迹为红色
        paint.setStyle(Paint.Style.STROKE);//设置为笔刷
        paint.setStrokeWidth(50);//笔粗为50


        //画一个圆
        canvas.drawCircle(190, 200, 150, paint);
    }
}

  • 在主布局文件中引入

<?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">
    <com.example.adminstator.myviewdesign.PaintBasic.BasicView
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>

</LinearLayout>

  • 实现效果:
    e6286b2e0a2fe911af71331e6b79c814.png

  • 画笔的属性设置:

    • setColor()
void setColor(int color)
一种颜色是由红、绿、蓝三色合成出来的,所以参数color只能取8位的 0xAARRGGBB 样式颜色值

A:代表透明度(Alpha) 取值范围为0~255(对应16进制为0x00~0xFF), 取值越小透明度越高,图像也就越透明,当取值0时,完全透明
R: 代表红色值 取值范围为0~255(对应16进制为0x00~0xFF), 取值越小红色越少,当取值0时,红色完全不可见
G: 代表绿色值 取值范围为0~255(对应16进制为0x00~0xFF), 取值越小绿色越少,当取值0时,绿色完全不可见
B: 代表蓝色值 取值范围为0~255(对应16进制为0x00~0xFF), 取值越小蓝色越少,当取值0时,蓝色完全不可见
* setStyle()
void setStyle(Style style)
该函数设置填充样式,对于文字和集合图形都有效,style的取值如下
Paint.Style.FILL: 仅填充内部
Paint.Style.FILL_AND_STROKE: 填充内部和描边
Paint.Style.STROKE:仅描边
* setStokeWidth()
void setStrokeWidth(float width)
用于设置描边的宽度值,单位是px, 当画笔的style样式是STROKE 、FILL_AND_STROKE时有效
  • Canvas使用基础:
void drawColor(itn color)
void drawARGB(int a, int r, int g, int b)
void drawRGB(int r, int g, int b)
其中
1.drawColor()函数中参数color的取值必须是8位的0xAARRGGBB样式颜色值。

2.drawARGB()函数允许分别传入A,R,G,B分量,每个颜色值的取值范围都是0~255(对应16进制),内部会通过这些颜色分量构造出对应的颜色值。

3.drawRGB()函数只允许传入R,G,B分量,透明度Alpha取值为255.
  • 画直线:
void drawLine(float startX, float startY, float stopX, float stopY, Paint paint)
参数:

* startX 起始点X坐标
* startY 起始点Y坐标
* stopX 终点X的坐标
* stopY 终点Y的坐标


canvas.drawLine(100,  100, 200,  200, paint);
  • 画点:
void drawPoint(float x , float y, Paint paint)
参数:
* float x: 点的x坐标
* float y: 点的y坐标

canvas.draw(100, 100, paint);
  • 矩形工具类 RectF、 Rect:
上述两个类都是 矩形类,根据四个点描绘出矩形结构
RectF和Rect中的类和方法完全相同,唯一不同是RectF是用来保存float类型的矩形结构的,而Rect是用来保存int类型的矩形结构的

RectF的构造函数:
RectF()
RectF(float left, float top, float right, float bottom)
RectF(RectF r)
RectF(Rect r)


Rect构造函数:
Rect()
Rect(int left, int top, int right, int bottom)
Rect(Rect r)

eg:
(1)Rect rect = new Rect(10, 10, 100, 100);
(2) Rect rect = new Rect()
    rect.set(10, 10, 100, 100)

  • 矩形绘制方法:
void drawRect(float left, float top ,float right, float bottom, Paint paint)
void drawRect(RectF rect, Paint paint)
void drawRect(Rect r, Paint paint)


canvas.drawRect(10, 10, 100, 100 paint)

RectF rectf = new REctF(210f, 10f, 300f, 100f);
canvas.drawRect(rectf, paint)
  • 颜色:
(1)可以使用Color.XXX来使用相应的颜色

(2)构造颜色:
        * 带有透明度的颜色:
        static int argb(int alpha, int red, int green, int blue)
        
        eg:
        public static int argb(int alpha, int red, int green, int blue){
            return (alpha<<24 | (red<<16) | (green << 8) | blue;
        }
  
  (3) 不带透明度的颜色:
  static int rgb(int red, int green, int blue)
  
  (4)提取颜色分量:
  static  int alpha(int color)
  static int red(int color)
  static int green(int color)
  static int blue(int color)
  
  int green  = Color.green(0xFF000F00)

猜你喜欢

转载自blog.csdn.net/qq_39424143/article/details/89683560