自定义View是最能体现一个Android开发者水平的技能之一了。
接下来的一些列博客将总结一下Android的自定义相关View知识,包括View的结构,事件体系,工作原理,自定义View的绘制等。
参考资料部分来自于书上以及各种博客。
新建了一个qq群 482543750,欢迎一起学习Android的小伙伴加入。
提供各种Android学习资料,面试资料,Android简历模板。
手指滑动后,自定义View走到了图示位置:
public class DragView extends View{
int lastX;
int lastY;
public DragView(Context context) {
super(context);
}
public DragView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
Log.e("触发onTouchEvent",x+"::::::"+y);
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:{
lastX = x;
lastY = y;
}
break;
case MotionEvent.ACTION_MOVE:{
int offsetX = x - lastX;
int offsetY = y - lastY;
Log.e("触发ACTION_MOVE",offsetX+"::::::"+offsetY);
layout(getLeft() + offsetX, getTop() + offsetY, getRight() + offsetX, getBottom() + offsetY);
Log.d("DragView",getLeft()+"______"+getTop()+"-------"+getBottom()+"-------"+getRight());
}
break;
}
return true;
}
}
ViewConfiguration.get(getContext()).getScaledTouchSlop();
//获取对象
VelocityTracker velocityTracker = VelocityTracker.obtain();
//托管event
velocityTracker.addMovement(event);
//设置时间间隔,结果会表示为每1000毫秒经过多少像素,若设置为100,结果表示为没100毫秒经过多少像素
velocityTracker.computeCurrentVelocity(1000);
//获取X和Y方向上的速度
int xVelicity = (int) velocityTracker.getXVelocity();
int yVelicity = (int) velocityTracker.getYVelocity();
velocityTracker.clear();
velocityTracker.recycle();
//获取对象
GestureDetector gestureDetector = new GestureDetector(this);
//解决长按屏幕后无法拖动的问题
gestureDetector.setIsLongpressEnabled(false);
//托管event
boolean consume = gestureDetector.onTouchEvent(event);
return consume;
/**
* Set the scrolled position of your view. This will cause a call to
* {@link #onScrollChanged(int, int, int, int)} and the view will be
* invalidated.
* @param x the x position to scroll to
* @param y the y position to scroll to
*/
public void scrollTo(int x, int y) {
if (mScrollX != x || mScrollY != y) {
int oldX = mScrollX;
int oldY = mScrollY;
mScrollX = x;
mScrollY = y;
invalidateParentCaches();
onScrollChanged(mScrollX, mScrollY, oldX, oldY);
if (!awakenScrollBars()) {
postInvalidateOnAnimation();
}
}
}
/**
* Move the scrolled position of your view. This will cause a call to
* {@link #onScrollChanged(int, int, int, int)} and the view will be
* invalidated.
* @param x the amount of pixels to scroll by horizontally
* @param y the amount of pixels to scroll by vertically
*/
public void scrollBy(int x, int y) {
scrollTo(mScrollX + x, mScrollY + y);
}
点击按钮,View在1秒钟的时间内向右平移200像素
通过如下代码:
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dragView.animate().translationX(200).setDuration(1000).start();
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) dragView.getLayoutParams();
layoutParams.width+=100;
layoutParams.leftMargin+=100;
dragView.requestLayout();
//或者dragView.setLayoutParams(layoutParams);
}
});