这是我的第一个应用程序,我尝试将翻译从右向左或从左向右。
这是代码
Res>动画> 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&gt;动画&gt; 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>
和
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){}
}
}
启动此代码后会出现一些错误!
错误是
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
答案 0 :(得分:3)
我也陷入了类似的情况,我必须向所有方向滑动布局,但我在下面完成的是我的代码可能对你有帮助。
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);