`
zkh43javaeye
  • 浏览: 84490 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

(转)android button 效果设计

阅读更多

原址:http://ssd910.blog.163.com/blog/static/23876797201021610282142/

 

Button 有按下效果

 

[功能]

让Button 有按下效果 更有视觉效果

 

[代码]

1. 先准备2张*.png 一张供默认使用 另一张供按下使用 本例为:

 

Java代码

 

  1. play.png   
  2. play_down.png  

play.pngplay_down.png

 

 

2.  根据各种状态 定制化所显示的 *.png 命名为: myselection.xml

Java代码

 

  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">   
  3.     <item    
  4.         android:state_pressed="false"    
  5.         android:drawable="@drawable/play" />   
  6.     <item    
  7.         android:state_pressed="true"    
  8.         android:drawable="@drawable/play_down" />   
  9.     <item    
  10.         android:drawable="@drawable/play" />   
  11. </selector>   

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false" android:drawable="@drawable/play" /> <item android:state_pressed="true" android:drawable="@drawable/play_down" /> <item android:drawable="@drawable/play" /></selector>

 

 

3. 在 main.xml 布局中 添加Button 元件 并 设置 使用 myselection.xml

Java代码

 

  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >   
  7. <TextView     
  8.     android:layout_width="fill_parent"    
  9.     android:layout_height="wrap_content"    
  10.     android:text="Button Style!"  
  11.     />   
  12. <ImageButton    
  13.             android:id="@+id/playorpause"    
  14.             android:layout_width="wrap_content"  
  15.             android:layout_height="wrap_content"    
  16.             android:src="@xml/myselection"    
  17.             android:background="#00000000" />   
  18. </LinearLayout>  

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Button Style!" /><ImageButton android:id="@+id/playorpause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@xml/myselection" android:background="#00000000" /></LinearLayout>

 

 

 

4. 大家可以自己看看效果 因为不好截图

 

 

其实 除了上面的方法 还有一个方法 为:

1. 在 maun.xml 中添加 ImageButton 且不设置使用的*.png

Java代码

 

  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >   
  7. <ImageButton   
  8.     android:id="@+id/button"     
  9.     android:layout_width="wrap_content"    
  10.     android:layout_height="wrap_content"    
  11.     />   
  12. </LinearLayout>  

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

 

 

2. 在该ImageButton上设置监听器 并根据其状态使用对应的资源 但是必须要设置默认资源

Java代码

 

  1. ImageButton btn = (ImageButton) findViewById(R.id.button);   
  2.            
  3.         //to set its default *.png   
  4.         btn.setBackgroundResource(R.drawable.play);   
  5.         btn.setOnTouchListener(new ImageButton.OnTouchListener(){   
  6.             @Override  
  7.             public boolean onTouch(View arg0, MotionEvent arg1) {   
  8.                 // TODO Auto-generated method stub   
  9.                 if(arg1.getAction() == MotionEvent.ACTION_DOWN){   
  10.                     arg0.setBackgroundResource(R.drawable.play_down);   
  11.                 }   
  12.                 else if(arg1.getAction() == MotionEvent.ACTION_UP){   
  13.                     arg0.setBackgroundResource(R.drawable.play);   
  14.                 }   
  15.                    
  16.                 return false;   
  17.             }   
  18.                
  19.         });  

ImageButton btn = (ImageButton) findViewById(R.id.button); //to set its default *.png btn.setBackgroundResource(R.drawable.play); btn.setOnTouchListener(new ImageButton.OnTouchListener(){ @Override public boolean onTouch(View arg0, MotionEvent arg1) { // TODO Auto-generated method stub if(arg1.getAction() == MotionEvent.ACTION_DOWN){ arg0.setBackgroundResource(R.drawable.play_down); } else if(arg1.getAction() == MotionEvent.ACTION_UP){ arg0.setBackgroundResource(R.drawable.play); } return false; } });

 

 

 

具体哪个方法更好 应该根据自己的场合:

1. 只有一个Button 推荐使用第一个方法

2. 有几个Button 推荐使用第二个 统一定义 然后根据指定的id 来使用目标*.png

分享到:
评论

相关推荐

    Android代码-一个漂亮的 Share Button UI 效果。

    ShareButton A share button with smooth animation Demo License Copyright 2016 kayan1990 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance ...

    Android 超炫的Path Button效果源码.zip项目安卓应用源码下载

    Android 超炫的Path Button效果源码.zip项目安卓应用源码下载Android 超炫的Path Button效果源码.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考

    Android 超炫的Path Button效果源码-IT计算机-毕业设计.zip

    前几年的Android应用源码Demo,主要面向的是学生毕业设计学习。

    Android应用源码之拖动 Button 显示效果-IT计算机-毕业设计.zip

    Android应用源码开发Demo,主要用于毕业设计学习。

    Android Studio 界面布局之xml文件中给输入框editText修改下划线颜色 / 为按钮设置圆角和颜色 / 约束布局预览效果和运行效果不一致 问题的解决

    Android Studio 在xml文件中设置界面布局1、xml文件中给输入框editText修改下划线颜色2、设置button按钮为圆角及修改按钮颜色3、运用约束布局时预览效果和运行效果不一致 写一写今天困扰我很久的问题:如何给...

    Android Button 自带阴影效果另一种解决办法

    在Android 5.0以后的版本中,定义一个button时,系统自动会加一个阴影的效果,有的时候这种效果看起来比较好,有的时候不符合UI的设计要求,这时候就需要手动去掉阴影。 网上很多文章写了解决办法,就是给button加一...

    android开发揭秘PDF

    第3章 Android程序设计基础 3.1 Android程序框架 3.1.1 Android项目目录结构 3.1.2 Android应用解析 3.2 Android的生命周期 3.3 Android程序U设计 3.4 小结 第4章 用户界面开发 4.1 用户界面开发详解 4.1.1 用户...

    android开发资料大全

    Android通过画线实现button效果 Android如何防止apk程序被反编译 Android 之 AIDL 和远程 Service 调用 Android 相对布局技巧 android开发环境之Logcat(日志)教程实例汇总 android网络通信之socket教程实例汇总 ...

    Android控件IconButton扩展版.zip

    ),效果如下图。 基于老外的这个项目,我将它迁入Android Studio,作了扩展以及打包成aar发布到JCenter中央仓库(可以通过声明依赖的方式引用了,不用再导入项目或拷贝代码到你的项目中)。 对其扩展的目的...

    Android UI组件实例集合

    是一个工具包用来帮助设计和开发 Android 用户界面,包含三个单独的工具:Android Asset Studio用户界面原型模具,Android 设计预览,时常需要重复确认程序版面设计状况的 Android App 开发者,应该会爱上这个轻量级...

    《Android应用开发揭秘》附带光盘代码.

     第3章 Android程序设计基础  3.1 Android程序框架  3.1.1 Android项目目录结构  3.1.2 Android应用解析  3.2 Android的生命周期  3.3 Android程序U设计  3.4 小结  第4章 用户界面开发  4.1 用户界面开发...

    Android应用开发揭秘pdf高清版

    不仅详细讲解了Android框架、Android组件、用户界面开发、游戏开发、数据存储、多媒体开发和网络开发等基础知识,而且还深入阐述了传感器、语音识别、桌面组件开发、Android游戏引擎设计、Android应用优化、OpcnGL等...

    基于android平台的移动滑板游戏的设计与实现

    对于第一部分的设计,主要用到android开发中的布局开发,本设计用到的是线性布局Linearlayout,通过对android布局的使用,从而达到理想的效果,这其中还牵扯到按钮控件Button的使用,总的来说是界面布局与控件结合...

    Float Button 圆形进度条效果.zip

    Float Button 圆形进度条效果.zip,圆形进度与fab集成(圆形装载机与浮动动作按钮、物料进度圆和浮动动作按钮集成,fab与进度指示环集成,android,物料设计)。

    《Android应用开发揭秘》源码

     第3章 Android程序设计基础  3.1 Android程序框架  3.1.1 Android项目目录结构  3.1.2 Android应用解析  3.2 Android的生命周期  3.3 Android程序U设计  3.4 小结  第4章 用户界面开发  4.1 用户界面开发...

    Android编程基础之简单Button事件响应综合提示控件Toast应用示例

    Button按钮所触发的事件处理,我们称之为Event Handle,只不过在Android当中,按钮事件是由系统的Button.OnClickListener所控制,熟悉Java程序设计的读者对OnXxxListener应该不陌生.以下的Demo,我们将实现当点击Button时...

    Android代码-AndroidUIAdapter

    只需一个步骤即可使用,也可根据自己项目设计图画布大小和像素尺寸实时精准适配,适配所有市面上各种大小不一的安卓手机屏幕,特点是可以直接在Android studio的XML编辑器中直接预览不同手机屏幕的适配效果,提高...

Global site tag (gtag.js) - Google Analytics