Android自定义View之一:初探实例

卷积神经网络-猫狗识别(附源码) 利用python实现一个卷积神经网络,训练猫狗数据集,实现对猫狗图片的预测识别 阅读详情

http://www.cnblogs.com/ufocdy/archive/2011/05/17/2048958.html

Android自定义View实现很简单

继承View,重写构造函数、onDraw,(onMeasure)等函数。

如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml。在其中定义你的属性。

在使用到自定义View的xml布局文件中需要加入xmlns:前缀="http://schemas.android.com/apk/res/你的自定义View所在的包路径".

在使用自定义属性的时候,使用前缀:属性名,如my:textColor="#FFFFFFF"。

实例:

 

  1. package demo.view.my;   
  2. import android.content.Context;   
  3. import android.content.res.TypedArray;   
  4. import android.graphics.Canvas;   
  5. import android.graphics.Color;   
  6. import android.graphics.Paint;   
  7. import android.graphics.Paint.Style;   
  8. import android.util.AttributeSet;   
  9. import android.view.View;   
  10. /**  
  11.  * 这个是自定义的TextView.  
  12.  * 至少需要重载构造方法和onDraw方法  
  13.  * 对于自定义的View如果没有自己独特的属性,可以直接在xml文件中使用就可以了  
  14.  * 如果含有自己独特的属性,那么就需要在构造函数中获取属性文件attrs.xml中自定义属性的名称  
  15.  * 并根据需要设定默认值,放在在xml文件中没有定义。  
  16.  * 如果使用自定义属性,那么在应用xml文件中需要加上新的schemas,  
  17.  * 比如这里是xmlns:my="http://schemas.android.com/apk/res/demo.view.my"  
  18.  * 其中xmlns后的“my”是自定义的属性的前缀,res后的是我们自定义View所在的包  
  19.  * @author Administrator  
  20.  *  
  21.  */  
  22. public class MyView extends View {   
  23.        
  24.     Paint mPaint; //画笔,包含了画几何图形、文本等的样式和颜色信息   
  25.     public MyView(Context context) {   
  26.         super(context);   
  27.            
  28.     }   
  29.        
  30.     public MyView(Context context, AttributeSet attrs){   
  31.         super(context, attrs);   
  32.         mPaint = new Paint();   
  33.         //TypedArray是一个用来存放由context.obtainStyledAttributes获得的属性的数组   
  34.         //在使用完成后,一定要调用recycle方法   
  35.         //属性的名称是styleable中的名称+“_”+属性名称   
  36.         TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);   
  37.         int textColor = array.getColor(R.styleable.MyView_textColor, 0XFF00FF00); //提供默认值,放置未指定   
  38.         float textSize = array.getDimension(R.styleable.MyView_textSize, 36);   
  39.         mPaint.setColor(textColor);   
  40.         mPaint.setTextSize(textSize);   
  41.            
  42.         array.recycle(); //一定要调用,否则这次的设定会对下次的使用造成影响   
  43.     }   
  44.        
  45.     public void onDraw(Canvas canvas){   
  46.         super.onDraw(canvas);   
  47.         //Canvas中含有很多画图的接口,利用这些接口,我们可以画出我们想要的图形   
  48.         //mPaint = new Paint();   
  49.         //mPaint.setColor(Color.RED);   
  50.         mPaint.setStyle(Style.FILL); //设置填充   
  51.         canvas.drawRect(10, 10, 100, 100, mPaint); //绘制矩形   
  52.            
  53.         mPaint.setColor(Color.BLUE);   
  54.         canvas.drawText("我是被画出来的", 10, 120, mPaint);   
  55.     }   
  56. }  

 

相应的属性文件:

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="MyView">  
  4.         <attr name="textColor" format="color"/>  
  5.         <attr name="textSize" format="dimension"/>  
  6.     </declare-styleable>  
  7. </resources>  

 

在布局文件中的使用:

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.               xmlns:my="http://schemas.android.com/apk/res/demo.view.my"    
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     >  
  8.        
  9.     <demo.view.my.MyView  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"    
  12.         my:textColor="#FFFFFFFF"    
  13.         my:textSize="22dp"  
  14.         />  
  15. </LinearLayout>  

复分析 阿尔福斯(Ahlfors) 第三版答案 英文 答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案 立即下载

相关推荐

Netapp FAS2750更换磁盘后不同步问题处理

netapp fas2750磁盘更换后不同步问题处理

jetliu05的博客 1256

android 自定义控件

Android自定义View实现很简单 继承View,重写构造函数、onDraw,(onMeasure)等函数。 如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml。在其中定义你的属性。 在使用到自定义View的xml布局文件中需要加入xmlns:前缀="http://schemas.android.com/apk/res/你的自定义View所在的包路径". ...

小丰程序人生 227

告别环境配置噩梦:用Docker Desktop + WSL2在Windows上5分钟搞定vLLM运行环境

本文详细介绍了如何在Windows系统上使用Docker Desktop和WSL2快速搭建vLLM开发环境,5分钟内完成配置。通过容器化技术解决传统环境配置中的依赖冲突、环境污染等问题,提供一键复用的高效方案,特别适合AI开发者快速部署和测试vLLM框架。

weixin_28728495的博客 503

