问题
It's my first app, I try to make translate right to left or left to right.
Here is code
Res > anim > translate_left
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
Android: interpolator="@Android: anim/accelerate_decelerate_interpolator"
>
<translate
android:fromXDelta="100%p"
android:toXDelta="0%p"
android:duration="500"
android:repeatCount="0"
android:fillAfter="true"
/>
</LinearLayout>
Res > anim > translate_right
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
>
<translate
android:fromXDelta="0%p"
android:toXDelta="100%p"
android:duration="500"
android:repeatCount="0"
android:fillAfter="false"
/>
</LinearLayout>
And
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
public class MatchingActivity extends Activity {
boolean isPageOpen = false;
Animation translateLeftAnim;
Animation translateRightAnim;
LinearLayout slidingPage01;
Button openBtn01;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.matching);
// Sliding Page
slidingPage01 = (LinearLayout)findViewById(R.id.slidingPage01);
translateLeftAnim = AnimationUtils.loadAnimation(this, R.anim.translate_left);
translateRightAnim = AnimationUtils.loadAnimation(this, R.anim.translate_right);
SlidingPageAnimationListener animListener = new SlidingPageAnimationListener();
translateLeftAnim.setAnimationListener(animListener);
translateRightAnim.setAnimationListener(animListener);
// Open Button
openBtn01 = (Button) findViewById(R.id.openBtn01);
openBtn01.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// start animation
if (isPageOpen) {
slidingPage01.startAnimation(translateRightAnim);
} else {
slidingPage01.setVisibility(View.VISIBLE);
slidingPage01.startAnimation(translateLeftAnim);
}
}
});
}
private class SlidingPageAnimationListener implements AnimationListener {
public void onAnimationEnd(Animation animation){
if (isPageOpen) {
slidingPage01.setVisibility(View.INVISIBLE);
openBtn01.setText("Open");
isPageOpen = false;
} else {
openBtn01.setText("Close");
isPageOpen = true;
}
}
public void onAnimationRepeat(Animation animation){}
public void onAnimationStart(Animation animation){}
}
}
After launch this code some error comes out!
Error is
03-02 12:19:31.828: E/AndroidRuntime(1981): FATAL EXCEPTION: main
03-02 12:19:31.828: E/AndroidRuntime(1981): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projectcupido/com.example.projectcupido.MatchingActivity}: java.lang.RuntimeException: Unknown animation name: LinearLayout
03-02 12:19:31.828: E/AndroidRuntime(1981): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-02 12:19:31.828: E/AndroidRuntime(1981): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-02 12:19:31.828: E/AndroidRuntime(1981): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-02 12:19:31.828: E/AndroidRuntime(1981): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-02 12:19:31.828: E/AndroidRuntime(1981): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 12:19:31.828: E/AndroidRuntime(1981): at android.os.Looper.loop(Looper.java:137)
03-02 12:19:31.828: E/AndroidRuntime(1981): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-02 12:19:31.828: E/AndroidRuntime(1981): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 12:19:31.828: E/AndroidRuntime(1981): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 12:19:31.828: E/AndroidRuntime(1981): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-02 12:19:31.828: E/AndroidRuntime(1981): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-02 12:19:31.828: E/AndroidRuntime(1981): at dalvik.system.NativeStart.main(Native Method)
03-02 12:19:31.828: E/AndroidRuntime(1981): Caused by: java.lang.RuntimeException: Unknown animation name: LinearLayout
03-02 12:19:31.828: E/AndroidRuntime(1981): at android.view.animation.AnimationUtils.createAnimationFromXml(AnimationUtils.java:124)
03-02 12:19:31.828: E/AndroidRuntime(1981): at android.view.animation.AnimationUtils.createAnimationFromXml(AnimationUtils.java:91)
03-02 12:19:31.828: E/AndroidRuntime(1981): at android.view.animation.AnimationUtils.loadAnimation(AnimationUtils.java:72)
03-02 12:19:31.828: E/AndroidRuntime(1981): at com.example.projectcupido.MatchingActivity.onCreate(MatchingActivity.java:36)
03-02 12:19:31.828: E/AndroidRuntime(1981): at android.app.Activity.performCreate(Activity.java:5104)
03-02 12:19:31.828: E/AndroidRuntime(1981): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-02 12:19:31.828: E/AndroidRuntime(1981): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-02 12:19:31.828: E/AndroidRuntime(1981): ... 11 more
回答1:
I also got stuck in similar kind of situation in which i have to slide layout in all directions but i have done it below is my code may be it help you.
Animation Left slide
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="100%"
android:toXDelta="0%" >
</translate>
Animation Right slide
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="-100%"
android:toXDelta="0%" >
</translate>
RightSwipe = AnimationUtils.loadAnimation(Screen.this, R.anim.right_swipe);
ScreenAnimation.startAnimation(RightSwipe);
来源:https://stackoverflow.com/questions/15174630/i-try-to-make-linear-layout-translate-using-animation-left-right-like-this