Android 实现一个简单的自定义View
来源:互联网 发布:pkm2软件 编辑:程序博客网 时间:2024/06/11 11:19
自定义View的相关文章:
- Android 实现一个简单的自定义View
- Android 自定义View步骤
- Android Paint详解
- Android 自定义View之Canvas相关方法说明
- Android 自定义View实例之 “京东跑”
- Android 自定义View实例之进度圆环
- Android 源码分析(TextView)
- Android 自定义View分发流程
- Android 自定义View 需要注意的事项
我们实现这样一个自定义View,绘制一个圆,半径和颜色可以通过属性设置。
public class MyView extends View { /** * 圆的半径,默认画出最大圆 */ private float radius; /** * 圆的颜色 */ private int color; private Paint mPaint; public MyView(Context context) { super(context); } public MyView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); //获取设置的属性值 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyView); radius = typedArray.getDimension(R.styleable.MyView_radius, 0); color = typedArray.getColor(R.styleable.MyView_android_color, Color.RED); typedArray.recycle(); //初始化Paint mPaint = new Paint(); mPaint.setColor(color); } public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //计算半径,如果属性中设置了半径则使用设置的值,否则是宽和高一半的较小值 float mRadius = radius == 0 ? Math.min(getWidth() / 2, getHeight() / 2) : radius; //绘制圆 canvas.drawCircle(getWidth() / 2, getHeight() / 2, mRadius, mPaint); }}
在res/values目录下创建attrs.xml,这里面我们定义了我们需要的属性:
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="MyView"> <attr name="radius" format="dimension"></attr> <attr name="android:color"/> </declare-styleable></resources>
使用:
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.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="qiwei.com.frameworknote.ui.MainActivity"> <qiwei.com.frameworknote.ui.MyView android:layout_width="100dp" android:layout_height="100dp" app:radius="20dp" android:color="@color/colorPrimaryDark"/></android.support.constraint.ConstraintLayout>
所以自定义View实现的步骤:
- 创建View并继承View
- 设置View属性
- 获取View属性值
- 初始化Paint(Paint用于绘制)
- 重写onMesure方法,不是必须的,此View未写,后面会详细介绍
重写onDraw方法
自定义View重点在onDraw方法,这个方法里面我们要绘制出界面下一篇文章我们会详细的介绍各个步骤。
阅读全文
0 0
- Android 实现一个简单的自定义View
- Android下 一个自定义VIEW实现简单的弹幕效果
- 简单实现一个自定义view的ProgressBar
- 一个简单的Android自定义view详解
- 一个简单的Android自定义View
- Android简单自定义view的实现
- Android简单实现自定义View
- 简单的实现自定义View画一个钟表
- Android实现一个简单的自定义适配器
- Android自定义简单的View
- Android自定义简单的View
- Android自定义简单的View
- android 自定义一个简单View总结
- android:滑动挂断自定义View的简单实现
- Android自定义View----时钟/仪表盘的简单实现
- Android自定义View实现简单的折线图、柱状图
- Android利用自定义View实现简单的足球战术板
- android 自定义一个View的基础实现方式
- dom4j解析xml
- 认识js函数对象(Function Object)
- linux系统/var/log目录下的信息详解
- annotation processor tool(apt)的套路
- EFM32片内外设CMU -- 设置时钟输出
- Android 实现一个简单的自定义View
- redis的常用命令(1)
- Spring Cloud(六):链路追踪Sleuth与Zipkin结合
- U3D 遇到的 object reference not set to an insance 原因及解决方法
- java 代理ip
- KafkaOffsetMonitor
- 【剑指Offer】最小的k个数
- Dlib库landmark算法解析
- 福利来啦!Apsara Clouder大数据专项技能认证:使用Quick BI 制作企业数据分析报表