一. 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. C#获取电脑型号、系统版本、内存大小、硬盘大小、CPU信息

    摘要 有时需要获取电脑的相关信息.这时可以通过调用windows api的方式,进行获取. 方法 可以通过在powershell中 通过下面的命令进行查询,然后可以通过c#调用获取需要的信息. gwm ...

  2. mybatis-plus排除非表中字段

    使用 transient 修饰 private transient String noColumn; 使用 static 修饰 private static String noColumn; 使用 T ...

  3. Pupu(hdu3003)数论

    Pupu Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  4. Java开发中常用的设计模式(二)---单例模式

    一. 懒汉式单例 //懒汉式单例类.在第一次调用的时候实例化自己 public class Singleton { private Singleton() {} private static Sing ...

  5. 十分钟搞定mac下的phpstorm增加xdebug调试

    一.版本信息 mac 10.10.5 php  5.5.38 phpstorm 10.0.3 xdebug   版本需要与php匹配,匹配地址 :点我匹配  点我查看所有版本 提示:不确定xdebug ...

  6. call,apply,bind

    var student={ name:"马云", say:function(){ console.log(this.name); } } var f=student.say; f. ...

  7. layui点击弹框页面 表单请求

    $("#addSite").click(function () { layer.open({ title: '添加站点', type: 1, area: ['700px', '40 ...

  8. 【读书笔记】iOS-网络-Cookie

    Cookie是HTTP协议在首个版本之后加入的一个重要组件.它向服务器提供了追踪会话状态的能力,同时又无须维持客户端与服务器之间的连接.在浏览器客户端,Cookie值是由服务器通过请求提供的,,然后被 ...

  9. 移动端开发时默认样式reset

    /* http://meyerweb.com/eric/tools/css/reset/ v2.0 | 20110126 License: none (public domain) */ html, ...

  10. 在JavaScript文件中用jQuery方法实现日期时间选择功能

    JavaScript Document $(document).ready(function(e) { 在文本框里面显示当前日期 var date = new Date(); var nian = d ...