位置: 编程技术 - 正文

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用法)

  • 个体工商户的免税政策
  • 美国个税计算器2021计算器
  • 预提费用在资产负债表里写在哪
  • 哪些科目会影响损益
  • 发票没金额能开吗
  • 季度申报利润表本月数怎么填
  • 月末在产品的原材料定额费用计算公式
  • 如何区别文化体育业和娱乐业
  • 成立企业的步骤
  • 允许税前扣除的利息支出
  • 补贴收入计入哪个科目
  • 补开的银行手续费发票怎么做账
  • 应付企业负担的职工社会保险费
  • 公司支付倒垃圾费怎么做会计分录
  • 发行股票所支付的手续费计入
  • 没有订立合同的著作权属于什么人
  • 可以给行政事业单位开增值税专用发票吗
  • 建筑合同通用条款
  • 什么情况下可以收取现金
  • 对公账户分为一般户和基本户
  • 有限责任公司企业名称有哪些
  • 发票密码区出格了怎么调整
  • 发票抵扣后对方恶意冲红
  • 新租赁准则承租人计提折旧吗
  • 期望值E(X)怎么算例题
  • 母子公司之间的借款利息支出增值税能否抵扣
  • 解决的英文
  • win7更改电脑设置在哪里
  • win10锁屏界面windows聚焦什么意思
  • 存出投资款是什么凭证
  • 企业注销时实收资本为0
  • 事业单位收到奖励款怎么入账
  • 增值税的滞纳金怎么做账
  • tracert命令的作用及用法
  • 一般纳税人企业所得税税率多少
  • 发票支票区别
  • 土地增值税清算扣除项目
  • 日本北海道 (© Hiroshi Yokoyama/eStock Photo)
  • 麦地那老城
  • php限制登录次数
  • 如何自定义smartart
  • 深度学习环境配置(pytorch版本)----超级无敌详细版(有手就行)
  • php自动部署
  • 京东到家的物流模式
  • 生日快乐html5代码
  • 交易性金融资产属于什么科目
  • 佣金代扣代缴增值税如何计算
  • 贷款的利息可以不还吗
  • 抄税期一般是几天
  • 金蝶做账流程视频
  • 药品的增值税税率13%还是17%?
  • 走账犯罪吗
  • 采购暂估业务处理流程
  • 可供出售金融资产新准则叫什么
  • 免缴土地使用税是不是不用交税
  • 应付账款与实际付款有差异如何分录
  • 股东出资资本金可以是问别人借来的吗
  • 当月发票是否需要当月支付
  • 企业退款给个人多久到账
  • 合资公司政策
  • 担保属于债权吗
  • 财政拨付注册资金怎么填
  • 工程公司本月没发工资
  • 私营企业归谁管
  • sql分页查询怎么实现
  • 最全的sql执行顺序
  • deepin 2014系统下安装mysql数据库的方法步骤
  • ubuntu搭建网站教程
  • linux网络协议栈内核分析
  • 快速查看浏览的英语短语
  • 在Linux系统中如何打开R语言控制台
  • linux 测试工具
  • win10系统家庭版和专业版区别
  • 用python编写登录程序
  • shell脚本对比两个文件夹内的内容
  • python爬虫过程
  • jquery常见问题
  • 公司代扣税怎么办理
  • 如何打印纳税申报表
  • 企业所得税优惠事项管理目录2023
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设