How to create this animation?
I want to do this animat开发者_JS百科ion, when switching between 2 activities:
--
--
any help?
those 2 methods are realy usefull to solve the issue quikly:
Animation slide_out_left = AnimationUtils.makeOutAnimation(getActivity(),false);
Animation slide_in_right = AnimationUtils.makeInAnimation(getActivity(), true);
Animation slide_out_right = AnimationUtils.makeOutAnimation(getActivity(),true);
Animation slide_in_left = AnimationUtils.makeInAnimation(getActivity(), false);
here is the doc of the methods, as you'll see the second parameter is a flag to choose the side.
The result is similar to what Rodrigo wrote before but it take 2 lines of code and dosen't require the definition of custom animation.
Here the answer:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewFlipper flipper = findViewById(R.id.flipper);
flipper.setOnTouchListener(this);
}
private void myOnTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
mDragged = true;
break;
case MotionEvent.ACTION_DOWN:
mDragged = false;
mXbefore = event.getX();
break;
case MotionEvent.ACTION_UP:
if(mDragged){
vf = (ViewFlipper) v;
mXafter = event.getX();
if(mXafter > mXbefore){
vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.right_to_left_in));
vf.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.right_to_left_out));
vf.showNext();
}else{
vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.left_to_right_in));
vf.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_to_right_out));
vf.showPrevious();
}
}
mDragged = false;
break;
default:
break;
}
}
main.xml:
<ViewFLipper id="@+id/flipper">
<YourLayout />
<YourLayout />
</ViewFlipper>
right_to_left_in.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%p"
android:toXDelta="0%p"
android:duration="500"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500" />
</set>
right_to_left_out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%p"
android:toXDelta="200%p"
android:duration="500"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500" />
</set>
left_to_right_in.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="500"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500" />
</set>
left_to_right_out_xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%p"
android:toXDelta="-100%p"
android:duration="500"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500" />
</set>
精彩评论