一、前言

上节 讲解了旋转圆环基本的实现方法。本文将在此基础上进一步改进,在属性文件中自定义控件属性,避免代码中显式调用setXXX() 方法。

二、流程

首先,在资源文件 values 中新建一个 attr.xml,其中定义了我们即将使用的几个旋转环的属性,如下所示

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="RotatingRing">
<attr name="width" format="integer"/>
<attr name="radius" format="integer"/>
<attr name="start_angle" format="integer"/>
<attr name="sweep_angle" format="integer"/>
<attr name="start_color" format="color"/>
<attr name="end_color" format="color"/>
<attr name="color" format="color"/>
<attr name="duration" format="integer"/>
</declare-styleable>
</resources>

在 RotatingRing 类中,添加第三个构造函数 (含3个参数的),如下所示

public RotatingRing(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

根据属性清单获取属性值

public RotatingRing(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RotatingRing);
radius = typedArray.getInt(R.styleable.RotatingRing_radius, 100);
width = typedArray.getInt(R.styleable.RotatingRing_width, 20);
startAngle = typedArray.getInt(R.styleable.RotatingRing_start_angle, 0);
sweepAngle = typedArray.getInt(R.styleable.RotatingRing_sweep_angle, 270);
startColor = typedArray.getColor(R.styleable.RotatingRing_start_color, Color.WHITE);
endColor = typedArray.getColor(R.styleable.RotatingRing_end_color, typedArray.getColor(R.styleable.RotatingRing_color, Color.BLUE));
duration = typedArray.getColor(R.styleable.RotatingRing_duration, 1000);
typedArray.recycle();
}

由上所述,默认值可以被第三个构造函数所定义,故可以去除第一个构造函数里定义的的默认值,改进后的构造函数如下所示

public RotatingRing(Context context) {
this(context, null);
} public RotatingRing(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
} public RotatingRing(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RotatingRing);
radius = typedArray.getInt(R.styleable.RotatingRing_radius, 100);
width = typedArray.getInt(R.styleable.RotatingRing_width, 20);
startAngle = typedArray.getInt(R.styleable.RotatingRing_start_angle, 0);
sweepAngle = typedArray.getInt(R.styleable.RotatingRing_sweep_angle, 270);
startColor = typedArray.getColor(R.styleable.RotatingRing_start_color, Color.WHITE);
endColor = typedArray.getColor(R.styleable.RotatingRing_end_color, typedArray.getColor(R.styleable.RotatingRing_color, Color.BLUE));
duration = typedArray.getColor(R.styleable.RotatingRing_duration, 1000);
typedArray.recycle();
}

到此,可以去除 MainActivity 中的 RotatingRing.setXXX()方法,而在 activity_main.xml 里定义属性

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <draw.bitmap.canvas.RotatingRing
android:id="@+id/rotating_ring"
android:layout_width="400dp"
android:layout_height="400dp" app:width="30"
app:radius="150"
app:start_angle="0"
app:sweep_angle="270"
app:duration="1500"
app:start_color="@color/colorWhite"
app:end_color="@color/colorRed" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>

其中‘app’为原布局文件里定义好的命名空间,这里我直接拿来用了,也可以自己定义命名空间,只须在布局文件头加上

xmlns:命名空间="http://schemas.android.com/apk/res-auto"

这里我为区分定义了一个红色的圆环,而其他参数与原先 MainActivity 里setXXX() 方法里的参数一致,修改后的代码为

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { RotatingRing rotatingRing; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rotatingRing = findViewById(R.id.rotating_ring);
rotatingRing.startAnim();
}
}

运行效果

到此已经初步掌握了自定义view的一些必要性质,下一节将深入了解一些高级属性以及绘制相关源码等。

