Java 使用处理程序创建频闪、闪烁背景
我正在尝试在我的应用程序的背景中制作一个频闪效果。我读到处理程序会工作得最好,但我不知道如何应用它们。我看到 我需要做什么来解决这个问题?有没有一种不用处理器就能做到这一点的方法Java 使用处理程序创建频闪、闪烁背景,java,android,android-layout,Java,Android,Android Layout,我正在尝试在我的应用程序的背景中制作一个频闪效果。我读到处理程序会工作得最好,但我不知道如何应用它们。我看到 我需要做什么来解决这个问题?有没有一种不用处理器就能做到这一点的方法 package com.example.converter; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.graphics.drawable.ColorDrawable
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 view motion事件一样。以下是我测试自己并完美运行的代码:
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;
}
});
运行代码时会发生什么?它会引发异常还是不起作用?onTouch命令和fondo标志为ErrorsO它仍在运行,但不会选通?我尝试了你的代码,但为了激活动画,我使用了相对布局的
onTouch()
事件,它工作得很好。那么你的问题是什么呢?是的,它运行但不会选通,我如何将ontouch添加到布局中?因此我尝试了你的代码,但它似乎不起作用,eclipse告诉我删除@override并将布尔值更改为void,findViewById字段无法识别我的类。当我更改这些内容时,它会运行,但不会做任何不同的事情。首先,您必须进入项目属性,在其中选择java编译器1.6。第二件事findViewById
发生在您在某个片段中使用代码时。如果是,则使用类似于以下内容的视图。findViewById。wherview
是当前片段中当前已膨胀的布局。我忘了在这里提到的一件事是我在Activity
onCreate()
方法中编写的上述代码。因此,您在上面提到,您将ontouch comand添加到了布局中,我是新来的,你能解释一下吗
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;
}
});