2013-12-11 47 views
1
不断闪烁

我的代码以下,但..当我运行它,它不会做任何事情VIPUL后如何让我的背景与动画

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

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> 
+0

谢谢你都为你的答案! –

回答

2

我用下面的代码进行着色我的应用程序的背景下,也许你可以使用多次:

@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 

} 
+0

ANIMATION_TINTBACKGROUND你做了什么,导致我无法解决? - –

+0

它只是一个以毫秒为单位的动画运行 – cYrixmorten

+0

对不起,麻烦了,..哪里有确定的mViewPager? –

0

您必须在LinearLayout中启动动画后:

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

好吧..不工作,崩溃 –

+0

显示你的logcat什么错误? –

+0

所以你能弄明白吗? –

0

我都用这个做一个淡出人们的视野中,并反复发作:

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