android自定义控件实现及其完整的生命周期(一)

【5年Android从零复盘系列之五】关于页面布局控件开发总结 1.Android开发常用布局 LinearLayout 【高频】 RelativeLayout 【高频】 FrameLayout 【较少】 FlexboxLayout 【较少】 ConstraintLayout 【中频】 AbsoluteLayout 【极少】 TableLayout 【极少】 GridLayout 【较少】 NestedScrollView 【中频】 HorizontalScrollView【较少】 PercentRelativeLayout 【较少】使用全解-> Percent 阅读详情

最近打算总结一下自己学习的android技术,就打算写一系列的android学习博客,这是起始篇。
在学习的过程中参考了多位大神的博客,包括:http://blog.csdn.net/lmj623565791/article/details/24252901;http://www.imooc.com/qadetail/117026。

内容可能存在错误的地方,欢迎各位大神指出并留言,不胜感激!!

本篇博客分为两个部分,第一部分就是主要介绍自定义控件的过程;第二部分就是主要介绍自定义控件的生命周期以及加载的过程。

下面介绍第一部分的内容,也就是自定义控件具体的实现过程:
整个应用的结构如图所示:
这里写图片描述

1.自定义控件首先需要定义一个类,我的类是MyTextView,然后重载三个构造函数

public MyTextView(Context context) {
        this(context, null);
        Log.i(TAG,"MyTextView 1");
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
        Log.i(TAG,"MyTextView 2");
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Log.i(TAG,"MyTextView 3");

    }

这里需要注意的是:如果在前两个构造函数中,是没有改为this,仍然使用的super,就会导致出现空指针异常,是因为只调用的第二个构造函数,第三个定义的构造函数没有被调用,没有new出需要的画笔。
这里写图片描述
这里写图片描述

2.在values文件夹下自定义一个attrs.xml资源文件,具体代码如下:

<resources>
    <attr name="Text" format="string"/>
    <attr name="TextColor" format="color"/>
    <attr name="TextSize" format="dimension"/>

    <declare-styleable name=
Android笔记之Android基本控件布局与Activity的生命周期 前言喜欢安卓里千奇百怪的app,酷炫的UI或者好玩的游戏。想知道那些软件是如何做的,为什么我在微博里发一条消息,就能一直存在网上,让别人看到?为什么我点击一个按钮就出现了一个效果,某个功能?为什么滑动屏幕,侧边栏就滑出来了?QQ里拖动消息的“红圆点”就让强迫症患者轻松了许多。那些强大的功能、酷炫的动画效果深深地吸引了我。于是,偏离了自己的专业,开始学习编程。好吧,这并不是在讲我的编程人生 阅读详情

相关推荐

Android 360度自定义摇杆控件的实现

自定义View在Android开发中是增强用户界面交互体验的关键手段。在这一章节中,我们将逐步深入了解自定义View的基本概念和创建流程。首先,我们会探讨什么是自定义View以及它在Android UI框架中的重要性。接着,我们将介绍自定义View的三种主要形式:View,ViewGroup和自定义控件,以及它们各自的应用场景。然后,我们将详细说明自定义View的生命周期,以及如何正确地覆盖和重写关键方法,比如onDrawonMeasure和onLayout。

weixin_42524864的博客 1238

Android抽奖转盘自定义控件实现与实践

抽奖转盘作为一种常见的用户交互形式,在各类应用中广泛存在,用于提升用户体验和用户粘性。自定义抽奖转盘能够更好地贴合应用的主题与风格,同时可以优化性能和用户体验。本章将介绍如何在Android平台实现一个完全自定义的抽奖转盘控件。实现一个自定义抽奖转盘,需要经历以下几个步骤:1.2.1 设计抽奖转盘的数据模型:根据实际需求定义奖品数据和转盘的结构。1.2.2 绘制转盘的UI界面:利用Android的绘图API绘制出转盘的静态界面。1.2.3 实现转盘的旋转逻辑和停止算法。

weixin_42452924的博客 1535

实现Android圆形遮罩图片的CustomImageView自定义控件

本文还有配套的精品资源,点击获取 简介:在Android开发中,自定义控件是创建个性化用户界面常用的方法。本文通过自定义ImageView控件来实现图片的圆形遮罩效果,这个效果对于头像显示或需要突出圆形元素的界面设计非常有用。通过继承ImageView并重写onDraw()方法,结合Bitmap和Canvas技术,实现了将图片转换为圆形。同时,在XML布局中可以像使用标准I...

weixin_32747681的博客 1003

自定义Android RatingBar控件教程与实战

