1

我是android开发的新手。我编写了这段代码,在屏幕上画了一个圆圈,通过移动设备,圆圈将在屏幕上移动。但在我的 Nexus 7 设备上速度很慢。你能帮我提高性能吗?

public class MainActivity extends Activity implements SensorEventListener {

public static SensorManager mSensorManager;
public static Sensor accelerometer;
public static Sensor magnetometer;
public static float[] mAccelerometer = null;
public static float[] mGeomagnetic = null;
private SampleCircle sampleCircle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainForm);
    sampleCircle = new SampleCircle(this);

    layout.addView(sampleCircle);


    layout.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            sampleCircle.setPoint(motionEvent.getX(), motionEvent.getY());
            return false;
        }
    });

    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
}

@Override
protected void onResume() {
    super.onResume();

    mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
    mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_GAME);
}

@Override
protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(this, accelerometer);
    mSensorManager.unregisterListener(this, magnetometer);
}

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

    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        mGeomagnetic = event.values;
    }

    if (mAccelerometer != null && mGeomagnetic != null) {
        float R[] = new float[9];
        float I[] = new float[9];
        boolean success = SensorManager.getRotationMatrix(R, I, mAccelerometer, mGeomagnetic);

        if (success) {
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);
            // at this point, orientation contains the azimuth(direction), pitch and roll values.
            double azimuth = 180 * orientation[0] / Math.PI;
            double pitch = 180 * orientation[1] / Math.PI;
            double roll = 180 * orientation[2] / Math.PI;

            sampleCircle.move((float) Math.floor(pitch), (float) Math.floor(roll));

        }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {
    System.out.println("Acc");
}

private class SampleCircle extends View {
    private Paint p = new Paint();
    {
        p.setColor(Color.RED);
        p.setStyle(Paint.Style.FILL);
    }
    private float y;
    private float x;

    public SampleCircle(Context context, float x, float y) {
        super(context);
        this.y = y;
        this.x = x;
    }

    public SampleCircle(Context context) {
        super(context);

    }

    public void setPoint(float x, float y) {
        this.x = x;
        this.y = y;
    }

    public void move(float x, float y) {
        this.x -= x;
        this.y -= y;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(x, y, 50, p);
        invalidate();
    }
}

}

4

3 回答 3

2

不要调用 invalidate(); 在您的 onDraw 方法中,仅当您想在新位置绘制圆时调用它:在 setPoint() 和 move() 方法中

于 2013-06-02T17:19:04.220 回答
1

调用View.invalidate()告诉操作系统通过调用来重绘视图,View.onDraw(Canvas)所以调用意味着设备将永远忙于重绘视图。所以正如@pskink 所建议的,应该从and方法中调用。但是,为了获得最大效率,您需要添加一个容差,这样如果移动很小,就不会费心重绘。invalidate()onDraw(...)View.invalidate()SampleCircle.setPoint(...)SampleCircle.move(...)

于 2013-06-02T19:11:14.983 回答
0

invalidate() 应该在 onSensorChanged(SensorEvent event) 中调用;并且仅在发生明显变化时。这肯定会提高性能

于 2014-12-04T16:15:42.680 回答