Android写动画效果不是一般的麻烦,网上找了好久,终于解决了动画的问题,总结记录以共勉。
仅以水平方向移动效果做说明,垂直方向类似。
public void slideview(final float p1, final float p2) { TranslateAnimation animation = new TranslateAnimation(p1, p2, 0, 0); animation.setInterpolator(new OvershootInterpolator()); animation.setDuration(durationMillis); animation.setStartOffset(delayMillis); animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { int left = view.getLeft()+(int)(p2-p1); int top = view.getTop(); int width = view.getWidth(); int height = view.getHeight(); view.clearAnimation(); view.layout(left, top, left+width, top+height); } }); view.startAnimation(animation); }
上述内容详见:http://www.cnblogs.com/eoiioe/archive/2012/08/29/2662546.html
以下是在项目中的应用实况:
public class WatchTvFragment extends Fragment { private static View view; static String TAG = "WatchTvFragment"; TvChannelListFragment channelFragment; TvProgramListFragment programFragment; TvCommentListFragment commentFragment; FragmentManager fragmentMgr; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (view != null) { ViewGroup parent = (ViewGroup) view.getParent(); if (parent != null) parent.removeView(view); } try { view = inflater.inflate(R.layout.fragment_watchtv, container, false); } catch (InflateException e) { } fragmentMgr = getFragmentManager(); channelFragment = (TvChannelListFragment) fragmentMgr.findFragmentById(R.id.channel_list_fragment); commentFragment = (TvCommentListFragment) fragmentMgr.findFragmentById(R.id.comment_list_fragment); programFragment = (TvProgramListFragment) fragmentMgr.findFragmentById(R.id.program_list_fragment); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); new AsyncTask<Void, Void, Void>() { @Override protected Void doInVoid... params) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } return null; } protected void onPostExecute(Void result) { programFragment.getView().setOnTouchListener(programTouchListener); } }.execute(); } public void slideview(final View view, final float p1, final float p2) { int durationMillis = 500; int delayMillis = 0; TranslateAnimation animation = new TranslateAnimation(p1, p2, 0, 0); animation.setInterpolator(new OvershootInterpolator()); animation.setDuration(durationMillis); animation.setStartOffset(delayMillis); animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { int left = view.getLeft() + (int) (p2 - p1); int top = view.getTop(); int width = view.getWidth(); int height = view.getHeight(); view.clearAnimation(); view.layout(left, top, left + width, top + height); } }); view.startAnimation(animation); } View.OnTouchListener programTouchListener = new OnProgramTouchListener(); class OnProgramTouchListener implements View.OnTouchListener { int downX, upX; boolean bAtStartPosition = true; // 在初始位置 @Override public boolean onTouch(View v, MotionEvent event) { View view = programFragment.getView(); int distance = channelFragment.getView().getMeasuredWidth(); if (event.getAction() == MotionEvent.ACTION_DOWN) { downX = (int) event.getX(); // 取得按下时的坐标x return true; } else if (event.getAction() == MotionEvent.ACTION_UP) { upX = (int) event.getX(); // 取得松开时的坐标x; if (upX - downX > 100 && !bAtStartPosition) { // 往右滑超过一定距离并且不在初始位置 bAtStartPosition = true; slideview(view, 0, distance); } else if (upX - downX < -100 && bAtStartPosition) { // 往左滑超过一定距离并且在初始位置 bAtStartPosition = false; slideview(view, 0, -distance); } return true; } // ACTION_UP // ACTION_MOVE状态不做任何处理。 return false; } } }