例
我已经按照我想要的方式拥有动画,我只想为不同的XML布局动画化.
有一个类LayoutAnimationController,但我真的不知道如何使用它.
有人可以用正确的方向指出我的一个例子或很好的解释.
继承我用来动画的代码.
TranslateAnimation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF,300f,0 ); slide.setAnimationListener(AL); slide.setFillAfter(true); slide.setDuration(1000); parentlayout.startAnimation(slide);
更新
由于很多投票,我决定将一个示例项目放入Git仓库.
查看我的答案链接.
解决方法
我不能用2个不同的XML文件来做,但我怀疑是不可能的.
我确实遇到了一些问题.
第一个动画结束后,按钮不可点击.
这是因为动画显示所有内容都被移动,但它不会更新布局,因此按钮仍然处于动画开始的位置.
所以我不得不计算布局的新位置.
我觉得我读了这个3.0这个不再是一个问题,但是如果我错了就纠正一下
另一个原因是当我的动画终于以我想要的方式工作时,我的底下的视图在动画完成之前已经消失了,因为我调用了view.setVisabilty(View.GONE);.
现在的问题是当我没有调用该方法时,动画只是挂了一秒钟,然后射手到动画的最终位置.
所以我添加一个空的LinearLayout(可以是任何东西),默认属性GONE,当动画开始设置它在可见.当您还原动画时,再次将其设置为不可用.
在这样做之后,动画正在以我想要的方式工作.
如果您使用的是Rel,Linear或任何其他布局.
那么你不能按照Z顺序叠加视图,所以你必须使用SurfaceView.
所以heres main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <SurfaceView android:id="@+id/surfaceView1" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <RelativeLayout android:id="@+id/layout" android:layout_width="220dp" android:layout_height="fill_parent" android:background="#ffee00" android:orientation="vertical" > <LinearLayout android:id="@+id/fake_layouy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:visibility="gone"> </LinearLayout> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </RelativeLayout> <RelativeLayout android:id="@+id/layoutTwo" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff00ee" android:orientation="vertical"> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="#ff0000" android:layout_margin="2dp"> <Button android:id="@+id/button" android:layout_width="50dp" android:layout_height="wrap_content" android:text="slide" /> </LinearLayout> </RelativeLayout> </RelativeLayout>
继承java代码
public class MenuAnimationActivity extends Activity { private Button buttonSwitch; private View subLayout; private View topLayout; private ListView subViewListView; private String listViewDummyContent[]={"Android","iPhone","BlackBerry","AndroidPeople"}; private Display display; private View fakeLayout; private AnimationListener AL; // Values for after the animation private int oldLeft; private int oldTop; private int newleft; private int newTop; private int screenWidth; private int animToPostion; // TODO change the name of the animToPostion for a better explanation. private boolean menuOpen = false; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); buttonSwitch = (Button)findViewById(R.id.button); subLayout = (View) findViewById(R.id.layout); topLayout = (View) findViewById(R.id.layoutTwo); subViewListView=(ListView)findViewById(R.id.listView1); fakeLayout = (View)findViewById(R.id.fake_layouy); subViewListView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listViewDummyContent)); display = getWindowManager().getDefaultDisplay(); screenWidth = display.getWidth(); int calcAnimationPosition = (screenWidth /3); // Value where the onTop Layer has to animate // also the max width of the layout underneath // Set Layout params for subLayout according to calculation animToPostion = screenWidth - calcAnimationPosition; RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(animToPostion,RelativeLayout.LayoutParams.FILL_PARENT); subLayout.setLayoutParams(params); topLayout.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v,MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN) { if (menuOpen == true) { animSlideLeft(); } } return false; } }); buttonSwitch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(menuOpen == false){ animSlideRight(); } else if (menuOpen == true) { animSlideLeft(); } } }); AL = new AnimationListener() { @Override public void onAnimationStart(Animation animation) { buttonSwitch.setClickable(false); topLayout.setEnabled(false); } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { if(menuOpen == true) { Log.d("","Open"); topLayout.layout(oldLeft,oldTop,oldLeft + topLayout.getMeasuredWidth(),oldTop + topLayout.getMeasuredHeight() ); menuOpen = false; buttonSwitch.setClickable(true); topLayout.setEnabled(true); } else if(menuOpen == false) { Log.d("","FALSE"); topLayout.layout(newleft,newTop,newleft + topLayout.getMeasuredWidth(),newTop + topLayout.getMeasuredHeight() ); topLayout.setEnabled(true); menuOpen = true; buttonSwitch.setClickable(true); } } }; } public void animSlideRight(){ fakeLayout.setVisibility(View.VISIBLE); newleft = topLayout.getLeft() + animToPostion; newTop = topLayout.getTop(); TranslateAnimation slideRight = new TranslateAnimation(0,newleft,0); slideRight.setDuration(500); slideRight.setFillEnabled(true); slideRight.setAnimationListener(AL); topLayout.startAnimation(slideRight); } public void animSlideLeft() { fakeLayout.setVisibility(View.GONE); oldLeft = topLayout.getLeft() - animToPostion; oldTop = topLayout.getTop(); TranslateAnimation slideLeft = new TranslateAnimation(newleft,oldLeft,0); slideLeft.setDuration(500); slideLeft.setFillEnabled(true); slideLeft.setAnimationListener(AL); topLayout.startAnimation(slideLeft); } }
我在感动的视图和内容上做了一些额外的编码.
最后的结果
动画前
第一动画之后
在第二个动画回到左边后,它将返回值作为第一个图像.
那些帮助我真正值得信任的帖子,但我找不到任何信息.
编辑
GIT https://bitbucket.org/maikelbollemeijer/sidepanelswitcher
更新:
https://github.com/jfeinstein10/SlidingMenu
此lib与Actionbar Sherlock兼容.
希望这可以帮助