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

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

概述由于项目这种类型的图片按钮比较多,所以重写了ImageButton类。复制代码代码如下:packageme.henji.widget;

由于项目这种类型的图片按钮比较多,所以重写了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,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控件实现图片按钮)全部内容,希望文章能够帮你解决android自定义按钮示例(重写imagebutton控件实现图片按钮)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1142009.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存