android自定义按钮示例(重写imagebutton控件实现图片按钮)

时间:2021-06-13 07:55:19 

由于项目这种类型的图片按钮比较多,所以重写了ImageButton类。


package me.henji.widget;

import android.content.Context;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;

/**
 * 自定义图片按钮(ImageButton),按下颜色改变
 * @author Leo
 * @created 2013-3-15
 */
public class CmButton extends ImageButton implements OnTouchListener, OnFocusChangeListener {

 public CmButton(Context context) {
  super(context);
  this.setOnTouchListener(this);
  this.setOnFocusChangeListener(this);
 }

 public CmButton(Context context, AttributeSet attrs) {
  this(context, attrs, android.R.attr.imageButtonStyle);
  this.setOnTouchListener(this);
  this.setOnFocusChangeListener(this);
 }

 public CmButton(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  setFocusable(true);
  this.setOnTouchListener(this);
  this.setOnFocusChangeListener(this);
 }

 @Override
 public void onFocusChange(View v, boolean hasFocus) {
  // 灰色效果
  ColorMatrix cm = new ColorMatrix();
  cm.setSaturation(0);
  if (hasFocus) {
   ((ImageButton) v).getDrawable().setColorFilter(new ColorMatrixColorFilter(cm));
  } else {
   ((ImageButton) v).getDrawable().clearColorFilter();
  }
 }

 @Override
 public boolean onTouch(View v, MotionEvent event) {
  // 灰色效果
  ColorMatrix cm = new ColorMatrix();
  cm.setSaturation(0);
  if (event.getAction() == MotionEvent.ACTION_DOWN) {
   ((ImageButton) v).getDrawable().setColorFilter(new ColorMatrixColorFilter(cm));
  } else if (event.getAction() == MotionEvent.ACTION_UP) {
   ((ImageButton) v).getDrawable().clearColorFilter();
  }
  return false;
 }
}

布局文件


<me.henji.widget.CmButton
    android:id="@+id/btn_login"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#00000000"
    android:src="@drawable/button_login"
    android:text="@string/login_login" />

标签:android,按钮,imagebutton
0
投稿

猜你喜欢

  • Java数组的定义与使用

    2023-08-12 11:44:38
  • IDEA安装后找不到.vmoptions文件的问题及解决

    2023-10-05 22:43:44
  • Java maven详细介绍

    2022-10-12 06:45:31
  • Kafka 日志存储实现过程

    2021-11-01 05:04:00
  • Java事务管理学习之Spring和Hibernate详解

    2023-04-11 00:01:25
  • 在IntelliJ IDEA中为自己设计的类库生成JavaDoc的方法示例

    2023-11-25 09:49:02
  • Android开发实现TextView超链接5种方式源码实例

    2022-12-10 16:50:32
  • C#实现利用Linq操作Xml文件

    2022-01-25 18:00:26
  • spring boot metrics监控指标使用教程

    2022-01-10 08:42:16
  • Android Studio 升级到3.0后输入法中文状态下无法选词的终极解决方案

    2022-11-21 07:34:41
  • Spring Cloud Ribbon客户端详细介绍

    2023-11-27 21:36:22
  • java8 集合之Stack详解及实例

    2023-08-02 16:04:07
  • 横竖屏切换导致页面频繁重启screenLayout解析

    2021-06-14 05:16:22
  • C语言算法打卡回文串验证算法题解

    2022-05-29 22:54:10
  • C#中判断本地系统的网络连接状态的方法

    2023-07-02 15:39:41
  • Java中的StringBuilder性能测试

    2021-09-11 03:38:20
  • spring-boot中使用spring-boot-devtools的实现代码

    2021-10-09 13:31:32
  • Java实现TCP/IP协议的收发数据(服务端)代码实例

    2023-08-11 08:44:51
  • Android studio 添加assets文件夹的方法

    2023-02-20 10:09:37
  • mybatis原理概述入门教程

    2023-10-08 13:10:57
  • asp之家 软件编程 m.aspxhome.com