整个屏幕充满了像facebook主屏幕的图像 . 如果图像数量较少,则可以正常工作 . 但是一旦有更多图像,例如50张图像 . 在Android设备中滚动时,应用程序停留了一段时间 . 看起来好像图像中有振动 . 在三星(j7 prime)中,这个概率较低但很明显,并且在其他三星设备中似乎非常不舒服,并且它给用户带来了不安 . 然而,在我测试的其他设备中,例如在金立手机(S6s)中,该应用程序几乎无法播放 . 滚动非常困难,几乎不可滚动 .
我的演示代码
mainContainer = new Container (new BoxLayout (BoxLayout.y ()));
for (String imgUrl : allImages){
Image singleImg = URLImage.createToStorage(placeholder, "small_" + imgObject + k, imgObject.toString(), URLImage.RESIZE_SCALE);
Button b = new Button (singleImg);
mainContainer.add (b);
}
更新1:我已经将占位符大小更改为屏幕大小并添加了2个相同图像的btns,因此有相当大的数量 . 的图像 . 当我再次构建厨房水槽应用程序时,会出现与上述相同的问题 .
if (placeholder == null) {
Image placeholderTmp = Image.createImage(Display.getInstance().getDisplayWidth(), Display.getInstance().getDisplayWidth(), 0);
if (Display.getInstance().isGaussianBlurSupported()) {
placeholderTmp = Display.getInstance().gaussianBlurImage(placeholderTmp, 10);
} else {
placeholderTmp = placeholderTmp.modifyAlpha((byte) 100);
}
placeholder = EncodedImage.createFromImage(placeholderTmp, true);
}
InfiniteContainer ic = new InfiniteContainer(10) {
List items;
List allItems = new ArrayList();
String nextURL = WEBSERVICE_URL;
@Override
public Component[] fetchComponents(int index, int amount) {
if (index == 0) {
nextURL = WEBSERVICE_URL;
}
if (nextURL == null) {
return null;
}
ConnectionRequest req = new ConnectionRequest(nextURL) {
@Override
protected void readResponse(InputStream input) throws IOException {
items = null;
JSONParser parser = new JSONParser();
Map response = parser.parseJSON(new InputStreamReader(input, "UTF-8"));
items = (List) response.get("items");
nextURL = (String) response.get("nextPage");
}
@Override
protected void handleException(Exception err) {
Log.e(err);
Display.getInstance().callSerially(() -> {
ToastBar.showErrorMessage("An error occured while connecting to the server: " + err);
});
}
@Override
protected void handleErrorResponseCode(int code, String message) {
Display.getInstance().callSerially(() -> {
ToastBar.showErrorMessage("Error code from the server: " + code + "\n" + message);
});
}
};
req.setPost(false);
NetworkManager.getInstance().addToQueueAndWait(req);
if (items == null) {
return null;
}
allItems.addAll(items);
Component[] result = new Component[items.size()];
for (int i = 0; i < 3; i++) {
for (int iter = 0; iter < result.length; iter++) {
Map<String, Object> m = (Map<String, Object>) items.get(iter);
String title = (String) m.get("title");
String details = (String) m.get("details");
String url = (String) m.get("url");
String thumb = (String) m.get("thumb");
URLImage thumbImage = URLImage.createToStorage(placeholder, url.substring(url.lastIndexOf("/") + 1), thumb, URLImage.RESIZE_SCALE_TO_FILL);
ScaleImageButton btn = new ScaleImageButton(thumbImage);
ScaleImageButton btn1 = new ScaleImageButton(thumbImage);
ScaleImageButton btn2 = new ScaleImageButton(thumbImage);
btn.setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FILL);
btn.getAllStyles().setMarginBottom(8);
btn1.setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FILL);
btn1.getAllStyles().setMarginBottom(8);
btn2.setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FILL);
btn2.getAllStyles().setMarginBottom(8);
result[iter] = BoxLayout.encloseY(btn,btn1,btn2);
}
}
Layout l = getLayout();
if (l instanceof GridLayout) {
int cmps = getComponentCount() - 1 + result.length;
int extra = 0;
if (cmps % 3 != 0) {
extra = 1;
}
setLayout(new GridLayout(cmps / 3 + extra, 3));
}
return result;
}
}
在这里,我添加了mainContainer中的按钮,并没有看到任何改进性能相关的解决方案 . 还有什么我需要寻找的吗?谢谢
1 回答
我建议查看厨房水槽中的狗演示和开发者指南中的性能部分 .
还可以看看厨房水槽里的联系人演示,我们懒洋洋地加载信息 .
每个
URLImage
都会执行下载/延迟加载,这两者都可能会破坏用户界面,你需要排队并处理它 .