Android App Dev - 简单操作的高CPU

时间:2017-05-26 08:24:14

标签: android performance cpu lag

我目前正在开发一款应用。该应用程序可以处理客户和订单。我最近写了一个小函数,创建了一个虚拟客户,以防订单没有客户。客户包含位图,名称,网址,电子邮件。当我使用这个简单的函数来创建该客户,然后导航到包含我所有客户的列表视图(例如总共2个)时,它就难以置信地落后。我得到2 FPS,一切都需要5秒才能加载。当我在我为该任务创建的GUI中在应用程序本身中创建完全相同的客户时,它将运行平稳,因为它应该运行。我不知道可能来自哪里。

这是CPU跟踪,我点击"客户"在导航抽屉中: enter image description here 通过应用程序创建客户的代码:

Bitmap icon = ((BitmapDrawable) customerImage.getDrawable()).getBitmap();

Customer customer =  new Customer(icon, name.getText().toString(), url.getText().toString(), email.getText().toString());

我直接在onCreate函数的MainActivity中创建客户的代码:

 public static Customer getEmptyCustomer(Activity act) {
        emptyCustomer  = new Customer(Utility.getIconEmpty(act),"-","-","-");
        return emptyCustomer;
    }

代码非常简单明了。我不知道代码的其他部分可能对该问题很重要。无法想到。

希望有人知道这种行为并且可以帮助一点。

编辑:

客户类:

 public Customer(Bitmap logo, String name, String URL, String email) {
        this.logo = logo;
        this.name = name;
        this.URL = URL;
        this.email = email;

        // arrayListCustomers.add(this);

        addCustomer(this);
    }

    public Customer() {

    }

    // return 0 = everything good
    // return 1 = name already exists
    public int addCustomer(Customer c) {

        boolean alreadyExists = false;

        for (Customer customer : arrayListCustomers) {
            if (customer.name.equals(c.name)) {
                alreadyExists = true;
            }
        }

        if (!alreadyExists) {
            arrayListCustomers.add(c);
            FileManager.saveCustomerToSdCard(c);
            return 0;
        } else {
            return 1;
        }
    }

FileManager功能:

 public static void saveCustomerToSdCard(Customer c) {

        String filename = c.name+".json";

        File customerDir = new File(Environment.getExternalStorageDirectory(), "Formicorn/Customer/");

        if (!customerDir.exists()) {
            if (!customerDir.mkdirs()) {
                Log.d("App", "Failed to create customer directory");
            }
        }


        Gson gson = new Gson();
        String json = gson.toJson(c);

        File file = new File(customerDir, filename);

        try {
            FileWriter writer = new FileWriter(file);
            writer.write(json);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


        saveLogoToSdCard(c);

    }


private static void saveLogoToSdCard(Customer c) {

    String filename = "logo_"+c.name+".png";

    File logosDir = new File(Environment.getExternalStorageDirectory(), "Formicorn/Customer/Logos");

    if (!logosDir.exists()) {
        if (!logosDir.mkdirs()) {
            Log.d("App", "Failed to create logo directory");
        } else {
            Log.d("App", "Successfully created logo directory");
        }
    }


    File logoFile = new File(logosDir, filename);

    boolean success = false;

    // Encode the file as a PNG image.
    FileOutputStream outStream;
    try {

        outStream = new FileOutputStream(logoFile);
        c.logo.compress(Bitmap.CompressFormat.PNG, 100, outStream);
    /* 100 to keep full quality of the image */

        outStream.flush();
        outStream.close();
        success = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

1 个答案:

答案 0 :(得分:0)

虽然图像仅为30kB,但其像素大小为1000x1000。将其减小到200x200像素解决了这个问题。我仍然怀疑,模拟器是弱者还是android无法处理像素化。如果有人对此有其他想法,请分享。