本节引言:
1.StateListDrawable简介:
我们可以设置的属性:
2.实现按钮的按下效果:
好的,先准备三个图片背景,一般我们为了避免按钮拉伸变形都会使用.9.png作为按钮的drawable! 先来看下 运行效果图:
代码实现:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/ic_course_bg_fen"/> <item android:state_enabled="false" android:drawable="@drawable/ic_course_bg_pressed"/> <item android:drawable="@drawable/ic_course_bg_cheng"/> </selector>
布局文件:activity_main.xml
<Button android:id="@+id/btnOne" android:layout_width="match_parent" android:layout_height="64dp" android:background="@drawable/btn_bg1" android:text="按钮"/> <Button android:id="@+id/btnTwo" android:layout_width="match_parent" android:layout_height="64dp" android:text="按钮不可用"/>
MainActivity.java:
public class MainActivity extends Activity { private Button btnOne,btnTwo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnOne = (Button) findViewById(R.id.btnOne); btnTwo = (Button) findViewById(R.id.btnTwo); btnTwo.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(btnTwo.getText().toString().equals("按钮不可用")){ btnOne.setEnabled(false); btnTwo.setText("按钮可用"); }else{ btnOne.setEnabled(true); btnTwo.setText("按钮不可用"); } } }); } }
3.使用颜色值绘制圆角按钮
先看下效果图:
bbuton_danger_rounded.xml:
<?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="@color/bbutton_danger_pressed" /> <stroke android:width="1dp" android:color="@color/bbutton_danger_edge" /> <corners android:radius="@dimen/bbuton_rounded_corner_radius"/> </shape></item> <item android:state_enabled="false"><shape> <solid android:color="@color/bbutton_danger_disabled" /> <stroke android:width="1dp" android:color="@color/bbutton_danger_disabled_edge" /> <corners android:radius="@dimen/bbuton_rounded_corner_radius"/> </shape></item> <item><shape> <solid android:color="@color/bbutton_danger" /> <stroke android:width="1dp" android:color="@color/bbutton_danger_edge" /> <corners android:radius="@dimen/bbuton_rounded_corner_radius"/> </shape></item> </selector>
color.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="bbutton_danger_pressed">#ffd2322d</color> <color name="bbutton_danger_edge">#ffd43f3a</color> <color name="bbutton_danger_disabled">#a5d9534f</color> <color name="bbutton_danger_disabled_edge">#a5d43f3a</color> <color name="bbutton_danger">#ffd9534f</color> <color name="text_font_white">#FFFFFF</color> </resources>
dimens.xml:
<dimen name="bbuton_rounded_corner_radius">5dp</dimen>
4.实现Material Design水波效果的Button
如果你的Android手机是5.0以上的系统,相信对下面这种按钮点击效果并不会陌生:
实现效果图:
快的那个是按下后的效果,慢的是长按后的效果!
实现逻辑:
1.我们继承ImageButton,当然你可以换成Button或者View,这里笔者想把龟放到中间才继承ImageButton
2.首先,创建两个Paint(画笔)对象,一个绘制底部背景颜色,一个绘制波纹扩散的
3.接着计算最大半径,开始半径每隔一段时间递增一次,直到等于最大半径,然后重置状态!
PS:大概的核心,刚学可能对自定义View感到陌生,没事,这里了解下即可,以后我们会讲,当然 你可以自己扣扣,注释还是蛮详细的~
实现代码:
自定义ImageButton:MyButton.java
package demo.com.jay.buttondemo; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.os.SystemClock; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.ViewConfiguration; import android.widget.ImageButton; /** * Created by coder-pig on 2015/7/16 0016. */ public class MyButton extends ImageButton { private static final int INVALIDATE_DURATION = 15; //每次刷新的时间间隔 private static int DIFFUSE_GAP = 10; //扩散半径增量 private static int TAP_TIMEOUT; //判断点击和长按的时间 private int viewWidth, viewHeight; //控件宽高 private int pointX, pointY; //控件原点坐标(左上角) private int maxRadio; //扩散的最大半径 private int shaderRadio; //扩散的半径 private Paint bottomPaint, colorPaint; //画笔:背景和水波纹 private boolean isPushButton; //记录是否按钮被按下 private int eventX, eventY; //触摸位置的X,Y坐标 private long downTime = 0; //按下的时间 public MyButton(Context context, AttributeSet attrs) { super(context, attrs); initPaint(); TAP_TIMEOUT = ViewConfiguration.getLongPressTimeout(); } /* * 初始化画笔 * */ private void initPaint() { colorPaint = new Paint(); bottomPaint = new Paint(); colorPaint.setColor(getResources().getColor(R.color.reveal_color)); bottomPaint.setColor(getResources().getColor(R.color.bottom_color)); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (downTime == 0) downTime = SystemClock.elapsedRealtime(); eventX = (int) event.getX(); eventY = (int) event.getY(); //计算最大半径: countMaxRadio(); isPushButton = true; postInvalidateDelayed(INVALIDATE_DURATION); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: if(SystemClock.elapsedRealtime() - downTime < TAP_TIMEOUT){ DIFFUSE_GAP = 30; postInvalidate(); }else{ clearData(); } break; } return super.onTouchEvent(event); } @Override protected void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); if(!isPushButton) return; //如果按钮没有被按下则返回 //绘制按下后的整个背景 canvas.drawRect(pointX, pointY, pointX + viewWidth, pointY + viewHeight, bottomPaint); canvas.save(); //绘制扩散圆形背景 canvas.clipRect(pointX, pointY, pointX + viewWidth, pointY + viewHeight); canvas.drawCircle(eventX, eventY, shaderRadio, colorPaint); canvas.restore(); //直到半径等于最大半径 if(shaderRadio < maxRadio){ postInvalidateDelayed(INVALIDATE_DURATION, pointX, pointY, pointX + viewWidth, pointY + viewHeight); shaderRadio += DIFFUSE_GAP; }else{ clearData(); } } /* * 计算最大半径的方法 * */ private void countMaxRadio() { if (viewWidth > viewHeight) { if (eventX < viewWidth / 2) { maxRadio = viewWidth - eventX; } else { maxRadio = viewWidth / 2 + eventX; } } else { if (eventY < viewHeight / 2) { maxRadio = viewHeight - eventY; } else { maxRadio = viewHeight / 2 + eventY; } } } /* * 重置数据的方法 * */ private void clearData(){ downTime = 0; DIFFUSE_GAP = 10; isPushButton = false; shaderRadio = 0; postInvalidate(); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); this.viewWidth = w; this.viewHeight = h; } }
<code> <p><b>color.xml:</b></p> <pre> <?xml version="1.0" encoding="utf-8"?> <resources> <color name="reveal_color">#FFFFFF</color> <color name="bottom_color">#3086E4</color> <color name="bottom_bg">#40BAF8</color> </resources>
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <demo.com.jay.buttondemo.MyButton android:id="@+id/myBtn" android:layout_width="match_parent" android:layout_height="64dp" android:src="@mipmap/ic_tur_icon" android:background="@color/bottom_bg" android:scaleType="center"/> </RelativeLayout>
源码下载(AS工程的哦):ButtonDemo.zip
本节小结:
本节给大家介绍了Button在实际开发中的一些用法,可能有些东西我们还没学,这里 知道下即可,后面学到自然会深入讲解,谢谢~