Android中如何优雅地自定义一个View

Android中自定义View的实现比较简单,无非就是继承父类,然后重载方法,即便如此,在实际编码中难免会遇到一些坑,我把自己遇到的一些问题和解决方法总结一下,希望对广大码友们有所帮助。 用xml定义Layout时,Root element 最好使用merge 当我们需要继承一个布局比较复杂的ViewGroup(比较多的是LinearLayout、RelativeLayout)时,通常会用xml

老梁的专栏 1万+

AWR射频仿真基本操作(一)

这里只讲述一个基本的操作: 软件是AWR 14版本

钢丝 2万+

SAP 物料账前期没有运行的解决方法

If you use the actual costing (material ledger), you must have completely closed all period-end closing operations in the actual costing for the penultimate period prior to the period shift. Use the '...

含笑饮砒霜死后更嚣张 3318

android自定义控件用xml赋值,自定义控件用XML

package com.example.mywidget;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.webkit.WebView;public class MyWebView extends WebView {publi...

weixin_39573981的博客 287

手把手教你学PCIE(11.3)--pcie的认证和合规性测试

PCIe 设备的认证和合规性测试是确保设备正常工作和达到预期性能的关键步骤。通过使用 PCI-SIG 提供的测试工具和第三方测试工具,可以全面评估 PCIe 设备的物理层、链路层、事务层和互操作性,确保设备符合行业标准和规范。

MHD0815的博客 1360

自定义view-自定义布局中引入布局xml

其实自定义这种引入布局的view的好处就是将Activity中view的处理交给自定义view简化了Activity的代码处理,具体步骤如下。1.首先创建需要引入布局的xml代码如下<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal

MarsKang的博客 6167

黑猫带你学eMMC协议第14篇:eMMC FFU详解(Field Firmware Update)

1 前言 1.1 声明 本文依据eMMC JEDEC5.1及个人工作经验整理而成,如有错误请留言。 本文结合eMMC JEDEC5.1协议手册查看效果更佳。 1.2 内容提要 1、ffu相关寄存器有哪些? 2、如何设置ffu? 3、什么是ffu?是否能够更新旧版本的固件到emmc? 4、ffu配置流程图。 2 什么是FFU 主机下载新的固件到eMMC,下载成功后eMMC将固件安装到设备。 3 相关寄存器/命令 SUPPORTED_MODES [493] FFU_FEATURES [492] 该

黑猫奥利奥的博客 4660

OSPF规划两大模型:双塔奇兵、犬牙交错

大家好,今天给大家带来的是OSPF规划两大模型:双塔奇兵、犬牙交错,让我们直接开始吧! OSPF规划之双塔奇兵 说明: OSPF 如果某个区域存在两个 ABR,并且在两个 ABR 上都对 area n 内的路由做了聚合操作。 最佳配置: 两台ABR之间的红线应该使用以太网的子接口技术,一个子接口属于area0,另一个属于 area n;或者该链路规划为 area n,同时配置虚连接; ABR 的 loopback0 规划入 area 0 OSPF 规划之犬牙交错 说明: 有时接入层的设备会以乱序的

网络技术联盟站 2424

android自定义控件用xml赋值,Android自定义组件,并把自定义组件和自定义xml布局文件关联...

前言在开发自定义view时,往往需要绑定xml布局文件,那具体怎么做呢?自定义view和xml布局文件关联的思路是:在自定义view的构造函数中,通过LayoutInflater.from(context).inflate(R.layout.test_layout, this)关联xml布局文件。具体代码如下1、代码自定义布局——xml代码(test_layout.xml)android:layo...

weixin_33224795的博客 662

Python全栈入门到实战【进阶篇 2】面向对象之封装:私有属性、私有方法与@property优雅访问

_属性名真正私有方法:双下划线开头(__方法名()),Python自动名字改写,外部无法访问;约定私有方法:单下划线开头(_方法名()),仅为编程约定,外部可访问但不推荐。实战中优先使用“真正私有方法”,隐藏核心辅助逻辑。封装的本质:隐藏对象的核心数据(私有属性)和内部逻辑(私有方法),仅对外提供规范、安全的访问接口,实现数据安全与可维护性;私有属性真正私有:双下划线开头(__属性名),Python名字改写,外部无法直接访问,仅类内部可用;约定私有:单下划线开头(_属性名。

那些看似不起波澜的日复一日,会在某天让你看到坚持的意义 2921

[zz]Android自定义View:初探实例

本文来自:http://www.cnblogs.com/ufocdy/archive/2011/05/17/2048958.html   Android自定义View实现很简单 继承View,重写构造函数、onDraw,(onMeasure)等函数。 如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml。在其中定义你的属性。 在使用到自定义View的xml布...

Java初学者 158

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

深度学习工具包 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")

Android自定义View:初探实例

本文来自:http://www.cnblogs.com/ufocdy/archive/2011/05/17/2048958.html Android自定义View实现很简单 继承View,重写构造函数、onDraw,(onMeasure)等函数。 如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml。在其中定义你的属性。 在使用到自定义View的xml布局文件...

dengbaoleng 131

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

上一篇: C/C++中函数参数传递详解
下一篇: Android 知识图谱
起风啰
博客等级 码龄16年 30粉丝 4原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值