Codenameone 如果有许多图像在某些设备中几乎不可用,则会出现滚动问题

Codenameone 如果有许多图像在某些设备中几乎不可用,则会出现滚动问题,codenameone,Codenameone,整个屏幕充满了像facebook主屏幕一样的jst图像。如果图像数量较少,则效果良好。但只要有更多的图片,例如50张图片。在android设备上滚动时,该应用程序卡住了一段时间。图像中似乎有振动。在三星(j7 prime)中,这个问题较低,但很明显&在其他三星设备中似乎很不舒服&这给用户带来了不安。然而,在我测试过的其他一些设备中,例如Gionee mobiles(S6s),该应用程序几乎无法运行。滚动非常困难,几乎无法滚动 我的演示代码 mainContainer = new Cont

整个屏幕充满了像facebook主屏幕一样的jst图像。如果图像数量较少,则效果良好。但只要有更多的图片,例如50张图片。在android设备上滚动时,该应用程序卡住了一段时间。图像中似乎有振动。在三星(j7 prime)中,这个问题较低,但很明显&在其他三星设备中似乎很不舒服&这给用户带来了不安。然而,在我测试过的其他一些设备中,例如Gionee mobiles(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:我已经将占位符大小更改为屏幕大小,并添加了两个具有相同图像的BTN,因此图像数量相当大。然后,当我再次构建厨房水槽应用程序时,可以看到与上面相同的问题

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;
    }
}
if(占位符==null){
图像占位符tmp=Image.createImage(Display.getInstance().getDisplayWidth(),Display.getInstance().getDisplayWidth(),0);
if(Display.getInstance().isGaussianBlurSupported()){
占位符tmp=Display.getInstance().gaussianBlurImage(占位符tmp,10);
}否则{
占位符tmp=占位符tmp.modifyAlpha((字节)100);
}
占位符=EncodedImage.createFromImage(占位符tmp,true);
}
InfiniteContainer ic=新的InfiniteContainer(10){
清单项目;
List allItems=new ArrayList();
字符串nextURL=WEBSERVICE\u URL;
@凌驾
公共组件[]获取组件(整数索引,整数金额){
如果(索引==0){
nextURL=WEBSERVICE\u URL;
}
if(nextURL==null){
返回null;
}
ConnectionRequest req=新的ConnectionRequest(nextURL){
@凌驾
受保护的void readResponse(InputStream输入)引发IOException{
items=null;
JSONParser=新的JSONParser();
Map response=parser.parseJSON(新的InputStreamReader(输入,“UTF-8”));
items=(List)response.get(“items”);
nextur=(字符串)response.get(“nextPage”);
}
@凌驾
受保护的void handleException(异常错误){
Log.e(err);
Display.getInstance().callSerially(()->{
ToastBar.showErrorMessage(“连接到服务器时出错:“+err”);
});
}
@凌驾
受保护的无效handleErrorResponseCode(整数代码、字符串消息){
Display.getInstance().callSerially(()->{
ToastBar.showErrorMessage(“来自服务器的错误代码:“+code+”\n“+message”);
});
}
};
请求setPost(错误);
NetworkManager.getInstance().addToQueueAndWait(req);
if(items==null){
返回null;
}
allItems.addAll(项目);
组件[]结果=新组件[items.size()];
对于(int i=0;i<3;i++){
对于(int-iter=0;iter

在这里,我在mainContainer中添加了按钮,但没有看到与性能相关的解决方案有任何改进。还有什么我要找的吗?谢谢你

我建议你看看《开发者指南》中厨房水槽中的狗演示和性能部分

还可以看看厨房水槽中的联系人演示,我们在那里懒洋洋地加载信息


每个
urlmimage
都会执行一个下载/懒散加载,这两个操作都会潜在地阻塞UI,您需要排队处理它

我看过kitchen sink webService,它在设备中运行良好。但我改变了占位符的大小(高度和宽度)以适应屏幕大小,并增加了厨房水槽页面中的图像数量。然后同样的问题也出现了。我已经更新了上面更新1中更改的代码。如果你有大量的整版图片滚动,你会对GC造成很大的冲击。你需要优化图像的使用,并做大量的底层工作,这样的UI才能顺利工作。我怎样才能优化图像的使用?我不明白。查看我定制的厨房水槽演示视频。(更新上面的1个代码)滚动多次停止&有一个类似振动的事情正在发生。。。downloadUrlToStorageInBackground解决了滚动问题,但缺点是它每次在屏幕上下载并替换图像