位置: 编程技术 - 正文

android 自定义view之(一) Creating a View Class(Android 自定义view炫酷动画)

编辑:rootadmin
Creating a View Class

推荐整理分享android 自定义view之(一) Creating a View Class(Android 自定义view炫酷动画),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:Android 自定义view 动画,Android 自定义view 动画,Android 自定义view面试题,android自定义view的三大流程,Android 自定义view拖拽缩放旋转,android自定义view的三大流程,android自定义view的三大流程,Android 自定义view炫酷动画,内容如对您有帮助,希望把文章链接给更多的朋友!

A well-designed custom view is much like any other well-designed class. It encapsulates a specific set of functionality with an easy to use interface, it uses CPU and memory efficiently, and so forth. In addition to being a well-designed class, though, a custom view should:

Conform to Android standardsProvide custom styleable attributes that work with Android XML layoutsSend accessibility eventsBe compatible with multiple Android platforms. The Android framework provides a set of base classes and XML tags to help you create a view that meets all of these requirements. This lesson discusses how to use the Android framework to create the core functionality of a view class.Subclass a View

All of the view classes defined in the Android framework extend View. Your custom view can also extend View directly, or you can save time by extending one of the existing view subclasses, such as Button.

To allow the Android Developer Tools to interact with your view, at a minimum you must provide a constructor that takes a Context and an AttributeSet object as parameters. This constructor allows the layout editor to create and edit an instance of your view.

Define Custom Attributes

To add a built-in View to your user interface, you specify it in an XML element and control its appearance and behavior with element attributes. Well-written custom views can also be added and styled via XML. To enable this behavior in your custom view, you must:

Define custom attributes for your view in a resource element 自定义attributes属性 Specify values for the attributes in your XML layout 在xml中指定属性值 Retrieve attribute values at runtime 在runtime 取回属性Apply the retrieved attribute values to your view 在自定义view上应用取回的属性值 This section discusses how to define custom attributes and specify their values. The next section deals with retrieving and applying the values at runtime.

To define custom attributes, add <declare-styleable> resources to your project. It’s customary to put these resources into ares/values/attrs.xml file. Here’s an example of an attrs.xml file:

This code declares two custom attributes, showText and labelPosition, that belong to a styleable entity named PieChart. The name of the styleable entity is, by convention, the same name as the name of the class that defines the custom view. Although it’s not strictly necessary to follow this convention, many popular code editors depend on this naming convention to provide statement completion.

Once you define the custom attributes, you can use them in layout XML files just like built-in attributes. The only difference is that your custom attributes belong to a different namespace. Instead of belonging to the they belong package name]. For example, here’s how to use the attributes defined for PieChart:

In order to avoid having to repeat the long namespace URI, the sample uses an xmlns directive. This directive assigns the alias custom to the namespace You can choose any alias you want for your namespace.

android 自定义view之(一)  Creating a View Class(Android 自定义view炫酷动画)

Notice the name of the XML tag that adds the custom view to the layout. It is the fully qualified name of the custom view class. If your view class is an inner class, you must further qualify it with the name of the view’s outer class. further. For instance, the PieChart class has an inner class called PieView. To use the custom attributes from this class, you would use the tag com.example.customviews.charting.PieChart$PieView.

Apply Custom Attributes 应用自定义属性

When a view is created from an XML layout, all of the attributes in the XML tag are read from the resource bundle and passed into the view’s constructor as an AttributeSet. Although it’s possible to read values from the AttributeSet directly, doing so has some disadvantages:

Resource references within attribute values are not resolvedStyles are not applied Instead, pass the AttributeSet to obtainStyledAttributes(). This method passes back a TypedArray array of values that have already been dereferenced and styled.

The Android resource compiler does a lot of work for you to make calling obtainStyledAttributes() _easier. For each <declare-styleable> resource in the res directory, the generated R.java defines both an array of attribute ids and a set of constants that define the index for each attribute in the array. You use the predefined constants to read the attributes from the _TypedArray. Here’s how the PieChart class reads its attributes:

Note that TypedArray objects are a shared resource and must be recycled after use.

Add Properties and Events 添加属性和事件

Attributes are a powerful way of controlling the behavior and appearance of views, but they can only be read when the view is initialized. To provide dynamic behavior, expose a property getter and setter pair for each custom attribute. The following snippet shows how PieChart exposes a property called showText:

Notice that setShowText calls invalidate() and requestLayout() (记住setter方法里调用 invalidate() 跟 requstLayout()方法). These calls are crucial to ensure that the view behaves reliably. You have to invalidate the view after any change to its properties that might change its appearance, so that the system knows that it needs to be redrawn. Likewise, you need to request a new layout if a property changes that might affect the size or shape of the view. Forgetting these method calls can cause hard-to-find bugs.

