转载学习自:菜鸟教程http://www.runoob.com
ImageView有两个可以设置图片的属性,分别是:src和background
常识:
①background通常指的都是背景,而src指的是内容!!
②当使用src填入图片时,是按照图片大小直接填充,并不会进行拉伸
而使用background填入图片,则是会根据ImageView给定的宽度来进行拉伸
1 package com.example.administrator.testt; 2 3 import android.content.Context; 4 import android.graphics.Canvas; 5 import android.graphics.Paint; 6 import android.os.SystemClock; 7 import android.util.AttributeSet; 8 import android.view.MotionEvent; 9 import android.view.ViewConfiguration; 10 import android.widget.ImageButton; 11 12 public class MyButton extends android.support.v7.widget.AppCompatImageButton { 13 private static final int INVALIDATE_DURATION = 15; //每次刷新的时间间隔 14 private static int DIFFUSE_GAP = 10; //扩散半径增量 15 private static int TAP_TIMEOUT; //判断点击和长按的时间 16 17 private int viewWIdth,viewHeight; //控件宽高 18 private int pointX,pointY; //空间原点坐标(左上角) 19 private int maxRadio; //扩散的最大半径 20 private int shaderRadio; //扩散的半径 21 22 private Paint bottomPaint, colorPaint; //画笔:背景和水波纹 23 private boolean isPushBUtton; //记录是否被按下 24 25 private int eventX, eventY; //触摸位置的X,Y坐标 26 private long downTime = 0; //按下的时间 27 28 29 public MyButton(Context context,AttributeSet attrs) { 30 super(context,attrs); 31 initPaint(); 32 TAP_TIMEOUT = ViewConfiguration.getLongPressTimeout(); //判断多久之后判定为长按 33 34 } 35 36 37 private void initPaint(){ 38 colorPaint = new Paint(); 39 bottomPaint = new Paint(); 40 colorPaint.setColor(getResources().getColor(R.color.reveal_color,null)); 41 //getResources().getColor(int id)已过时,现已被getColor(int id,Theme theme)代替,theme可用null 42 bottomPaint.setColor(getResources().getColor(R.color.bottom_color,null)); 43 44 } 45 46 @Override 47 public boolean onTouchEvent(MotionEvent event) { 48 switch (event.getAction()){ 49 case MotionEvent.ACTION_DOWN: 50 if(downTime == 0)downTime = SystemClock.elapsedRealtime(); 51 eventX = (int) event.getX(); 52 eventY = (int) event.getY(); //获取点击的XY坐标 53 //计算最大半径 54 countMaxRadio(); 55 isPushBUtton = true; //确认已被按下 56 postInvalidateDelayed(INVALIDATE_DURATION); 57 break; 58 case MotionEvent.ACTION_UP: 59 case MotionEvent.ACTION_CANCEL: 60 if (SystemClock.elapsedRealtime() - downTime < TAP_TIMEOUT) { 61 DIFFUSE_GAP = 30; //扩散半径增量 62 postInvalidate(); //刷新界面 63 } else { 64 clearData(); 65 } 66 break; 67 } 68 69 return super.onTouchEvent(event); 70 } 71 72 @Override 73 protected void dispatchDraw(Canvas canvas) { 74 super.dispatchDraw(canvas); 75 if(!isPushBUtton) return; //如果按钮没有被按下则返回 76 //绘制按下后的整个背景 77 canvas.drawRect(pointX, pointY, pointX + viewWIdth, pointY + viewHeight, bottomPaint); 78 canvas.save(); 79 //绘制扩散圆形背景 80 canvas.drawCircle(eventX,eventY,shaderRadio,colorPaint); 81 canvas.restore(); 82 //直到半径等于最大半径 83 if(shaderRadio < maxRadio){ 84 postInvalidateDelayed(INVALIDATE_DURATION, 85 pointX, pointY, pointX + viewWIdth, pointY + viewHeight); 86 shaderRadio += DIFFUSE_GAP; 87 }else{ 88 clearData(); 89 } 90 } 91 92 private void countMaxRadio(){ 93 if (viewWIdth > viewHeight) { 94 if (eventX < viewWIdth / 2) { 95 maxRadio = viewWIdth - eventX; 96 } else { 97 maxRadio = viewWIdth / 2 + eventX; 98 } 99 } else { 100 if (eventY < viewHeight / 2) { 101 maxRadio = viewHeight - eventY; 102 } else { 103 maxRadio = viewHeight / 2 + eventY; 104 } 105 } 106 107 } 108 109 private void clearData(){ 110 downTime = 0; 111 DIFFUSE_GAP = 10; 112 isPushBUtton = false; 113 shaderRadio = 0; //扩散的半径 114 postInvalidate(); //刷新界面 115 116 } 117 118 @Override 119 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 120 super.onSizeChanged(w, h, oldw, oldh); 121 this.viewWIdth = w; 122 this.viewHeight = h; 123 } 124 }
ImageButton和Button的区别:
A.ImageButton用图像来表现按钮;
设置方式:android:src="img_id"
android:src="@drawable/ic_launcher_background"
B.ImageButton控件的Click事件是配合System.Web.UI.ImageClickEventHandler委托工作的,ImageButton可获取所点击的X,Y坐标,详情见例子.