gpt4 book ai didi

java - 如何让我的背景随着动画不断闪烁

转载 作者:行者123 更新时间:2023-11-29 21:22:49 25 4
gpt4 key购买 nike

我的代码如下但是..当我运行它时,它什么也没做

Animation animation = new AlphaAnimation(1, 0); // Change alpha
// from fully
// visible to
// invisible
animation.setDuration(500); // duration - half a second
animation.setInterpolator(new LinearInterpolator()); // do not alter
// animation
// rate
animation.setRepeatCount(Animation.INFINITE); // Repeat animation
// infinitely
animation.setRepeatMode(Animation.REVERSE); // Reverse animation at
// the
// end so the layout will
// fade back in
LinearLayout x = (LinearLayout)findViewById(R.id.warning);
x.clearAnimation();

vipul mittal 建议后的 logcat

12-11 20:08:18.477: E/AndroidRuntime(1571): FATAL EXCEPTION: main
12-11 20:08:18.477: E/AndroidRuntime(1571): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.NTS.standroid/com.NTS.standroid.Settings}: java.lang.NullPointerException
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.os.Looper.loop(Looper.java:130)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-11 20:08:18.477: E/AndroidRuntime(1571): at java.lang.reflect.Method.invokeNative(Native Method)
12-11 20:08:18.477: E/AndroidRuntime(1571): at java.lang.reflect.Method.invoke(Method.java:507)
12-11 20:08:18.477: E/AndroidRuntime(1571): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-11 20:08:18.477: E/AndroidRuntime(1571): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-11 20:08:18.477: E/AndroidRuntime(1571): at dalvik.system.NativeStart.main(Native Method)
12-11 20:08:18.477: E/AndroidRuntime(1571): Caused by: java.lang.NullPointerException
12-11 20:08:18.477: E/AndroidRuntime(1571): at com.NTS.standroid.Settings.onCreate(Settings.java:35)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-11 20:08:18.477: E/AndroidRuntime(1571): ... 11 more

请提出任何建议。

我想让我的背景在我按下按钮后不断闪烁

我当前的代码

}
@SuppressLint("NewApi")
public void tintBackground() {
LinearLayout x = (LinearLayout)findViewById(R.id.warning);
ColorDrawable[] color = { new ColorDrawable(Color.RED),
new ColorDrawable(Color.WHITE) };
TransitionDrawable trans = new TransitionDrawable(color);
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
x.setBackgroundDrawable(trans);
} else {
x.setBackground(trans);
}
trans.startTransition(2000); // do transition over 2 seconds

}

和xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout="@+id/warning">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="@+id/warning1"
android:layout_marginTop="50dp"
android:layout_marginLeft="100dp"
android:background="@drawable/warning"
/>
</LinearLayout>

最佳答案

我使用以下代码为我的应用程序背景着色,也许您可​​以重复使用:

@SuppressLint("NewApi")
public void tintBackground() {
View rootView = findViewById(android.R.id.content);
ColorDrawable[] color = { new ColorDrawable(Color.RED),
new ColorDrawable(Color.WHITE) };
TransitionDrawable trans = new TransitionDrawable(color);
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
rootView.setBackgroundDrawable(trans);
} else {
rootView.setBackground(trans);
}
trans.startTransition(2000); // do transition over 2 seconds

}

关于java - 如何让我的背景随着动画不断闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20526358/

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