Android在主线程上做了很多:尝试了单独的线程

时间:2015-01-09 19:58:29

标签: java android multithreading android-animation

我遇到了乱七八糟的动画问题,应用程序最终崩溃了。我已经尝试分离到线程但我仍然得到在主线程中做很多事情的消息。我假设我的传感器更新和UI中的工作量是直接成比例的,如果我可以一些如何从UI线程的负载,事物将开始平滑动画和传感器。有没有办法让Sensor监听器脱离UI线程并仍然在UI中更新View?

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;


public class MyActivity extends Activity implements SensorEventListener {



private SensorManager mSensorManager;
private Sensor sensorAccelerometer;
private Sensor sensorMagneticField;
public static ImageView right;
public static ImageView left;

double pitch = 0;
double roll = 0;
public static float currentX = 0f;
public static float currentY = 0f;
public static float degreeR = 0.f;
public static float degreeL = 0.f;

public static TextView rightText;
public static TextView leftText;

public static RotateAnimation rotationLeft;
public static RotateAnimation rotationRight;

private float[] valuesAccelerometer;
private float[] valuesMagneticField;

private float[] matrixR;
private float[] matrixI;
private float[] matrixValues;

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

    left = (ImageView)findViewById(R.id.leftView);
    right = (ImageView)findViewById(R.id.rightView);
    leftText = (TextView)findViewById(R.id.leftText);
    rightText = (TextView)findViewById(R.id.rightText);

    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    sensorAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    sensorMagneticField = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

    valuesAccelerometer = new float[3];
    valuesMagneticField = new float[3];

    matrixR = new float[9];
    matrixI = new float[9];
    matrixValues = new float[3];



}





 @Override
 public void onSensorChanged(final SensorEvent event) {

    switch (event.sensor.getType()){
        case Sensor.TYPE_ACCELEROMETER:
            for (int i =0; i <3;i++){
                valuesAccelerometer[i] = event.values[i];
            }
            break;
        case Sensor.TYPE_MAGNETIC_FIELD:
            for (int i =0;i<3;i++){
                valuesMagneticField[i] = event.values[i];
            }
            break;
    }
    boolean success = SensorManager.getRotationMatrix(
            matrixR,
            matrixI,
            valuesAccelerometer,
            valuesMagneticField);
    if (success){
        SensorManager.getOrientation(matrixR,matrixValues);
        double azimuth = Math.toDegrees(matrixValues[0]);
        pitch = Math.toDegrees(matrixValues[1]);
        roll = Math.toDegrees(matrixValues[2]);
    }



    degreeL = Math.round(roll);
    degreeR = Math.round(pitch);

    Thread animateL = new animateLeft();
    Thread animateR = new animateRight();
    animateL.run();
    animateR.run();
        /*Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentX = (-degreeR-degreeR);
                currentY = ((degreeL - 90)-degreeL);///working here
            }
        });*/



}



     import android.view.animation.Animation;
     import android.view.animation.LinearInterpolator;
    import android.view.animation.RotateAnimation;

    public class animateLeft extends Thread {

    @Override
    public  void run() {

       android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
       MyActivity.leftText.setText("" + MyActivity.degreeL);

        MyActivity.rotationRight = new RotateAnimation(
                MyActivity.currentX,
                MyActivity.degreeR,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        MyActivity.rotationLeft = new RotateAnimation(
                MyActivity.currentY,
                MyActivity.degreeL,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        MyActivity.rotationRight.setInterpolator(new LinearInterpolator());
        MyActivity.rotationRight.setFillAfter(true);
        MyActivity.rotationRight.setDuration(100);
        MyActivity.currentY = (MyActivity.degreeL+90);
        MyActivity.left.startAnimation(MyActivity.rotationLeft);


    }


    }
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;

public class animateRight extends Thread {
    @Override
    public void run() {
        android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
      MyActivity.rightText.setText("" + MyActivity.degreeR);
       MyActivity.rotationLeft = new RotateAnimation(
                MyActivity.currentY,
                MyActivity.degreeL,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        MyActivity.rotationRight = new RotateAnimation(
                MyActivity.currentX,
                MyActivity.degreeR,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);

        MyActivity.rotationLeft.setDuration(100);
        MyActivity.rotationLeft.setFillAfter(true);
        MyActivity.currentX = (-MyActivity.degreeR);
        MyActivity.rotationLeft.setInterpolator(new LinearInterpolator());
        MyActivity.right.startAnimation(MyActivity.rotationRight);


    }

}

2 个答案:

答案 0 :(得分:1)

您应该将AsyncTask用于此类目的。它允许您通过将其封装在doInBackground(Params ...)方法中在后台执行任务,一旦完成,您可以使用AsyncTask类的onPostExecute(Result)发布UI事件。

您可以在以下链接Async Task

中详细了解相关信息

答案 1 :(得分:0)

有点晚了,但如果其他人还想知道,这是实现这一目标的好方法。和多线程一样,确保你知道自己在做什么并花时间去做,以避免那些奇怪的错误。玩得开心!

班级成员:

private HandlerThread mSensorThread;
private Handler mSensorHandler;
在OnCreate中

mSensorThread = new HandlerThread("Sensor thread", Thread.MAX_PRIORITY);
mSensorThread.start();
mSensorHandler = new Handler(mSensorThread.getLooper()) //Blocks until looper is prepared, which is fairly quick
yourSensorManager.registerListener(yourListener, yourSensor, interval, mSensorHandler);

取消注册时,也可以:

mSensorThread.quitSafely();