onDraw介绍和总结前面两节说的onLayout和onMeasure

onDraw用来来描绘控件是什么样子的

onLayOut、onMeasure、onDraw我觉得调用时机应该是这样子的;

我是一个view,外部的父容器调用我layout,measure,draw方法,

触发我的onLayOut、onMeasure、onDraw方法


我可以再layOut中放置我的子控件的位置,可以再onMeasure中觉得我自身的大小,可以再onDraw中决定我自身的长相


package com.ymtj.test.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by fei on 2015/10/22.
 */
public class MyViewGroup extends ViewGroup {

    // 子View的水平间隔
    private final static int padding = 20;

    public MyViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    /**
     * 是ViewGroup的方法,用于定义这个子控件如何摆放
     * @param changed
     * @param l
     * @param t
     * @param r
     * @param b
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub

        // 动态获取子View实例
        for (int i = 0, size = getChildCount(); i < size; i++) {
            View view = getChildAt(i);
            // 放置子View,宽高都是100
            view.layout(l, t, l + 100, t + 100);
            l += 100 + padding;
        }

    }


    /**
     * 当前这个容器测量的方法,接收这个容器的父亲传过来的可用空间,然后决定当前这个view的大小
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int wMode = MeasureSpec.getMode(widthMeasureSpec);
        int hMode = MeasureSpec.getMode(heightMeasureSpec);
        if(MeasureSpec.AT_MOST == hMode){
            Log.i("main","出现1");
        }
        if(MeasureSpec.EXACTLY == hMode){
            Log.i("main","出现2");
        }
        int widthMeasure = MeasureSpec.getSize(widthMeasureSpec);
        int heightMeasure = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(widthMeasure*4, heightMeasure*4);
    }

    /**
     * 描绘当前view的外观
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        // 首先定义一个paint
        Paint paint = new Paint();
        // 绘制矩形区域-实心矩形
        // 设置颜色
        paint.setColor(Color.WHITE);
        // 设置样式-填充
         paint.setStyle(Paint.Style.FILL);
        // 绘制一个矩形
         canvas.drawRect(new Rect(0, 0, getWidth(), getHeight()), paint);

        // 绘空心矩形
        // 设置颜色
                paint.setColor(Color.RED);
        // 设置样式-空心矩形
                paint.setStyle(Paint.Style.STROKE);
        // 绘制一个矩形
                canvas.drawRect(new Rect(10, 10, 50, 20), paint);

        // 绘文字
        // 设置颜色
                paint.setColor(Color.GREEN);
        // 绘文字
                canvas.drawText("hello", 30, 30, paint);

        // 绘图
        // 从资源文件中生成位图
         //       Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.aa);
        // 绘图
           //     canvas.drawBitmap(bitmap, 10, 10, paint);
        super.onDraw(canvas);
    }
}

<RelativeLayout  android:padding="10dp" android:layout_height="100dp" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">

    <com.ymtj.test.myapplication.MyViewGroup android:layout_height="50dp" android:layout_width="match_parent" android:background="#0000ff">

      <!--  <View android:layout_height="match_parent" android:layout_width="match_parent" android:background="#ff0000" />
        <View android:layout_height="match_parent" android:layout_width="match_parent" android:background="#00ff00" />-->
       </com.ymtj.test.myapplication.MyViewGroup>
</RelativeLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值