本文还有配套的精品资源,点击获取 简介:Android内置的RatingBar控件在样式上有限,无法满足所有设计需求。因此,本文将探讨如何自定义RatingBar,包括修改其样式、背景图、间距、大小和分数等属性。教程分为创建自定义View类、绘制星星、设置属性、XML布局和自定义属性定义等步骤,详细阐述了实现高度可定制化RatingBar的过程。 1. Andro...

weixin_42603332的博客 1551

View的生命周期

自定义控件在Android开发中是非常重要的,只有回了自定义控件,才能知道view的内部机制,我今天写了一个例子,测试了一下view的生命周期,把demo上传到这里和大家交流学习

Knewton自适应学习平台实战:如何用大数据打造个性化教育体验

本文深入解析Knewton自适应学习平台如何利用大数据技术构建个性化教育体验。通过多维度数据采集、动态认知诊断模型和智能推荐系统,平台能精准识别学生知识盲区,提供定制化学习路径。实战案例显示,该方案显著提升完课率并优化学习效率,展现了自适应学习技术的教育变革潜力。

info6的博客 522

告别时钟树平衡噩梦:在Innovus中巧妙设置stop/ignore pin来优化分段时钟网络

本文深入探讨了在Innovus工具中利用stop/ignore pin优化分段时钟树网络的高阶技巧。针对数字IC后端设计中的时钟树平衡难题,详细解析了特殊引脚属性的战略应用场景和配置方法,帮助工程师有效解决SoC设计中复杂的时钟结构问题,显著提升时序收敛效率。

weixin_30836759的博客 306

揭秘个人开发者接支付宝的‘野路子’:不用企业资质,用Python模拟扫码转账与回调监听

本文探讨了个人开发者如何通过Python模拟支付宝扫码转账与回调监听的技术实现,解析了支付宝URL协议格式,并设计了本地化的支付状态监听方案。文章强调该方法仅用于技术研究,实际应用需遵守支付宝官方接口和金融合规要求。

weixin_30736301的博客 418

论文阅读笔记--Grid Anchor based Image Cropping: A New Benchmark and An Efficient Model

基于网格锚的图片裁剪:一种新的标准和高效的模型摘要1.介绍2.相关工作2.1 图像裁剪数据集和评估指标 摘要 图像裁剪的目的是通过去除图像中无关的内容来改善图像的组成和美学质量。现有的图像裁剪数据库大多只提供一个或多个人工标注的边界框作为基准,难以反映实际中图像裁剪的非唯一性和灵活性。 采用IoU等评价指标也不能可靠地反映裁剪模型的真实性能。本文回顾了图像裁剪的问题,并通过考虑图像裁剪的特殊特性和要求(如局部冗余、内容保存、高宽比),提出了基于网格锚的公式。 我们的公式将候选裁剪窗口的搜索空间从数百万减少到

qq_22565865的博客 2954

基于Python的MOOC课程评价爬取与分析系统设计与实现

本文详细介绍了如何使用Python最新技术栈构建一个高效的MOOC课程评价爬取与分析系统。系统采用Scrapy框架结合Selenium实现动态页面抓取,利用PyMySQL进行数据存储,并通过Pandas和Matplotlib实现数据可视化分析。文章从爬虫原理、技术选型、系统设计到代码实现进行全面讲解,为教育工作者改进教学质量提供数据支持。关键词:Python爬虫、MOOC评价分析、Scrapy框架、数据可视化、教学改进。

2201_76125261的博客 1108

Cadence PCB 仿真宝典【目录】

欢迎大家来到《Cadence PCB 仿真宝典》该专栏包括【入门篇】【提升篇】【技巧篇】【疑难篇】四个部分,以供大家参考。大家直接点击大纲中的蓝色标题即可轻松传送。

fydar的博客 3276

【WorkBuddy专栏13】WB的「记忆系统」是怎么搭建的

【WorkBuddy专栏13】WB的「记忆系统」是怎么搭建的

AI人人懂 1216

《一》:LBE,VR 大空间系列——场景环境需求解析

当前,大量 VR 体验活动在室内场地开展。因此,对于场地照明有特定要求:除天花板外,场地的所有表面均需有照明覆盖。这是为了保证玩家无论处于场地的哪个位置,地面与墙面都不会出现明显的影子。场地内不建议安装射灯或高强度的点光源,若已安装此类灯具,需使用柔光板进行遮盖。此外,要避免在阳光下使用 VR 设备台。参考图(请忽略其中Maker标签)

unitygroup的博客 955

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

上一篇: 使用Eclipse + OpenCV + C++ 环境搭建(二)
下一篇: Android百分比适配
明天的增加
博客等级 码龄11年 22粉丝 71原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值