View.setScaleX View.setScaleY
以上两个方法对View.getWidth()和View.getMeasureWidth()没有影响,获得的仍是缩放以前的尺寸。ide
View.setTranslationX
以上方法对View.getLeft()没影响,获得的是移动以前的据屏幕左边的距离,而对View.getX()则有影响,获得的是移动后的据屏幕左边的距离。函数
***********还有一个很厉害的类就是ViewDragHelper,可用在自定义的View上**************布局
1.建立这个对象动画
ViewDragHelper viewDragHelper=ViewDragHelper.create(ViewGroup forParent ,CallBack cb)
forParent :把此布局做为父布局,通常能够用自定view的this来传,如今本身 理解的不是很清楚this
源码以下:就是能够得到context,以及不能为空spa
public static ViewDragHelper create(ViewGroup forParent, Callback cb) { return new ViewDragHelper(forParent.getContext(), forParent, cb); }
private ViewDragHelper(Context context, ViewGroup forParent, Callback cb) { if (forParent == null) { throw new IllegalArgumentException("Parent view may not be null"); } if (cb == null) { throw new IllegalArgumentException("Callback may not be null"); } mParentView = forParent; mCallback = cb; final ViewConfiguration vc = ViewConfiguration.get(context); final float density = context.getResources().getDisplayMetrics().density; mEdgeSize = (int) (EDGE_SIZE * density + 0.5f); mTouchSlop = vc.getScaledTouchSlop(); mMaxVelocity = vc.getScaledMaximumFlingVelocity(); mMinVelocity = vc.getScaledMinimumFlingVelocity(); mScroller = ScrollerCompat.create(context, sInterpolator); }
2.重写view的onTouchEvent方法获得触摸的对象对象
@Override public boolean onTouchEvent(MotionEvent event) { viewDragHelper.processTouchEvent(event)--获得触摸对象,以此来解析触摸动做 return true; }
3.最好重写view的拦截事件接口
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { return viewDragHelper.shouldInterceptTouchEvent(ev)---让这个对象来决定拦截与否 }
4.回调接口ViewDragHelper.Callback要重写的一些重要方法事件
@Override public boolean tryCaptureView(View child, int pointerId) ---参数是触摸过的最上面的子view,以及它的id,若是返回true则可 在后面重写的方法中处理这个触摸动做。返回false则不会理会这个 触摸动做
@Override public int clampViewPositionHorizontal(View child, int left, int dx) ---在水平方向上的子view根据触摸动做来移动 ---child:被触摸的最上面的子view ---left :子view最左边距离正Y轴的距离,left=child.getLeft()+dx ---dx :是触摸移动的距离 ---该返回值决定子view的位置,若是返回值为定值则该view始终最左边始终在改返回值的位置, 言外之意就是这个view无论你怎么触摸移动它始终在一个位置,不会根据触摸来移动 @Override //和水平方向上重写的方法相似 public int clampViewPositionVertical(View child, int top, int dy) ---在垂直方向上的子view根据触摸动做来移动
@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy)
---在父布局中的子布局只要经过clampViewPositionVertical和
clampViewPositionHorizontal以及viewDrag.smoothSlideViewTo等相关方法
改变了位置则会调用这个方法,当dy,dx等为0时也会调用。
---left:子view的左边离正Y轴的距离
---top :子view的上边离正X轴的距离
---dx :子view在X轴上的移动距离
---dy :子view在Y轴上的移动距离
@Override public void onViewReleased(View releasedChild, float xvel, float yvel) ---此方法是触摸子view移动时候释放时候会被调用 ---releasedChild:触摸子view移动时候释放的那个子view ---xvel :没有释放时候触摸滑动的平均移动的在X轴上的速度 ---yvel :没有释放时候触摸滑动的平均移动的在Y轴上的速度
5.ViewDragHelper的可能会用的方法ci
。。。。。
*******************************总结TranslateAnimation使用******************************************
1.建立构造函数
//建立 A.TranslateAnimation animation = new TranslateAnimation( Animation.RELATIVE_TO_PARENT,-1 ----int fromXType, float fromXValue ,Animation.RELATIVE_TO_PARENT, 1 ----int toXType, float toXValue ,Animation.RELATIVE_TO_PARENT, 0 ----int fromYType, float fromYValue, ,Animation.RELATIVE_TO_PARENT, 0 ----int toYType, float toYValue );
Type: Animation.ABSOLUTE(绝对的屏幕坐标,单位为像素)
Animation.RELATIVE_TO_SELF(相对身的宽度和高度的几倍)
Animation.RELATIVE_TO_PARENT(相对父布局的宽度和高度的几倍)
Valuse: 控件的左边的摆放位置
//建立 B.public TranslateAnimation ( float fromXDelta float toXDelta, float fromYDelta, float toYDelta)
Delta:与控件原始位置的距离差
2.相关设置:
animation.setDuration(12000)--设置动画运行时间 animation.setRepeatMode(TranslateAnimation.RESTART)--设置动画重复的模式(从新开始,倒退回来等) animation.setInterpolator(new LinearInterpolator())--设置动画的加速器(有匀速,加速等) animation.setRepeatCount(TranslateAnimation.INFINITE)--设置动画的重复次数(一直重复等)
animation.setAnimationListener--设置监听器,能够监控到动画的开始结束以及开始重复等动做