一.背景
最近在温习view自定义这一块的知识,所以把一些学过的东西记录下来
二.相关类
1 Configuration:用来描述设备的配置信息,例如用户的配置信息:locale和scaling等等,比如设备的相关信息:输入模式,屏幕大小,屏幕方向等
Configuration configuration = getResource().getConfiguration();
//判断横竖屏
if(configuration.orientation==Configuration.ORIENTATION_PORTRAIT){
}else{
}
2 ViewConfiguration:提供一些自定义控件用到的标准常量,尺寸大小,滑动距离,敏感度等等
ViewConfiguration viewConfiguration = ViewConfiguration.get(context);
//获取touchSlop,系统所能获取到的
// 获取滑动的最小距离
int touchSlop = viewConfiguration.getScaledTouchSlop();
// 获取Fling速度的最小值和最大值
int minimumVelocity = viewConfiguration.getScaledMinimumFlingVelocity();
int maxmumVelocity = viewConfiguration.getScaledMaximumFlingVelocity();
// 判断是否有物理按键
boolean hasPermanentMenuKey = viewConfiguration.hasPermanentMenuKey();
// 双击间隔时间,在改时间内是双击,否则为单击
int doubleTapTimeout = ViewConfiguration.getDoubleTapTimeout();
// 按住状态转为长按状态需要的时间
int longPressTimeout = ViewConfiguration.getLongPressTimeout();
// 重复按键的时间
int keyRepeatTimeout = ViewConfiguration.getKeyRepeatTimeout();
3 GestureDetector:主要作用是简化Touch操作
public class GestureDetectorActivity extends Activity {
private Context mContext;
private GestureDetector mGestureDetector;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
mGestureDetector = new GestureDetector(mContext,new GestureDetectorImpl());
}
private class GestureDetectorImpl implements GestureDetector.OnGestureListener{
@Override
public boolean onDown(MotionEvent motionEvent) {
// 触摸屏幕按下时候
return false;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
// 滚动
return false;
}
@Override
public void onLongPress(MotionEvent motionEvent) {
}
@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
// 滑动
return false;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
}
4 VelocityTracker:用于跟踪触摸屏事件的速率
public class VelocityTrackerActivity extends Activity {
private VelocityTracker mVelocityTracker;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startVelocityTracker(null);
getVelocity();
stopVelocityTracker();
}
private void startVelocityTracker(MotionEvent motionEvent) {
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(motionEvent);
}
private int getVelocity() {
// 设置VelocityTracker单位,1000表示1秒时间内运动的像素
mVelocityTracker.computeCurrentVelocity(1000);
// 获取在1秒内X方向所滑动像素值
int xVelocity = (int) mVelocityTracker.getXVelocity();
return Math.abs(xVelocity);
}
private void stopVelocityTracker() {
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
}
}
5 Scroller
6 ViewDragHelper(帮助实现view拖拽的工具)