Android 自定义View (三)的更多相关文章

  1. Android 自定义View (三) 圆环交替 等待效果

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24500107 一个朋友今天有这么个需求(下图),我觉得那自定义View来做还是很 ...

  2. Android 自定义 view(三)—— onDraw 方法理解

    前言: 上一篇已经介绍了用自己定义的属性怎么简单定义一个view<Android 自定义view(二) -- attr 使用>,那么接下来我们继续深究自定义view,下一步将要去简单理解自 ...

  3. Android 自定义View及其在布局文件中的使用示例(三):结合Android 4.4.2_r1源码分析onMeasure过程

    转载请注明出处 http://www.cnblogs.com/crashmaker/p/3549365.html From crash_coder linguowu linguowu0622@gami ...

  4. Android自定义View(三、深入解析控件测量onMeasure)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51490283 本文出自:[openXu的博客] 目录: onMeasure什么时候会被调用 ...

  5. 【朝花夕拾】Android自定义View篇之(四)自定义View的三种实现方式及自定义属性使用介绍

    前言 转载请声明,转自[https://www.cnblogs.com/andy-songwei/p/10979161.html],谢谢! 尽管Android系统提供了不少控件,但是有很多酷炫效果仍然 ...

  6. Android自定义View 画弧形,文字,并增加动画效果

    一个简单的Android自定义View的demo,画弧形,文字,开启一个多线程更新ui界面,在子线程更新ui是不允许的,但是View提供了方法,让我们来了解下吧. 1.封装一个抽象的View类   B ...

  7. Android 自定义View合集

    自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...

  8. Android 自定义View (五)——实践

    前言: 前面已经介绍了<Android 自定义 view(四)-- onMeasure 方法理解>,那么这次我们就来小实践下吧 任务: 公司现有两个任务需要我完成 (1)监测液化天然气液压 ...

  9. Android 自定义 view(四)—— onMeasure 方法理解

    前言: 前面我们已经学过<Android 自定义 view(三)-- onDraw 方法理解>,那么接下我们还需要继续去理解自定义view里面的onMeasure 方法 推荐文章: htt ...

  10. Android 自定义view(二) —— attr 使用

    前言: attr 在前一篇文章<Android 自定义view -- attr理解>已经简单的进行了介绍和创建,那么这篇文章就来一步步说说attr的简单使用吧 自定义view简单实现步骤 ...

随机推荐

  1. Django Cannot assign "A1": "B1" must be a "C1" instance.

    Django Cannot assign "A1": "B1" must be a "C1" instance. 原因:使用了外键 说明:如 ...

  2. 更换CentOS的下载源为阿里云

    阿里Linux镜像地址:http://mirrors.aliyun.com/ 1.备份mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/Cen ...

  3. 第14章 Windows管理规范

    第14章 Windows管理规范 我们一直期望但是又害怕写这一章.Windows管理规范(Windows Management Instrumentation,WMI)可能是微软提供给管理员使用最优秀 ...

  4. PostProcess

    后处理器: AutowiredAnnotationBeanPostProcess.class 可以处理@Autowired.@Value 如何注册:context.registerBean(xxx.c ...

  5. 知识图谱demo net core + neo4j

    知识图谱 基于neo4j数据库和.net core开发环境 Neo4j数据库的安装 安装neo4j数据库之前,需安装java的JDK文件,安装版本需与数据库保持一致,较新的neo4j数据库都需要至少j ...

  6. gRPC中protobuff type和C# type原生标量对应表

    查看Microsoft官方文档: Create Protobuf messages for .NET apps | Microsoft Docs

  7. 模块二:os模块、sys模块、json模块、pickle模块,包

    os模块 1 os模块是与操作系统交互的一个接口 2 import os: 3 1.获取当前工作目录 4 print(os.getcwd()) 5 6 2.切换到D:\yuan文件目录 7 os.ch ...

  8. git添加Github、Gitee、Gitlab秘钥

    傻逼兮兮的我以为这三个的秘钥都是通用的 参考原文:https://www.cnblogs.com/zmdComeOn/p/11748991.html 首先确认已安装Git,可以通过 git –vers ...

  9. pytorch学习笔记(8)--现有模型的使用和修改

    官网网址: https://pytorch.org/vision/0.9/models.html#semantic-segmentation (1).ImageNet train_data = tor ...

  10. bigdecimal 比较大小、bigdecimal 数学运算、bigdecimal 精度

    创建 BigDecimal 建议使用 public BigDecimal(String val),使用 number 参数可能会有精度问题 设置精度 setScale(3, BigDecimal.RO ...