android 动画调换上下布局
?
定义动画:
private static final int ANIMATION_DURATION = 550; Animation mDowntoUpHideAnimation, mUptoDownHideAnimation, scaleAnimation ; public void init() { mDowntoUpHideAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f); mDowntoUpHideAnimation.setDuration(ANIMATION_DURATION); mUptoDownHideAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f); mUptoDownHideAnimation.setDuration(ANIMATION_DURATION); scaleAnimation = new ScaleAnimation(1.0f, 1.0f,1.0f,1.0f,Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_SELF); scaleAnimation.setDuration(500); }
?
?
调换布局:(这里使用异步操作)
?
?
public void swapViewUpDown(int upViewId, int downViewId) { View upView = (View) findViewById(upViewId); View downView = (View) findViewById(downViewId); upView.startAnimation(mUptoDownHideAnimation); downView.startAnimation(mDowntoUpHideAnimation); new LoadingTask().execute(upViewId,downViewId); }private class LoadingTask extends AsyncTask<Integer, Void, List<Integer>> { protected List<Integer> doInBackground(Integer... id) { List<Integer> list = new ArrayList<Integer>(); list.add(id[0]); list.add(id[1]); try { Thread.sleep(ANIMATION_DURATION); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } protected void onPostExecute(List<Integer> mList) { super.onPostExecute(mList); View upView = (View) findViewById(mList.get(0)); View downView = (View) findViewById(mList.get(1)); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.BELOW, mList.get(1)); upView.setLayoutParams(params); RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params2.removeRule(RelativeLayout.BELOW); downView.setLayoutParams(params2); downView.startAnimation(scaleAnimation); upView.startAnimation(scaleAnimation); } }
?
?
?