Custom views should also support event listeners to communicate important events. (只是事件监听)For instance, PieChart exposes a custom event called OnCurrentItemChanged to notify listeners that the user has rotated the pie chart to focus on a new pie slice.

It’s easy to forget to expose properties and events(不要忘记暴露属性跟事件), especially when you’re the only user of the custom view. Taking some time to carefully define your view’s interface reduces future maintenance costs. A good rule to follow is to always expose any property that affects the visible appearance or behavior of your custom view.

Design For Accessibility(易接近)

Your custom view should support the widest range of users. This includes users with disabilities that prevent them from seeing or using a touchscreen. To support users with disabilities, you should:

Label your input fields using the android:contentDescription attributeSend accessibility events by calling sendAccessibilityEvent() when appropriate.Support alternate controllers, such as D-pad and trackball For more information on creating accessible views, see Making Applications Accessible in the Android Developers Guide.

Android 四种点击事件 Android四种点击事件xml文件代码如下:(1--3均可用)Buttonandroid:id=@id/button1android:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:text=Button1/Buttonandroid:id=@id

activity通信 上节说到,Activity是什么;这次来了解了解Activity通信相关内容。都知道,人与人交流产生许多不可预知的美。因为交流,信息得意传递;因为交流,任

android轻量级开源缓存框架——ASimpleCache(ACache)源码分析 转载请注明出处:

标签: Android 自定义view炫酷动画

本文链接地址:https://www.jiuchutong.com/biancheng/383226.html 转载请保留说明!

上一篇:AndroidAnnotations框架Eclipse下的配置

下一篇:Android 四种点击事件(android点击事件onclick用法)

  • 盈利能力的概念及内容
  • 已注册登记的机动车有什么情形的
  • 法人向公司账户汇款怎么做账
  • 关联交易纳税调整期限
  • 发票抬头公司名称有空格
  • 印花税没有在我的待办里是不是就不用申报
  • 合伙企业的合伙协议
  • 销毁会记凭证
  • 外币投入的资本
  • 附加税按实际缴纳计提吗
  • 制造业交新型墙体材料专项基金款分录
  • 社会团体收到的投资款怎么入账
  • 减少注册资本弥补亏损的依据
  • 单位购置汽车如何入账
  • 从个人出开进的房租租赁发票可以抵扣几个点?
  • 企业法人个人帐户是什么
  • 调整已结转的税种有哪些
  • 护栏发票税点
  • 增值税普票需要进项吗
  • 累计折旧费用
  • 汇算清缴已退税的还可以作废吗
  • 外包物流适用范围
  • 企业空气检测费应该计入什么会计科目核算?
  • 代理进口业务受托方账务处理流程
  • 商业写字楼
  • 别的公司代缴社保合法
  • wim文件用什么软件打开
  • 投资长期溢价债券,容易获取投资收益
  • PHP:xml_get_current_column_number()的用法_XML解析器函数
  • 程序员编程代码大全
  • 胆固醇为什么会高
  • 美团提现手续费入哪个会计科目
  • 有关五险一金的知识
  • 绿萝怎么修剪才能更旺盛
  • php怎么执行sql语句
  • php assign
  • 深入理解php内核
  • 物流中的代收货款是什么意思
  • vue中使用nodejs
  • 申报系统异常
  • 用友软件怎么删除已经建立的账套
  • 公章的法律效力范围
  • yolov3实例
  • js 数组中的重数
  • 收到对方退回的现金
  • 工地购买的厨房用品计入哪个科目
  • 有效税额可以更改吗
  • 企业分红所得税
  • 担保公司的风险准备金在报表中如何反应
  • 小规模纳税人是简易计税还是一般计税
  • 应付职工薪酬的含义
  • 一般纳税人的增值税税率
  • 一般风险准备的科目类别
  • 可供出售权益工具公允价值的增加计入当期损益吗
  • 营改增对建筑业税负的影响
  • 结转销项税额至未交增值税的结转系数
  • 审计 调整分录
  • 连锁药店成本会计
  • 汇算清缴弥补以前年度亏损多少年
  • 高新企业研发项目规定几个
  • 作废发票丢失怎么补办
  • 利息税额计算公式
  • 开设明细账
  • sql语句错误提示
  • windows xp开机按f12是什么操作
  • win10系统开机蓝屏怎么修复
  • Win10 Mobile Build 10572 其它未记录更新内容汇总
  • macbook key
  • scanserver.exe - scanserver是什么进程 有什么用
  • win7系统无法创建分区也无法定位
  • 工商网银安装
  • js时间范围
  • css 之 background-position-x
  • Html5+jQuery+CSS制作相册小记录
  • cssimport
  • node.js的exports、module.exports与ES6的export、export default深入详解
  • js实现左右拖动功能
  • python汉字
  • python的介绍
  • 卷式发票如何查验真伪
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设