Android----EdgeEffect类边缘效果的实现
2016-05-22 20:12
519 查看
package sample.edward.com.normalsample.edge; import android.annotation.TargetApi; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.os.Build; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewConfiguration; import android.widget.EdgeEffect; /** * Created by edward.ma on 2016/5/12. */ public class EdgeView extends View { private EdgeEffect mRightEffect; private int mTouchSlop; private int mLastMotionX, mLastMotionY; private boolean mIsShownEdge = false; public EdgeView(Context context) { super(context); init(context); } public EdgeView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) private void init(Context context) { mRightEffect = new EdgeEffect(context); mRightEffect.setColor(Color.RED); mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); } @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getActionMasked(); switch (action) { case MotionEvent.ACTION_DOWN: mLastMotionX = (int) event.getX(); mLastMotionY = (int) event.getY(); mIsShownEdge = false; break; case MotionEvent.ACTION_MOVE: int x = (int) event.getX(); int y = (int) event.getY(); int deltax = x - mLastMotionX; int deltay = y - mLastMotionY; if (!mIsShownEdge) { if (Math.abs(deltax) > mTouchSlop && deltax < 0 && Math.abs(deltax) > Math.abs(deltay)) { mIsShownEdge = true; mLastMotionX = x; mLastMotionY = y; } } if (mIsShownEdge) { if (deltax < 0) { // 这个方法确定每次拉伸边缘需要更新的参数 // 第一个参数:每次拉伸增加的距离(0——1)叠加的 ex:第一次传入0.1, 第二次传入0.2的话,总距离就会叠加0.1+0.2 = 0.3 // 第二个参数:边缘圆的位置,上下拉动时边缘鼓起的最大位置的坐标就是这个参数决定的,默认是0.5 mRightEffect.onPull((float) Math.abs(deltax) / getMeasuredWidth()); //mRightEffect.onPull((float)Math.abs(deltax) / getMeasuredWidth(), 0.2f); //更新后刷新view postInvalidateOnAnimation(); } } break; case MotionEvent.ACTION_UP: mIsShownEdge = false; mRightEffect.onRelease(); postInvalidateOnAnimation(); break; default: break; } return true; } @Override public void draw(Canvas canvas) { super.draw(canvas); // 默认边缘是画在最上面,在画图时需要对画布进行平移和旋转 // 具体可参照listview的上下边缘和viewpager的左右边缘的实现方法。 // 这里实现的是右边缘 if (!mRightEffect.isFinished()) { int count = canvas.save(); canvas.rotate(90); canvas.translate(0, -getMeasuredWidth()); mRightEffect.setSize(getMeasuredHeight(), getMeasuredWidth()); if (mRightEffect.draw(canvas)) { postInvalidateOnAnimation(); } canvas.restoreToCount(count); } else { mRightEffect.finish(); } } }
相关文章推荐
- PorterDuffXfermode实现Android刮刮卡效果
- Android日期时间类,解决其他时间类时间会出现误差的bug
- 【Android】安全退出应用程序
- Android中的线程机制(Handler Looper)(二)
- Android Activity生命周期详讲
- Android中 Matrix类
- Notification的几种用法(API不同)
- 关于Activity与Fragment混用中对于startActivityForResult方法的解析
- android中的线程机制
- 【Android 应用开发】GitHub 优秀的 Android 开源项目
- Android开发之Fragment的生命周期
- 安卓屏幕最大化
- android_socket_switch
- Java Android Bug:duplicate entry:classes.dex
- Android Studio系列教程与技巧--下载安装
- Android 笔记
- android:descendantFocusability用法简析
- Notification通过Intent传递参数getIntent()为null的问题解决
- Android实战第一篇——时钟+闹钟+计时器+秒表
- 页面未随软键盘上升及android隐藏软键盘总结