从源码学习自定义View

从源码来理解自定义View

以下这些内容都是理解自定义View的基础知识点。

首先需要理解ViewGroup的概念,ViewGroup是一个特殊的View,它可以包含其它多个View,这些被包含的View也被称为childView(子View)。

一个ViewGroup可以包含多个子View,同时,这个子View也可以是ViewGroup。
例如,LinearLayout是一个ViewGroup,在里面添加一个Button,则这个Button是LinearLayout的子View。

那么,一个View是如何呈现在手机屏幕上的呢?


View的三大工作流程:

1.[测量流程] onMeasure(int widthMeasureSpec, int heightMeasureSpec)

  • 在谷歌给的源码中,它是这么解释的:Ask all children to measure themselves and compute the measurement of this layout based on the children. 意思是:要求所有的子View测量自己,然后通过这些测量值,计算这个承载所有子View的layout。

  • 这个方法的作用就是用于测量View的尺寸。

2.[布局流程] onLayout(boolean changed, int l, int t, int r, int b)

  • 在谷歌给的源码中,它是这么解释的:Position all children within this layout. 意思是:确定所有子View的位置。

  • 作用即是确定所有View的位置。

3.[绘制流程] onDraw(Canvas canvas)

  • 作用就是将View绘制到屏幕上

1. onMeasure() 需要明白的方法

  • ① public boolean shouldDelayChlidPressedState() {return false;}

    任何一个不需要使用滑动的布局都要使用它,不滚动的子类,一般应该重写此方法并返回false。

  • ② getChildCount()

    返回ViewGroup里所有子View的个数

  • ③ getChlidAt(int)

    返回一个在ViewGroup里指定位置的View

  • ④ getVisibility()

    返回视图的可见性状态,有:VISIBLE/INVISIBLE/GONE

  • ⑤ meassureChildWithMargins(View childView, int parentWidthMeasureSpec,int widthUsed, int parentHeighMeasureSpec, int heightUsed)

    要求子View测量好自己,要求考虑到父容器的宽高,及额外的空间

  • ⑥ LayoutParams

    布局参数类

  • ⑦ childView.getLayoutParams()

    获取子View的布局参数

  • ⑧ childView.getMeasuredWidth()

    以Raw的方式计算获取宽度(自行百度getX和getRawX的区别)

  • ⑨ childView.getMeasuredWidthAndState()

    跟上面一样,但不是以Raw方式

  • ⑩ getSuggestedMinimumHeight()

    返回这个View建议使用的最小高度

  • ⑩① resolveSizeAndState()

    用于创建最终的宽度或高度值,这个助手返回适当的视图所需的尺寸

  • ⑩② 这个方法本身:

    onMeasure(int widthMeasureSpec, int heightMeasureSpec)中的两个形参,即为ViewGroup最初的测量值。

  • ⑩③ setMeasuredDimension(int measuredWidth, int measuredHeight)

    注意:这个方法在最后一定要调用,否则会在测量期间引发一个异常。它用来存储最后测量得到的宽和高。


2. onLayout() 需要明白的方法

  • ① child.layout(left, top, right, bottom)

    摆放这个子View

  • ② 这个方法本身:

    onLayout(boolean changed, int l, int t, int r, int b) 当这个视图应该给每个子View分配一个大小和位置时调用。changed:当View发生改变时,changed=true


3. onDraw() 需要理解的知识点

  • 1.在onDraw方法前创建对象是一个非常重要的优化,View重新绘制的频率非常高,很多绘图的对象需要昂贵的初始化。Android官方建议的屏幕刷新率为60FPS,那么计算下来,每一次调用onDraw方法,可以执行的时间长度仅仅为16ms。在onDraw方法中创建图形对象显著降低性能,会让用户界面变得迟缓。
  • 2.为了正确地画出你自定义的视图,精确的测量很重要,不应该对屏幕上的视图大小做出假设,而是应该通过onMeasure()来进行测量。
  • 3.如果你认为不需要对其尺寸进行特殊控制,只需要重写一个方法: onsizeChanged()
  • 4.onDraw()常用到的方法:
    • 绘制文本:使用drawText(),调用setTypeface()指定字体,文本颜色调用setColor()
    • 绘制shape:使用drawRect()/drawOval()/drawArc(),指定它们为轮廓或者填充,调用setStyle()
    • 绘制更复杂的shape:使用Path类,通过Path对象添加线条或曲线路径,然后drawpath()绘制形状,形状可以为轮廓或充满,调用setStyle()
    • 渐变:定义渐变填充,使用LinearGradient对象,吊桶setShader()来使用这个渐变填充的shape
    • Bitmap:这个直接调用drawBitmap()

看完上述内容后,再来从代码案例中理解上述的知识点:

hongyang大神的博客,Android 手把手教您自定义ViewGroup(一)

一切清晰明了

