gpt4 book ai didi

android - 我尝试使用动画使线性布局平移,像这样向左 <-> 向右。但我有一些错误

转载 作者:行者123 更新时间:2023-11-29 18:02:29 27 4
gpt4 key购买 nike

这是我的第一个应用程序,我尝试进行从右到左或从左到右的翻译。

这是代码

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>

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

最佳答案

我也陷入了类似的情况,我必须在所有方向上滑动布局,但我已经在下面完成了,我的代码可能对你有帮助。

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);

关于android - 我尝试使用动画使线性布局平移,像这样向左 <-> 向右。但我有一些错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15174630/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com