StateListDrawable的使用

Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解 其实Android开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。上面分享的腾讯、头条、阿里、美团、字节跳动等公司2019-2021年的高频面试题,博主还把这些技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,上面只是以图片的形式给大家展示一部分。【Android思维脑图(技能树)】知识不体系? 阅读详情

官方文档:

Lets you assign a number of graphic images to a single Drawable and swap out the visible item by a string ID value.

It can be defined in an XML file with the <selector> element. Each state Drawable is defined in a nested <item> element. For more information, see the guide toDrawable Resources.

解释:

StateListDrawable类定义了不同状态下,与之对应的资源(更多时候是图片资源)。

StateListDrawable可以在xml文件中定义,通过<selector>标签。其中每个状态的属性(可以是设置drawable,图片资源),可以通过<item>元素来设置。


下面写的例子不是状态与图片的切换,只是简单的在状态改变时,改变字体的颜色属性。

关于StateListDrawable的详细解析,可参考Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解


开始贴出小例子:

在focus.xml文件中定义StateListDrawable,设置的item主要是控制焦点获得/失去时,颜色的变化。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
	<!-- 设置控件属性,是否获得焦点 cause 显示不同颜色 -->
    <item android:state_focused="true" android:color="#f44"/>
    <item android:state_focused="false" android:color="#111"/>
</selector>

在布局文件main.xml中添加了两个EditText控件,将控件中默认字体颜色设置成focus.xml文件中控制的属性颜色。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    
<!-- android:textColor属性使用了focus.xml文件中定义的颜色 -->
    <EditText
        android:id="@+id/edit01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="StateListDrawable"
        android:textColor="@drawable/focus" />

    <EditText
        android:id="@+id/edit02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="StateListDrawable"
        android:textColor="@drawable/focus" />

</LinearLayout>

主Activity中没有添加什么语句,这样运行,就可以看到以下效果图:

上面一个EditText获得焦点时


下面一个EditText获得焦点时



OSGEarth 基于鼠标位置缩放 说明 osgearth 原有漫游器EarthManipulator 不支持此功能,所以需要修改此漫游器代码,其中主要原理就是在缩放的同时也要改变当前漫游器中心焦点的位置,从而保持鼠标指定点的位置缩放是不改变。 核心代码修改部分 EarthManipulator 的头文件zoom方法添加参数view /** * Zoom the camera using delt... 阅读详情

相关推荐

yakit 通过codec 解决RSA加密

靶场就不过多叙述,具体的可以看yakit系列的前几篇文章。yakit 热加载进行 RSA 加密如何显示想要的payloads。

Arkmornings的博客 2686

StateListDrawable使用详解

drawable state系列文章 XML类型的drawable图片的解析处理过程StateListDrawable使用详解详解refreshDrawableList()的执行流程Checkable ViewsAndroid中自定义drawable states===============================================前面介绍了XML类型的drawable图片

欢迎关注微信公众号:DroidMind 1万+

版图学习汇总十一(DRC、LVS注意事项)

esd。

weixin_56071389的博客 2262

Android中View绘制各种状态的背景图片原理深入分析以及StateListDrawable使用

/* Call this to force a view to update its drawable state. This will cause drawableStateChanged to be called on this view. Views that are interested in the new state should call getDrawableState. */ //主要功能是根据当前的状态值去更换对应的背景Drawable对象 public void refre

m0_65511857的博客 597

使用TBtools进行物种间共线性分析

Sequence Toolkit ---Fasta Tools ----ID Prefix (fasta文件)

sunny_dayone的博客 8744

基于 YOLOv26 的陶瓷产品缺陷检测系统:智能视觉质检方案

本文提出了一种基于YOLOv26深度学习模型的陶瓷产品智能缺陷检测系统。该系统针对陶瓷生产中的裂纹、气泡、釉面缺陷等8类常见质量问题,采用YOLOv26目标检测算法实现高精度自动化检测。系统架构包含数据采集、预处理、YOLOv26检测和后处理四个模块,具有多类别分类、实时推理和强鲁棒性等技术优势。文章详细介绍了数据预处理与增强方法,并提供了Python代码实现示例。该方案可显著提升陶瓷产品质量检测效率,替代传统人工检测方式,为陶瓷制造业提供智能化质检解决方案。

一键难忘的博客 788

StateList Drawable的用法以及各属性的作用

StateList Drawable 使用环境,情况。通过使用在同一图像区域使用不同的图片,来表示一个对象的 不同状态。 (比如 button的 pressed,focused,or其他的)<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/androi

初心 922

还在纠结二层交换机和三层交换机的区别?这篇文章帮你讲明白!

你好,这里是网络技术联盟站,我是瑞哥。公众号后台有小伙伴想要写一篇文章具体介绍一下二层交换机和三层交换机,此类的文章,之前写过几篇,今天瑞哥再写一篇,希望对大家有所帮助!

网络技术联盟站 4441

Android 中的 StateListDrawable

前言-关于StateListDrawable Android里面经常会在开发中在res/drawable目录下, 使用shape或者selector编写XML文件, 来定制一些View的背景, 而这些XML最终会转换为StateListDrawable *注: 并非所有的XML都是StateListDrawable* 对于StateListDrawable文档中如此描述: 允许您将多个图形图像分配给单个 Drawable,并通过字符串 ID 值替换可见项。它可以在带有 元素的 XML 文件中定义

