主线程上的工作量太大了?

时间:2016-08-03 10:31:17

标签: android android-fragments fragment java-threads

我有一个包含图像和文本的片段。我的应用程序运行速度非常慢,导航抽屉需要30秒才能滑动。我在主线程中使用了什么?我声明textView方法中的ImageViewRun()要在不同的线程中编译。

  

Fragment Java;

public class headercode extends Fragment implements Runnable{

    ImageView image;
    TextView text;
    View myView;    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        myView = inflater.inflate(R.layout.frag, container, false);
        this.run();
        return myView;
    }
    @Override
    public void run(){   
        android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
        image = (ImageView)myView.findViewById(R.id.image);
        image.setBackgroundResource(R.drawable.ahmed);
        text = (TextView)myView.findViewById(R.id.text);

    }
}
  

我的MainActivity Java OnCreate方法;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener   {   

    Button button;
    headercode header;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);    

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();    

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        if (findViewById(R.id.fragment) != null){

            header = new headercode();
            getSupportFragmentManager().beginTransaction().add(R.id.fragment,header).commit();
        }

    }

1 个答案:

答案 0 :(得分:0)

您需要在线程中运行.run(),而不是使用Thread thread = new Thread(this /* the runnable */); thread.start();

document.body.style.opacity = "0.3";