0

嗨,我的应用程序在传感器上,如果我摇晃它,我找不到任何其他颜色。我在 if else 循环中只提供了两种颜色。但是,如果想添加更多颜色,那么我应该添加什么。解决方案是什么。请帮助我解决这个问题。

package com.sensortestactivity;

import android.app.Activity;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class SensorTestActivity extends Activity implements SensorEventListener {

    private SensorManager sensorManager;
    private Boolean color = false;
    private View view;
    private long lastUpdate;

    // -- CALLED WHEN THE ACTIVITY IS FIRST CREATED --//

    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        view = findViewById(R.id.textView);
        view.setBackgroundColor(Color.GREEN);

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);

        lastUpdate = System.currentTimeMillis();

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            getAccelerometer(event);
        }

    }

    private void getAccelerometer(SensorEvent event) {
        float[] values = event.values;

        // -- MOVEMENT --

        float x = values[0];
        float y = values[1];
        float z = values[2];
        float accelerationSquareRoot = (x * x + y * y + z * z)
                / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
        long actualTime = System.currentTimeMillis();

        if (accelerationSquareRoot >= 2) {
            if (actualTime - lastUpdate < 200) {
                return;
            }
            lastUpdate = actualTime;
            Toast.makeText(this, "DEVICE WAS SHUFFELED", Toast.LENGTH_SHORT)
                    .show();
            if (color) {
                view.setBackgroundColor(Color.GREEN);
            } else {
                view.setBackgroundColor(Color.RED);
            }
            color = !color;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // -- REGISTER THIS CLASS AS A LISTENER FOR THE ORIENTATION AND
        // ACCELEROMETER SENSORS --

        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);

    }
}
4

1 回答 1

0

你可以使用

view.setBackgroundColor(HEXVALUE);

十六进制值是这样使用的:0xAARRGGBB

AA = Alpha 通道 RR = 红色通道 GG = 绿色通道 BB = 蓝色通道

一个有效的颜色是:0xFFFF0000 这将创建红色另一个有效的颜色:0xFFF0F0F0

对于十六进制数字,您应该查看维基百科。http://en.wikipedia.org/wiki/十六进制

您可以使用随机数生成器随机设置颜色。

Math.random() * 17

这将为您提供从 0 开始并以 16 结束的整数值。您只需将它们计算为十六进制:0 = 0

1 = 1

2 = 2

...

9 = 9

10 = 一个

11 = 乙

...

16 = F

于 2012-07-05T13:09:41.953 回答