Android笔记

本文深入解析了Android中的消息机制(message,handler),包括消息的构造与处理、Handler的功能与使用方法,以及如何通过BitmapFactory和Canvas进行图像资源操作。同时,详细介绍了View的更新机制,包括invalidate()和requestLayout()的区别与应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

消息机制(message, handler)

  message:

    int what;  //用于区分这个消息到底是属于谁来处理的

    int arg1, arg2;  //用于携带int数据类型(相对于setData方法来讲,更省空间)

    Object obj;  //传递引用数据类型(最好用setData方法)

  handler:

    handler处理机制的意义:让新线程周期性地修改UI组件的属性值

    handler的作用:发送与处理信息

    handler的函数:

    • sendEmptyMessage(int what):发送空消息
    • sendMessage(Message msg):立即发送信息
    • void handleMessage(Message msg):处理消息的方法,通常是用于被重写!
    • sendMessageDelayed(Message msg):指定延时多少毫秒后发送信息
    • sendEmptyMessageDelayed(int what,long delayMillis):指定延时多少毫秒后发送空信息
    • final boolean hasMessage(int what):检查消息队列中是否包含what属性为指定值的消息;如果是参数为(int what,Object object):除了判断what属性,还需要判断Object属性是否为指定对象的消息

bitmap:

BitmapFactory:

    BitmapFactory.decodeResource(Resource res,int id)  #获取位图资源

      例如:birdBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bird);

canvas 

  这些函数表示距离的单位是pixel

  获取屏幕尺寸

width = context.getResources().getDisplayMetrics().widthPixels;
height = context.getResources().getDisplayMetrics().heightPixels;
System.out.println(width);
System.out.println(height);

 

View

  View的更新(重绘):

    调用View的成员函数:invalidate() 和 requestLayout()

    invalidate():只会执行onDraw方法,仅View的显示内容发生改变

    requestLayout():只会执行onMeasure方法和onLayout方法,仅View的大小宽高、位置发生改变

 

参考:

  • message:https://blog.youkuaiyun.com/afdasfggasdf/article/details/83058712
  • handler消息处理机制:http://www.runoob.com/w3cnote/android-tutorial-handler-message.html
  • BitmapFactory.decodeResource():https://blog.youkuaiyun.com/Birdmotianlun/article/details/72851626
  • getResource():https://www.cnblogs.com/keyi/p/6282838.html
  • bitmap和BitmapFactory:https://www.cnblogs.com/Free-Thinker/p/6394797.html
  • canvas:https://blog.youkuaiyun.com/qq_41405257/article/details/80487997
  • View的更新:https://blog.youkuaiyun.com/wangwangli6/article/details/79832294
  • 自定义View:https://www.cnblogs.com/jiayongji/p/5560806.html

转载于:https://www.cnblogs.com/sbj123456789/p/10217767.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值