iphone的按钮默认按下都带了光晕效果,其实android也很容易实现。只需要在按下的时候贴图即可。
* Xiaoyuan at -4-25
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
/**
* @author Xiaoyuan
*
*/
public class QImageView extends ImageView {
/** 按下光晕图片 */
private Drawable mPressedDrawable;
private boolean mPressed;
/**
* @param context
* @param attrs
*/
public QImageView(Context context, AttributeSet attrs) {
super(context, attrs);
setFocusable(true);
mPressedDrawable = getResources().getDrawable(R.drawable.ic_image_pressed);
/** 设置边距,让光晕完全覆盖图片 */
setPadding(10, 10, 10, 10);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mPressedDrawable.setBounds(0, 0, getWidth(), getHeight());
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(mPressed){
mPressedDrawable.draw(canvas);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPressed = true;
invalidate();
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mPressed = false;
invalidate();
break;
default:
break;
}
return true;
}
}
如果觉得《android canvas光晕绘制_为ImageView增加iphone式的光晕效果》对你有帮助,请点赞、收藏,并留下你的观点哦!