UI组件之ImageView及其子类(二)ImageButton ,ZoomButton
从ImageButton这个字面意思上来看,它是一个图片按钮,那么我们就可以使用它做一个我们想要的图片按钮了,但是我们在实际使用的过程当中,就会发现该按钮的使用并没有想像中的那么简单,需要再增加一些代码或再配置XML才能实现图片按钮按下的效果
ImageButton 还直接派生了ZoomButton组件,只是Android默认提供了btn_minus,btn_plus两个Drawable资源,只要为ZoomButton的android:src属性分别指着两个android提供的资源,即可实现“放大”,“缩小”按钮,ZoomButton组件仅仅是android:src属性默认使用的是android的资源,添加了一些方法可实现放大缩小。
android还提供了一个ZoomControls组件,该组件继承了是LeanerLayout布局组件,把两个放大缩小的按钮水平线性的组合在一起,并允许分别为两个按钮绑定不同的事件监听器
图片按钮正常状态的效果:
第二个按钮按下是的效果
实现图片按钮按下的效果有两种方式可以实现:一是增加代码,二配置XML。
一、在java中为图片按钮增加触摸监听的函数来实现图片切换
ImageButton btn = (ImageButton)findViewById(R.id.imageButton1); //在java代码中修改按下按钮时不同的状态btn.setOnTouchListener(new View.OnTouchListener(){ @Overridepublic boolean onTouch(View v, MotionEvent event) {if(event.getAction() == MotionEvent.ACTION_DOWN){ //重新设置按下时的背景图片 ((ImageButton)v).setImageDrawable(getResources().getDrawable(R.drawable.red)); }else if(event.getAction() == MotionEvent.ACTION_UP){ //再修改为抬起时的正常图片 ((ImageButton)v).setImageDrawable(getResources().getDrawable(R.drawable.purple)); } // TODO Auto-generated method stubreturn false;} });
代码比较简单,就是当图片按下时,修改按钮的背景图片,当抬起时再修改为正常的图片显示
二、通过给按钮配置XML文件来实现图片按钮的背景切换效果
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><!-- 指定按钮按钮下时的图片 --><item android:state_pressed="true"android:drawable="@drawable/red"/><!-- 指定按钮松开时的图片 --> <item android:state_pressed="false"android:drawable="@drawable/purple"/>
</selector>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/root"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><!-- 普通图片按钮 --><ImageButtonandroid:id="@+id/imageButton1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/blue" />
<!-- 按下时显示不同图片的按钮 ,android:background--><ImageButtonandroid:id="@+id/imageButton2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/button_selector" /><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_margin="10dp"android:orientation="horizontal" ><!-- 分别定义两个zoombutton,分别用了android提供的btn_minus和btn_plus图片,当然自己也可以定义 --><ZoomButtonandroid:id="@+id/zoomButton1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@android:drawable/btn_plus" /><ZoomButtonandroid:id="@+id/zoomButton2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@android:drawable/btn_minus" /></LinearLayout><!-- 定义zoomcontrols组件 --><ZoomControlsandroid:id="@+id/zoomControls1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:orientation="horizontal" /></LinearLayout>
需要特别注意的是:在ImageButton中,如果使用XML配置文件来设置图片的效果的话,就不要再指定它的android:src=""属性值了,否则图片的按下效果就出不来了。
这两种方法各有各的好处,在实际运用过种当种可以根据自己的需要进行选择。
推荐ImageButton用的好的文章:
http://my.oschina.net/amigos/blog/59751
http://blog.csdn.net/ztp800201/article/details/7312687
UI组件之ImageView及其子类(二)ImageButton ,ZoomButton相关推荐
- UI组件之ImageView及其子类(一)ImageView显示图片
ImageView家族的继承关系如图: ImageView继承自View组件,它的主要功能室显示图片,还可以显示Drawable对象. ImageView直接子类是ImageButton,QuickC ...
- UI组件之TextView及其子类(二)RadioButton和CheckBox
单选按钮(RadioButton)和复选框(CheckBox),状态开关按钮(ToggleButton),开关(Switch)都是普通的UI组件,都继承了Button类,因此都可以用Button的各种 ...
- UI组件之AdapterView及其子类(二)GridView网格视图的使用
GridView网格视图属性: android:numColumns="auto_fit" --------列数设置为自动,可以为确定的数值 android:columnWidth ...
- UI组件之ProgressBar及其子类(二)SeekBar拖动条和RatingBar星级评分条的使用
拖动条采用拖动滑块的位置来表示数值 SeekBar的常用xml属性值: 重要的android:thumb制定一个Drawable对象,改变滑块外观 通过滑块来改变图片的透明度: main.xml &l ...
- UI组件之AdapterView及其子类(六)ExpandableListView组件和ExpandableListActivity的使用
ExpandableListView是ListView的子类,他在ListView上进行了扩展,它把列表项分成了几组,每组里包含了多个列表项 ExpandableListView的列表项是由Expan ...
- UI组件之AdapterView及其子类(一)三种Adapter适配器填充ListView
AdapterView的内容一般是包含多项相同格式资源的列表,常用的有5种AdapterView的子类: (1)ListView:简单的列表 (2)Spinner:下拉列表,给用户提供选择 (3)Ga ...
- UI组件之AdapterView及其子类(四)Gallery画廊控件使用
听说 Gallery现在已经不使用了,API使用ViewPaper代替了,以后再学专研ViewPaper吧现在说说Gallery画廊,就是不停显示图片的意思 Gallery是用来水平滚动的显示一系列项 ...
- UI组件之AdapterView及其子类(五)ListView组件和ListActivity
ListView组件是一个显示组件,继承AdapterView基类,前面已经介绍了分别使用ArrayAdapter,SimpleAdapter,扩展BaseAdapter来为LisView提供列表项h ...
- UI组件之AdapterView及其子类(三)Spinner控件详解
Spinner提供了从一个数据集合中快速选择一项值的办法.默认情况下Spinner显示的是当前选择的值,点击Spinner会弹出一个包含所有可选值的dropdown菜单或者一个dialog对话框,从该 ...
最新文章
- 坚持刷题678天的感受!
- R语言绘制核密度图实战(Kernel Density Plot)
- Codeforces Round #180 (Div. 2) A. Snow Footprints 贪心
- 【CyberSecurityLearning 46】PHP 函数
- 决策算法python_GitHub - nxety/MachineLearning_Python: 机器学习算法python实现
- ES11新特性_动态import---JavaScript_ECMAScript_ES6-ES11新特性工作笔记065
- java 编写无状态代码,一种真正实现RMI无状态化的方法续:JVM源码修改步骤
- 加入商品分类信息,考虑用户所处阶段的 图模型 推荐算法 Rws(random walk with stage)...
- 性能计数器驱动_Vulkan 探密:AMD Vulkan 开源驱动源码解析-零
- ip定压终端服务器是什么,IP网络功放 IP网络物联功放 IP网络联机功放 IP网络数字定压功放...
- 【UG】二次开发如何调试
- 利用Python批量把flv文件转换成mp4文件
- 2.Vue3.0 性能提升主要是通过哪几方面体现的?
- reduce()用法
- 神舟战神Z7M-笔记本背光灯怎样打开?
- 分享Linux 机器 CPU 毛刺问题排查经历
- 剑指 Offer 51-60
- 单片机毕业设计 stm32智能路灯设计与实现
- opencv cvRound函数cvClone
- 程序员该该怎么样转型 5G 开发呢?