1、自定义MyButton类

public class MyButton extends Button {

//This constructormust be

public MyButton(Context context, AttributeSet attrs) {   super(context, attrs); }

public MyButton(Context context) {   super(context); }

private Paint mPaint = null; private String mText; private int mX, mY;

public void onSetText(String text, int nLeft, int nBottom, int nTextSize,    int nTextColor) {   mPaint = new Paint();   mPaint.setTextSize(nTextSize);   mPaint.setColor(nTextColor);   this.mText = text;   this.mX = nLeft;   this.mY = nBottom; }

private int mDownBmpId, mUpBmpId;

public void onSetBmp(int nDownID, int nUpID) {   this.mDownBmpId = nDownID;   this.mUpBmpId = nUpID; }

@Override public void onDraw(Canvas canvas) {

if (mPaint != null)    canvas.drawText(mText, mX, mY, mPaint);   super.onDraw(canvas); }

@Override public boolean onTouchEvent(MotionEvent event) {   if (event.getAction() == MotionEvent.ACTION_DOWN) {    super.setBackgroundResource(mDownBmpId);   } else if (event.getAction() == MotionEvent.ACTION_UP) {    super.setBackgroundResource(mUpBmpId);   }   return super.onTouchEvent(event); } }

2、 在xml布局文件中添加MyButton控件,像应用普通的Button控件一样。

<com.MyButton    android:id="@+id/test_btn" android:layout_width="120px"    android:layout_height="fill_parent" android:text="Test"    android:background="@drawable/btn_u" />

其中com.MyButton是你定义的MyButton类所在的包名

3、在onCreate()中加载MyButton控件。

MyButton btn = (MyButton)findViewById(R.id.test_btn);   btn.onSetBmp(R.drawable.btn_d, R.drawable.btn_u);

其中btn_d表示为按下btn时背景图片,btn_u为默认状态下btn背景图片

利用selector设置ImageButton不同状态下的背景图片的更多相关文章

  1. JAVA代码设置selector不同状态下的背景

    Selector设置button点击效果(详细)以及常见问题https://www.jianshu.com/p/a0ddba6d7969 Android 代码动态设置TextView的背景.颜色Sel ...

  2. 利用wget 抓取 网站网页 包括css背景图片

    利用wget 抓取 网站网页 包括css背景图片 wget是一款非常优秀的http/ftp下载工具,它功能强大,而且几乎所有的unix系统上都有.不过用它来dump比较现代的网站会有一个问题:不支持c ...

  3. document.execCommand("BackgroundImageCache",false,true)解决ie6下的背景图片缓存问题

    E6下的背景图片每次使用都会重新发送请求(not 本地),连一个hover效果时候同样的背景图片仅仅位置不同而已,ie6都会再次发送请求,这个令人崩溃的事情需要解决掉:对于ie来说,filter:ex ...

  4. android 中ImageButton按下改变背景图片的效果

    最近在做一个app的登陆界面,才发现原来认为很简单的UI效果,其实背后却蕴含的知识很多,积累一个算一个吧. 实现方法有两种:一种是添加代码,一种是配置xml文件. 方法一:代码添加 ImageButt ...

  5. 在rem布局下使用背景图片以及sprite

    现在移动端页面用rem布局已经是一大流派了,成熟的框架如淘宝的flexiable.js,以及我的好友@墨尘写的更轻量级的hotcss.用rem作单位使得元素能够自适应后,还有一块需要关注的,那就是背景 ...

  6. rem布局下使用背景图片和sprite图

    现在移动端页面用rem布局已经是一大流派了,成熟的框架如淘宝的flexiable.js,以及更轻量级的hotcss.用rem作单位使得元素能够自适应后,还有一块需要关注的,那就是背景图片.本文就来聊聊 ...

  7. JAVA代码设置selector不同状态下的背景颜色

    代码实现Shape 代码实现Selector StateListDrawable与GradientDrawable 的运用 在Android开发中,我们时常会用到自定义drawable样式,在draw ...

  8. 设置button不同状态下的背景色,即把这个颜色变成图片设置成,背景图片

    - (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state { [self setBack ...

  9. 转载--使用image-set来优化在retian屏幕下的背景图片

    mage-set对我来说,我也很陌生,于是借助G爸和度娘海量的搜索image-set,才知道Webkit内核"safari6"和“chrome21”支持CSS4的backgroun ...

随机推荐

  1. Leetcode jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  2. Android sdk资源包res里面的drawable(ldpi、mdpi、hdpi、xhdpi、xxhdpi)

    (1)drawable-hdpi里面存放高分辨率的图片,如WVGA (480x800),FWVGA (480x854) (2)drawable-mdpi里面存放中等分辨率的图片,如HVGA (320x ...

  3. Struts2中的ModelDriven机制及其运用

    所谓ModelDriven,意思是直接把实体类当成页面数据的收集对象.比如,有实体类User如下: package cn.com.leadfar.struts2.actions; public cla ...

  4. 20145205 《Java程序设计》第6周学习总结

    教材学习内容总结 -若要将数据从来源中取出,可以使用输入串流:若要将数据写入目的地,可以使用输出串流.在java中,输入串流代表对象为java.in.InputStream的实例:输出串流代表对象为j ...

  5. Apache Flink初接触

    Apache Flink闻名已久,一直没有亲自尝试一把,这两天看了文档,发现在real-time streaming方面,Flink提供了更多高阶的实用函数. 用Apache Flink实现WordC ...

  6. Neil·Zou 语录一

    1  既然选择了远方 Since I’ve chosen to go far    便只顾风雨兼程 I will just walk down the path I chose step by ste ...

  7. HTML5 头部标签定义

    <!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> <html lang="zh-cmn-Hans"&g ...

  8. linux 源码安装详解

    ./configure是用来检测你的安装平台的目标特征的.比如它会检测你是不是有CC或GCC,并不是需要CC或GCC,它是个shell脚本.make是用来编译的,它从Makefile中读取指令,然后编 ...

  9. 【转】c#处理3种json数据的实例

    http://www.jb51.net/article/48027.htm http://json2csharp.chahuo.com/ 网络中数据传输经常是xml或者json,现在做的一个项目之前调 ...

  10. FireDAC 连接SQL Server一些要注意的地方

    TFDConnection: FetchOptions.Mode 设置为fmAll, 返回全部结果, 否则默认只返回前50条, 效果与open以后再执行FetchAll一样 Specifies how ...