android自定义按钮示例(重写imagebutton控件实现图片按钮)
由于项目这种类型的图片按钮比较多,所以重写了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" />
相关文章
利用HorizontalScrollView实现滑动页面时的缩放效果
这篇文章主要为大家详细介绍了利用HorizontalScrollView实现滑动页面时的缩放效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2018-11-11android中Glide实现加载图片保存至本地并加载回调监听
本篇文章主要介绍了android中Glide实现加载图片保存至本地并加载回调监听,具有一定的参考价值,有兴趣的可以了解一下2017-09-09Android TextView渐变颜色和方向及动画效果的设置详解
TextView的在安卓中可以理解为一个文本视图控件,Android的视图控件的基类是View类,可以理解的TextView是View的子类。我们通常在.XML布局文件中会为文本视图控件指定各种属性来设置它的样式,今天我们要讲的当然不是传统常见的那种,将会带有渐变颜色和方向及动画效果2021-11-11Android PullToRefreshLayout下拉刷新控件的终结者
这篇文章主要介绍了Android自定义控件实战中下拉刷新控件终结者PullToRefreshLayout的实现方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2016-03-03
最新评论