一. 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. winform 窗体中 Time 控件的用法

    作用: 用于背景进程中.通过引发Timer事件,Timer控件可以有规律的隔一段时间执行一次代码.也就是,你可以根据你自己的需要,给Timer控件设置时间,Timer每隔这段时间,就执行一次代码. 属 ...

  2. weex stream 之fetch的get、post获取Json数据

    无论何种平台,网络数据的获取都是十分重要的,最近学习weex,不可避免的要学习weex的数据请求方法了.网址 个人感觉,weex stream相较于其他平台,还算比较简单了,但是由于文档以及官方代码中 ...

  3. Android---TextView实现图文混排一

    TextView使用ImageSpan显示图片 SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder() ...

  4. 畅通工程再续(hdu1875) 并查集

    畅通工程再续 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  5. nginx+tomcat抵御慢速连接攻击

    一.安装nginx apt-get install nginx 安装中途可能会要求填写许可,输入‘y’就好了 如果安装提示“E: Unable to locate package nginx”,那么输 ...

  6. Codeforces731C(SummerTrainingDay06-M 并查集)

    C. Socks time limit per test:2 seconds memory limit per test:256 megabytes input:standard input outp ...

  7. POJ1222(SummerTrainingDay01-E)

    EXTENDED LIGHTS OUT Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11078   Accepted: 7 ...

  8. 本地服务器搭建服务:ftp

    开启FTP 服务针对局域网上需要管理的一些文件共享还是有一些帮助的,感兴趣的小伙伴可以尝试下: 1.开启internt 中ftp协议服务 完成即可 -> 可以访问了. tip: UTF-8 选f ...

  9. 查看linux 内存

    1.vmstat vmstat命令显示实时的和平均的统计,覆盖CPU.内存.I/O等内容.例如内存情况,不仅显示物理内存,也统计虚拟内存. $ vmstat -s 2.top top命令提供了实时的运 ...

  10. hibernate动态表名映射

    引自:http://blog.csdn.net/xvshu/article/details/39187779 最近的一个项目有一个需求,有N个考核单位,要对每个考核单位生成一张考核情况表,这样做的目的 ...