package com.shou.loginfjame; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import java.util.List; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.xml.bind.util.ValidationEventCollector; import com.shou.LoginUtil.LoginUser; import com.shou.dao.LoginDao; import com.shuo.util.ValidCode; public class LoginFjame extends JFrame implements ActionListener { private JFrame frame = new JFrame("登录"); private JPanel panel = new JPanel(); private JLabel tiel = new JLabel("龍丶逸小说登录系统"); // 创建标题 private JLabel userLabel = new JLabel("用户名:"); // 创建UserJLabel private JTextArea userText=new JTextArea("请输入内容",7, 30); // 获取登录名 private JLabel passLabel = new JLabel("密 码:"); // 创建PassJLabel private JPasswordField passText = new JPasswordField(20); // 密码框隐藏 private JLabel verCodeLa = new JLabel("验证码:"); // 验证码 private JTextField inputCode = new JTextField(); // 验证码框 private ValidCode vcode = new ValidCode(); // 验证码内容 JTextField jt_code; private JButton loginButton = new JButton("登录"); // 创建登录按钮 private JButton registerButton = new JButton("注 册"); // 创建注册按钮 private JButton newPasswordButton = new JButton("忘记密码"); // 创建注册按钮 private JButton exitButton = new JButton("退出"); JTextField field = null; public LoginFjame() { System.out.println("====================================="); System.out.println("== 龍丶逸小说系统 =="); System.out.println("== V1.1.1.0 =="); System.out.println("====================================="); WinLogin(); } public void WinLogin() { panel.setLayout(null); // 设置布局为 null // 创建标题名称 this.tiel.setFont(new Font("宋体", 1, 20)); this.tiel.setBounds(150, 30, 300, 25); this.panel.add(this.tiel); // 创建 UserJLabel this.userLabel.setFont(new Font("宋体", 1, 13)); this.userLabel.setBounds(70, 80, 80, 25); this.panel.add(userLabel); // 创建文本域用于用户输入 this.userText.setBounds(145, 80, 165, 25); this.panel.add(this.userText); // 注册 this.registerButton.setFont(new Font("宋体", 1, 15)); this.registerButton.setContentAreaFilled(false); this.registerButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.registerButton.setBounds(320, 80, 100, 25); this.panel.add(this.registerButton); // 变成小手 this.registerButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 创建PassJLabel this.passLabel.setFont(new Font("宋体", 1, 13)); this.passLabel.setBounds(70, 110, 80, 25); this.panel.add(this.passLabel); // 密码输入框 隐藏 this.passText.setBounds(145, 110, 165, 25); this.panel.add(this.passText); // 忘记密码 this.newPasswordButton.setFont(new Font("宋体", 1, 15)); this.newPasswordButton.setContentAreaFilled(false); this.newPasswordButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.newPasswordButton.setBounds(320, 110, 100, 25); this.panel.add(this.newPasswordButton); // 变成小手 this.newPasswordButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 验证码code this.verCodeLa.setFont(new Font("宋体", 1, 13)); this.verCodeLa.setBounds(70, 140, 80, 25); this.panel.add(this.verCodeLa); // 验证码框 this.inputCode.setBounds(145, 140, 165, 25); this.panel.add(this.inputCode); // 验证码图片 this.vcode.setBounds(320, 140, 165, 25); this.panel.add(this.vcode); System.out.println(this.vcode); // 创建登录按钮 this.loginButton.setFont(new Font("宋体", 1, 15)); this.loginButton.setBounds(95, 190, 80, 25); this.panel.add(this.loginButton); // 变成小手 this.loginButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 退出按钮 this.exitButton.setFont(new Font("宋体", 1, 15)); this.exitButton.setBounds(230, 190, 80, 25); this.panel.add(this.exitButton); // 变成小手 this.exitButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 设置窗体的位置及大小 this.frame.setSize(460, 355); frame.setLocationRelativeTo(null); // 在屏幕中居中显示 frame.add(this.panel); // 添加面板 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置X号后关闭 //设置按钮 this.registerButton.addActionListener(this); //注册按钮 this.newPasswordButton.addActionListener(this); //忘记密码 this.loginButton.addActionListener(this); //登录 this.exitButton.addActionListener(this); //退出 // 往窗体里放其他控件 frame.setVisible(true); // 设置窗体可见 } @Override public void actionPerformed(ActionEvent e) { JButton bt = (JButton) e.getSource(); // 获取按钮信息 String str = bt.getText(); // 获取用户名 String name = this.userText.getText().trim(); // 获取密码 String password = this.passText.getText().trim(); // 获取验证码 String code = this.inputCode.getText().trim(); // 获取jsp验证码 String vcode = this.vcode.getCode(); // 登录 if (str.equals("登录")) { System.out.println("登录"); // 验证码转为大 String Dcode = code.toUpperCase(); String Dvcode = vcode.toUpperCase(); // 验证码判断 if (Dcode.equals(Dvcode)) { //获取页面的用户名 String username=this.userText.getText().trim(); // 根据用户名查看是否有该用户 try { List loginUser=new LoginDao().queryAll(username); String a=loginUser.toString(); System.out.println(a.toString()); if(!a.toString().equals("[]")){ //密码判断 String mysqlPasword=loginUser.get(0).l_password(); if(mysqlPasword.equals(password)){ //登录成功 JOptionPane pane = new JOptionPane("登录成功"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); }else{ JOptionPane pane = new JOptionPane("密码错误错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } }else{ JOptionPane pane = new JOptionPane("用户名错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } /*System.out.println(loginUser.toString()); String sqlUername=loginUser.get(0).getL_username();*/ /*int sqlpassword=loginUser.get(0).getL_power();*/ /*System.out.println("loginF:"+sqlUername);*/ } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { JOptionPane pane = new JOptionPane("验证码错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); System.out.println(dialog.getFont()); dialog.show(); } } else // 退出 if (str.equals("退出")) { System.out.println("退出"); System.exit(0); } else // 注册 if (str.equals("注 册")) { System.out.println("注 册"); } // 注册 else if (str.equals("忘记密码")) { System.out.println("忘记密码"); } else { System.out.println("异常错误"); } } public boolean isValidCodeRight() { System.out.println(this.jt_code.getText()); if (this.jt_code == null) { return false; } if (this.vcode == null) { return true; } if (this.vcode.getCode().equals(this.jt_code.getText())) { return true; } return false; } public static void main(String[] args) { new LoginFjame(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值