java - Android 在主线程上做了太多事情 : Tried separate threads

标签 java android multithreading android-animation

我遇到了动画断断续续的问题,应用程序最终崩溃了。我尝试过分成线程,但我仍然收到在主线程中做了很多事情的消息。我假设我的传感器更新和 UI 中的工作负载成正比,如果我能找到一些方法来减轻 UI 线程的负载,动画和传感器将开始变得平滑。有没有办法让传感器监听器脱离 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);


    }

}

最佳答案

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

您可以通过以下链接了解更多相关信息 Async Task

关于java - Android 在主线程上做了太多事情 : Tried separate threads,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27868248/

相关文章:

multithreading - 如何使用协程懒惰地迭代一个大(大于内存)的 kotlin 序列

java - 了解 Android 上的多点触控?

android - 缓存 Fragment 是一种好习惯吗?

java - 尝试编译和启动一个开源 android 应用程序,出现以下错误 : Error inflating class com. android.deskclock.widget.RtlViewPager

android - Android-将不确定的ProgressBar与另一个线程一起使用

c++ - Qt中如何停止线程

java - 在 web-app 目录中创建嵌入式 hsqldb

java - 如何在 Amazon Lambda 中提供数据库凭证等配置参数?

java - 如何将数据客户端发送到服务器django channel

android - CSV 缺失值