首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用动画使我的背景不断闪现

如何用动画使我的背景不断闪现
EN

Stack Overflow用户
提问于 2013-12-11 17:48:45
回答 3查看 631关注 0票数 1

我的代码如下但是..。当我运行它的时候,它什么也做不了

代码语言:javascript
复制
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建议

代码语言:javascript
复制
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

请给我任何建议。

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

我现在的代码

代码语言:javascript
复制
}
    @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文件

代码语言:javascript
复制
<?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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-12-11 17:58:10

我使用下面的代码来修饰我的应用程序的背景,也许您可以重复使用它:

代码语言:javascript
复制
@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

}
票数 2
EN

Stack Overflow用户

发布于 2013-12-11 17:54:40

必须在线性布局上启动动画:

代码语言:javascript
复制
    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.startAnimation(animation)//<-- start animation don't clear it
票数 0
EN

Stack Overflow用户

发布于 2013-12-11 18:03:28

我曾多次使用它来使视图淡入或消失:

代码语言:javascript
复制
    fadeIn = new AlphaAnimation(0.25f, 1);
    fadeIn.setInterpolator(new AccelerateInterpolator()); // add this
    fadeIn.setDuration(500);
    fadeIn.setFillAfter(true);
    fadeIn.setAnimationListener(new RepeatAnimationListener());

    fadeOut = new AlphaAnimation(1, 0.25f);
    fadeOut.setInterpolator(new DecelerateInterpolator()); // and this
    fadeOut.setDuration(500);
    fadeOut.setFillAfter(true);
    fadeOut.setAnimationListener(new RepeatAnimationListener());

private boolean currently_fadeOut;

private class RepeatAnimationListener implements AnimationListener {
    public void onAnimationEnd(Animation animation) {

        if (currently_fadeOut) {
            view.startAnimation(fadeIn);
            currently_fadeOut = false;
        } else {
            view.startAnimation(fadeOut);
            currently_fadeOut = true;
        }
    }

    public void onAnimationRepeat(Animation animation) {
    }

    public void onAnimationStart(Animation animation) {
    }
}

// automatically repeated because of RepeatAnimationListener 
view.startAnimation(fadeOut); 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20526358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档