0

我正在尝试在我的应用程序的背景中制作频闪效果。我读到处理程序会工作得最好,但我只是不确定如何应用它们。我看到了这个帖子

 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 视图视图运动事件一样。

4

1 回答 1

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;
        }
    });
于 2012-12-14T17:51:53.110 回答