利用ObjectAnimator控制view的位置
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
View view; | |
ObjectAnimator valueAnimator = | |
ObjectAnimator.ofFloat(view, "translationY", 80); | |
//"translationY"為property name,80為位移量 | |
valueAnimator.setDuration(1000); //動畫完成的時間 | |
valueAnimator.start(); |
應用
使用TouchListener來控制view的移動
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scrollView.setOnTouchListener(new View.OnTouchListener() { | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
switch (event.getAction()){ | |
case MotionEvent.ACTION_DOWN: | |
mFirstY = event.getY();//getY获取的是相对于View的坐标,getRawY获取的是相对于屏幕的坐标 | |
break; | |
case MotionEvent.ACTION_MOVE: | |
mLastY = event.getY(); | |
if(mLastY - mFirstY > mTouchSlop){//手指向下滑动,显示toolbar | |
if(!mShow){ | |
// Log.i("tag", "mLastY_手指下滑="+mLastY); | |
toolbarAnim(0);//显示 | |
mShow = !mShow; | |
} | |
}else if(mFirstY - mLastY > mTouchSlop){//手指向上滑动,隐藏toolbar | |
if(mShow){ | |
// Log.i("tag", "mLastY_手指上滑="+mLastY); | |
toolbarAnim(1);//隐藏 | |
mShow = !mShow; | |
} | |
} | |
break; | |
default:break; | |
} | |
return false; | |
} | |
}); | |
public void toolbarAnim(int flag){ | |
if(mAnimator != null && mAnimator.isRunning()){ | |
mAnimator.cancel(); | |
} | |
if(mAnimatorforll != null && mAnimatorforll.isRunning()){ | |
mAnimatorforll.cancel(); | |
} | |
if(flag == 0){ | |
// Log.i("tag", "手指下滑,》》》》》》》显示"); | |
// mAnimator = ObjectAnimator.ofFloat(toolbar, "translationY", toolbar.getTranslationY(), 0); | |
mAnimatorforll = ObjectAnimator.ofFloat(ll, "translationY", ll.getTranslationY(), 0); | |
params.height = ll.getHeight()-share_button.getHeight(); | |
ll.setLayoutParams(params); | |
}else if(flag == 1){ | |
// Log.i("tag", "手指上滑,》》》》》》》隐藏"); | |
// mAnimator = ObjectAnimator.ofFloat(toolbar, "translationY", toolbar.getTranslationY(),-share_button.getHeight()); | |
mAnimatorforll = ObjectAnimator.ofFloat(ll, "translationY", ll.getTranslationY(),-share_button.getHeight()); | |
params.height = ll.getHeight()+share_button.getHeight(); | |
ll.setLayoutParams(params); | |
} | |
// mAnimator.start();//开始动画 | |
mAnimatorforll.start(); | |
} | |
留言
張貼留言