2012-12-14 76 views
0

我想在我的应用程序的背景中使频闪效果。我读到处理程序会最好的工作,但我不知道如何应用它们。我看到了this post创建闪光灯,使用处理程序闪烁背景

public boolean onTouch(View v, MotionEvent event) { 

    final int DELAY = 100; 

    if(event.getAction() == MotionEvent.ACTION_UP) { 


     RelativeLayout fondo = (RelativeLayout) findViewById(R.id.fondo); 

     ColorDrawable f = new ColorDrawable(0xff00ff00); 
     ColorDrawable f2 = new ColorDrawable(0xffff0000); 
     ColorDrawable f3 = new ColorDrawable(0xff0000ff); 
     ColorDrawable f4 = new ColorDrawable(0xff0000ff); 

     AnimationDrawable a = new AnimationDrawable(); 
     a.addFrame(f, DELAY); 
     a.addFrame(f2, DELAY); 
     a.addFrame(f3, DELAY); 
     a.addFrame(f4, DELAY); 
     a.setOneShot(false); 

     fondo.setBackgroundDrawable(a); // This method is deprecated in API 16 
     // fondo.setBackground(a); // Use this method if you're using API 16 
     a.start(); 
    } 
    return true; 
} 

我需要做些什么来解决这个问题?有没有办法做到这一点,没有处理程序?

package com.example.converter; 

import android.app.Activity; 
import android.graphics.drawable.AnimationDrawable; 
import android.graphics.drawable.ColorDrawable; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.RelativeLayout; 

public class Savannah extends Activity { 

    RelativeLayout r = (RelativeLayout) findViewById(R.layout.activity_savannah); 
    r.setOnTouchListener=(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View view, MotionEvent event) { 

      final int DELAY = 100; 

      if (event.getAction() == MotionEvent.ACTION_UP) { 

       RelativeLayout fondo = (RelativeLayout) findViewById(R.layout.activity_savannah); 

       ColorDrawable f = new ColorDrawable(0xff00ff00); 
       ColorDrawable f2 = new ColorDrawable(0xffff0000); 
       ColorDrawable f3 = new ColorDrawable(0xff0000ff); 
       ColorDrawable f4 = new ColorDrawable(0xff0000ff); 

       AnimationDrawable a = new AnimationDrawable(); 
       a.addFrame(f, DELAY); 
       a.addFrame(f2, DELAY); 
       a.addFrame(f3, DELAY); 
       a.addFrame(f4, DELAY); 
       a.setOneShot(false); 

       fondo.setBackgroundDrawable(a); // This method is deprecated 
               // in API 
               // 16 
       // fondo.setBackground(a); // Use this method if you're 
       // using API 16 
       a.start(); 
      } 
      return true; 
     } 
    }); 
} 

这就是我,但seontouch监听器是萎靡不振的错误,是ontouch视图视图运动事件。

+0

我想你的代码,但激活我使用'onTouch()'相对布局的事件动画,它完美地工作。那么你的问题是什么? –

+0

当你运行代码时会发生什么?它会抛出异常还是只是不起作用? – dannyRods

+0

onTouch命令和fondo标记为错误 – Guparasti

回答

0

这里是我测试了我的自我,可以正常使用的代码:

RelativeLayout r = (RelativeLayout) findViewById(R.id.relativeLayout1); 
    r.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 

      final int DELAY = 100; 

      if (event.getAction() == MotionEvent.ACTION_UP) { 

       RelativeLayout fondo = (RelativeLayout) findViewById(R.id.relativeLayout1); 

       ColorDrawable f = new ColorDrawable(0xff00ff00); 
       ColorDrawable f2 = new ColorDrawable(0xffff0000); 
       ColorDrawable f3 = new ColorDrawable(0xff0000ff); 
       ColorDrawable f4 = new ColorDrawable(0xff0000ff); 

       AnimationDrawable a = new AnimationDrawable(); 
       a.addFrame(f, DELAY); 
       a.addFrame(f2, DELAY); 
       a.addFrame(f3, DELAY); 
       a.addFrame(f4, DELAY); 
       a.setOneShot(false); 

       fondo.setBackgroundDrawable(a); // This method is deprecated 
               // in API 
               // 16 
       // fondo.setBackground(a); // Use this method if you're 
       // using API 16 
       a.start(); 
      } 
      return true; 
     } 
    }); 
+0

所以我试着你的代码,它似乎并没有工作,日食告诉我删除@override并将布尔值更改为void,并且findViewById字段不会识别我的类。 – Guparasti

+0

当我改变这些东西它运行,但没有做任何不同 – Guparasti

+0

首先你必须去的项目属性,你必须选择Java编译器1.6。当你在某个片段中使用你的代码时,第二件事'findViewById'会发生。如果是,那么使用'view.findViewById'这样的东西.wher'view'是当前片段中当前充气的布局。 –