Android 应用开发 - 高 CPU 简单操作
Android App Dev - High CPU on simple actions
我目前正在开发一个应用程序。该应用程序可以处理客户和订单。我最近写了一个小函数来创建一个虚拟客户,以防订单还没有客户。客户包含位图、名称、url、电子邮件。当我使用这个简单的功能来创建该客户,然后导航到包含我所有客户(例如总共 2 个)的列表视图时,它会非常滞后。我得到大约 2 FPS,加载所有内容大约需要 5 秒。当我在为该任务创建的 GUI 中在应用程序本身中创建完全相同的客户时,它 运行 很流畅,因为它应该 运行。我不知道那是从哪里来的。
这是我在导航抽屉中单击 "Customers" 的 CPU 轨迹:
通过应用程序创建客户的代码:
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;
}
代码超级简单明了。我不知道代码的哪些其他部分对于该问题可能很重要。想不出任何。
希望有人知道这种行为并能提供一点帮助。
编辑:
客户class:
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();
}
}
虽然图像只有 30kB,但其像素大小为 1000x1000。将它减小到 200x200 像素解决了这个问题。我仍然怀疑,模拟器是那么弱还是 android 无法处理那个像素大小。如果有人对此有其他想法,请分享。
我目前正在开发一个应用程序。该应用程序可以处理客户和订单。我最近写了一个小函数来创建一个虚拟客户,以防订单还没有客户。客户包含位图、名称、url、电子邮件。当我使用这个简单的功能来创建该客户,然后导航到包含我所有客户(例如总共 2 个)的列表视图时,它会非常滞后。我得到大约 2 FPS,加载所有内容大约需要 5 秒。当我在为该任务创建的 GUI 中在应用程序本身中创建完全相同的客户时,它 运行 很流畅,因为它应该 运行。我不知道那是从哪里来的。
这是我在导航抽屉中单击 "Customers" 的 CPU 轨迹:
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;
}
代码超级简单明了。我不知道代码的哪些其他部分对于该问题可能很重要。想不出任何。
希望有人知道这种行为并能提供一点帮助。
编辑:
客户class:
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();
}
}
虽然图像只有 30kB,但其像素大小为 1000x1000。将它减小到 200x200 像素解决了这个问题。我仍然怀疑,模拟器是那么弱还是 android 无法处理那个像素大小。如果有人对此有其他想法,请分享。