一. Imageview的点击效果——图片稍微变暗突出点击效果

public class ClickImageView extends AppCompatImageView {
public ClickImageView(Context context) {
super(context);
}
public ClickImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ClickImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
this.setColorFilter(0x99000000);
return true;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
this.setColorFilter(null);
break;
}
return super.onTouchEvent(event);
}
}

二. Button、TextView的点击效果

  1. 仅仅突出点击效果(点击之后不需要特定颜色)——用style(不用shapeselector),当需要波纹效果可设置foreground
    <TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="点击效果"
android:textColor="#DDFFFFFF"
android:foreground="?android:attr/selectableItemBackground"//波纹效果
android:background="@drawable/shape_5dp_blue"
style="@style/Widget.AppCompat.Button"/> //设置弧度、颜色(shape_5dp_blue) <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp"/>
<solid android:color="@android:color/holo_blue_dark"/>
</shape>
 
TextView的click.gif
  1. 设置点击和非点击两种状态特定颜色——用shapeselector,当需要波纹效果可设置foreground,当也需要底部点击阴影效果可设置style
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="点击效果"
android:textColor="#DDFFFFFF"
android:foreground="?android:attr/selectableItemBackground"//当需要波纹效果
android:background="@drawable/selector_5dp_blue" ![点击效果.gif](http://upload-images.jianshu.io/upload_images/3067748-e6e6601170e3b21a.gif?imageMogr2/auto-orient/strip)
style="@style/Widget.AppCompat.Button"
/> (selector_5dp_blue)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="@android:color/holo_red_dark"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item android:state_pressed="false">
<shape>
<solid android:color="@android:color/holo_blue_dark"/>
<corners android:radius="5dp"/>
</shape>
</item>
</selector>
 
点击效果.gif
  1. 当有的要求更高,需要设置波纹效果的颜色——用ripple(只能在21或以上使用,所以新建drawable-v21,都是命名selector_5dp_blue)
(selector_5dp_blue)
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@android:color/holo_red_dark">
<item>
<selector>
<item android:state_pressed="true">
<shape>
<solid android:color="@android:color/holo_red_dark"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item android:state_pressed="false">
<shape>
<solid android:color="@android:color/holo_blue_dark"/>
<corners android:radius="5dp"/>
</shape>
</item>
</selector>
</item>
</ripple>
 
点击效果.gif

作者:我想成为创业者
链接:https://www.jianshu.com/p/68fac9baf077
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

Android设置常见控件点击效果的更多相关文章

  1. Android 中常见控件的介绍和使用

    1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...

  2. 【ALearning】第三章 Android基本常见控件

    本章主要介绍主要的寻常较多使用的控件,包含TextView.EditView.ImageView.Button等.本章将介绍相关控件基本属性的使用,为以后章节的进阶学习提供基础.案例中引用的Linea ...

  3. Android -- 常见控件的小效果

    1,EditText控件 ① 修改光标颜色 自定义drawable 创建cursor.xml文件 <?xml version="1.0" encoding="utf ...

  4. Android ListView 子控件点击事件

    android:descendantFocusability beforeDescendants:viewgroup会优先其子类控件而获取到焦点 afterDescendants:viewgroup只 ...

  5. Android的GridView控件点击图片变暗效果

    @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setC ...

  6. Android自己定义控件系列五:自己定义绚丽水波纹效果

    尊重原创!转载请注明出处:http://blog.csdn.net/cyp331203/article/details/41114551 今天我们来利用Android自己定义控件实现一个比較有趣的效果 ...

  7. 【Android】10.0 UI开发——如何编写程序界面、常见控件的使用

    ************************ 转载请注明出处:https://www.cnblogs.com/xiaofu007/p/10331880.html ***************** ...

  8. 注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式

    注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式 这个坑,必须要注意呀, 比如在用ListView的时候,如果在List_ ...

  9. android中RecyclerView控件实现点击事件

    RecyclerView控件实现点击事件跟ListView控件不同,并没有提供类似setOnItemClickListener()这样的注册监听器方法,而是需要自己给子项具体的注册点击事件. 本文的例 ...

随机推荐

  1. MFC框架之线程局部存储

    线程局部存储中用到的API基础:(TLS:Thread Local Storage) 1.在主线程中申请索引 g_index=::TlsAlloc(); 2.在线程函数中使用索引 存值:::TlsSe ...

  2. Net 使用UEditor笔记

    WebForm使用Ueditor获取编辑器的值有两种方法:1.通过前台js 获取 function test() { alert(UE.getEditor('控件Id').getContent()); ...

  3. echarts展示箱型图&正态分布曲线

    效果图: 直接上代码吧: <!DOCTYPE HTML> <html> <head> <style type="text/css"> ...

  4. MyCat安装配置

    Mycat : 数据库分库分表中间件 http://www.mycat.io/ mycat运行需要JVM,所以先安装java环境,JDK1.7以上.数据库采用mysql5.7,或者8.0 下载 下载地 ...

  5. Moon Game (凸四边形个数,数学题)

    Problem 2148 Moon Game Accept: 24    Submit: 61 Time Limit: 1000 mSec    Memory Limit : 32768 KB Pro ...

  6. 设计模式(16)--Iterator(迭代器模式)--行为型

    作者QQ:1095737364    QQ群:123300273     欢迎加入! 1.模式定义: 迭代模式又叫游标(Cursor)模式,是对象的行为模式.迭代模式可以顺序地访问一个聚集中的元素而不 ...

  7. scikit-learn画ROC图

    1.使用sklearn库和matplotlib.pyplot库 import sklearn import matplotlib.pyplot as plt 2.准备绘图函数的传入参数1.预测的概率值 ...

  8. js-ES6学习笔记-数值的扩展

    1.ES6 提供了二进制和八进制数值的新的写法,分别用前缀0b(或0B)和0o(或0O)表示. 2.如果要将0b和0o前缀的字符串数值转为十进制,要使用Number方法. 3.ES6在Number对象 ...

  9. Spring AOP 中@Pointcut的用法

    Spring Aop中@pointCut的用法,格式:execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? nam ...

  10. Windows Win7建立wifi热点,手机共享WIFI上网

    Win7建立wifi热点,手机共享wifi上网 by:授客 QQ:1033553122 1.以管理员身份运行命令提示符:快捷键win+R→输入cmd→回车 2.启用并设定虚拟WiFi网卡:运行命令:n ...