ansondroider的博客 5214

Feature ‘alsa‘ was enabled, but the pre-condition ‘config.unix && !config.qnx && libs.alsa‘ failed解决

这个问题主要是Qt多媒体插件中缺少了ALSA音频库,所以我们需要自行先编译ALSA 在执行./configure的时候,配置项中ALSA为no Qt Multimedia: ALSA ................................... no GStreamer 1.0 .......................... no GStreamer 0.10 ......................... no Video for Linux .............

有问题或资源连接有缺失的可以私信公众号新潮看世界获取 2587

嵌入式工程师成长之路(3)——PCB设计

带有目的的学:要知道,PCB板子做出来需要我们给厂家哪些东西,我们设计板子的时候,需要遵循哪些设计规范,防止画好也是块废板子。然后再逐步开始学习画板。

云轩阁 8万+

StateListDrawable结合LayerDrawable使用

在项目中,遇到过,甲方爸爸要求图片中圈圈的颜色跟着系统颜色改变,之前也没有做过类似的,翻找资料,好不容易做出来了。现在记下来,怕自己忘记 radio_button_selected.xml中 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/a...

nidan1027的博客 481

【第三方互联】12、支付宝(Alipay)授权第三方登录

我们创建了支付宝开放平台的网页&移动应用,审核通过后,我们需要拿到appid,支付宝公钥,私钥进项开发 一、添加应用信息至项目环境中 我们保存了 appid、应用私钥、支付宝公钥、支付宝回调地址等信息,这里的公钥为支付宝开放平台为我们生成的“支付宝公钥” 二、引入 Maven 依赖 <!-- 支付宝SDK --> <dependency> <g.........

JACKAL 21万+

记录使用StateListDrawable相关

StateListDrawable1、配合ImageView使用2、使用中遇到的问题2.1 跟踪StateListDrawable的setState()源码2.2 跟踪ImageView的setSelected()源码2.3 问题1的源码 1、配合ImageView使用 比如向StateListDrawable对象中添加选中状态,以及选中状态所显示的Drawable //定义常规和选中两种状态 int[] normal = StateSet.WILD_CARD; in

weixin_42589500的博客 680

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

使用StateListDrawable 实现状态效果

StateListDrawable 定义 Lets you assign a number of graphic images to a single Drawable and swap out the visible item by a string ID value. 可以设定多个图片到一个图片资源内,并且根据配置的R.drawble.id值转换出当前需可见的图片 实际理解是状态资源,根据当前绑定对象的状态匹配当前状态下需要显示的images 使用 defined in xml 以<selec

m.t.u 522

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

StateListDrawable资源和ShapeDrawable资源的使用

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666) StateListDrawable用于组织多个Drawable对象,StateListDrawable对象所显示的Drawable对象会随目标组件状态的改变而自动切换。StateListDrawable对象的XML...

孤云博客 1998

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

StateListDrawable资源的使用

StateListDrawable用于组织多个Drawable对象,当使用StateListDrawable作为目标组件的 背景和前景图片时,StateListDrawable对象所显示的Drawable对象会随着目标组件状态的改变而改变, 说白了就是给某个组件添加颜色选择器, 比如一个按钮 按下和离开状态会有不同颜色 StateListDrawable支持的状态如下 举一个例子...

190

android 代码编写selector--StateListDrawable使用

在项目开发中肯定会遇到各种点击效果,一般都是创建一个xml文件然后放在drawable文件夹下面,最后控件设置background引用。

mackkill的专栏 6886

Android状态列表StateListDrawable的使用

时常我们需要为我们的控件在用户不同操作时显示不同的样式,比如选中和取消的样式,按钮点击和松开的样式,Android为我们提供了状态列表的方法显示不同的样式满足我们的需求。在drawable中我们定义如下文件: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item andr...

水月洞天 1134

初学Android 使用Drawable资源之使用StateListDrawable资源 十三

初学Android 使用Drawable资源之使用StateListDrawable资源 十三

hffyyff的博客 667

Android StateListDrawable 和 ShapeDrawable 使用

Android StateListDrawable 和 ShapeDrawable 使用方法

u012514113的专栏 1644

android 关于Button在StateListDrawable 中使用AnimationDrawable的问题

最近在实现一个button的效果时,UI要求未点击时,button有相应的动画,按住时有相应的效果,于是自然而然的想到了使用StateListDrawable ,以及帧动画。遇到了一个异常:android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.AnimationDrawable。

zhangshao45612的专栏 765

TransitionDrawable&StateListDrawable的使用

这个SDK里面的一段代码: 比较适合来做一个简单的动画(比如文字的渐变放大效果等) Resources res = getResources(); TransitionDrawable transition = (TransitionDrawable) res .getDrawable(R.drawable.expand_collapse); ImageView im

gundumw100的专栏 251
上一篇: XmlResourceParser解析XML文件
下一篇: ubuntu 开机黑屏 (设置android编译环境时导致该问题)
cbjust
博客等级 码龄15年 4粉丝 20原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值