在onclick视图和切换案例中使用线程

时间:2016-07-08 06:53:47

标签: java android multithreading android-asynctask threadpool

我是Android开发的新手,我的应用程序已准备好但在某些设备上它打开导航抽屉和onclick事件上的某些东西时滞后。我试图使用线程,以便每个事件发生在新线程但我仍然看到滞后请帮助我。

public class MainActivity extends BaseActivity implements OnClickListener {
private Button scanBtn,scanBtn2,scanBtn3;
Animation animTranslate,animAlpha;
LinearLayout lv;
Handler handler;
String[] permissions;

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

    permissions = new String[3];
    permissions[0] = Manifest.permission.WRITE_EXTERNAL_STORAGE;
    permissions[1] = Manifest.permission.READ_EXTERNAL_STORAGE;
    permissions[2]=Manifest.permission.CAMERA;
    ActivityCompat.requestPermissions(
            this,
            permissions,
            5
    );



    scanBtn = (Button)findViewById(R.id.scanner);
    scanBtn2=(Button)findViewById(R.id.scanner2);
    scanBtn3=(Button)findViewById(R.id.scanner3);

    lv=(LinearLayout)findViewById(R.id.container);


    animTranslate=  AnimationUtils.loadAnimation(MainActivity.this,R.anim.anim_translate);
       animAlpha=AnimationUtils.loadAnimation(MainActivity.this,R.anim.anim_alpha);

            scanBtn.setOnClickListener(MainActivity.this);       

            scanBtn2.setOnClickListener(MainActivity.this);         

            scanBtn3.setOnClickListener(MainActivity.this);

 }     
 public void onClick(View v){
    if(v.getId()==R.id.scanner){
        v.startAnimation(animTranslate);
        lv.startAnimation(animAlpha);
        final IntentIntegrator scanIntegrator = new IntentIntegrator(this);
         scanIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
        scanIntegrator.setPrompt(" ");
        scanIntegrator.setCameraId(0);
        scanIntegrator.setBeepEnabled(true);
        scanIntegrator.setBarcodeImageEnabled(false);
        handler=new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                scanIntegrator.initiateScan();
            }
        },500);
    }else if(v.getId()==R.id.scanner2){
        v.startAnimation(animTranslate);
        lv.startAnimation(animAlpha);
        final IntentIntegrator scanIntegrator2=new  IntentIntegrator(this);
         scanIntegrator2.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
        scanIntegrator2.setPrompt(" ");
        scanIntegrator2.setCameraId(0);
        scanIntegrator2.setBeepEnabled(true);
        scanIntegrator2.setBarcodeImageEnabled(true);
        handler=new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                scanIntegrator2.initiateScan();
            }
        },500);
    }else if(v.getId()==R.id.scanner3){
        v.startAnimation(animTranslate);
        lv.startAnimation(animAlpha);
        final Intent intent=new  Intent(this,GenerateQRCodeActivity.class);
        handler=new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(intent);                     overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);

            }
        },500);
    }
 }

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanningResult =  IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    if (scanningResult != null) {
        String scanContent = scanningResult.getContents();
        String scanFormat = scanningResult.getFormatName();
        Intent intent1=new Intent(this,ResultActivity.class);
        intent1.putExtra("con",scanContent);
        if(scanContent != null && scanFormat != null) {
            startActivity(intent1);
        }

    }
    else{
        Toast toast = Toast.makeText(getApplicationContext(),
                "No scan data received!", Toast.LENGTH_SHORT);
        toast.show();
    }
 }


@Override
public boolean onKeyDown(int keyCode, KeyEvent e) {
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        // your action...

        if (!mDrawerLayout.isDrawerOpen(mDrawerList)) {
            mDrawerLayout.openDrawer(mDrawerList);
        }
        else{
            mDrawerLayout.closeDrawer(mDrawerList);
        }
        return true;
    }
    return super.onKeyDown(keyCode, e);
 }

@Override
public void onBackPressed() {
    if(mDrawerLayout.isDrawerOpen(mDrawerList)){
        mDrawerLayout.closeDrawer(mDrawerList);
    }
    else{
        finish();
    }
}
}

0 个答案:

没有答案