【发布时间】:2014-08-21 10:20:03
【问题描述】:
下面的代码是一个筹款晚宴购买土地的代码,目的是显示购买土地的平方米(约2976平方米)的进度。每次购买一平方米时,该应用程序都会添加一个对应于实际 1 平方米的图像块。最终,瓷砖(约 2976 个)像网格一样填满,一旦完全购买,就完成了土地。
每个图块的大小约为 320 字节,总共有 2976 个图块。
我还在下面展示了一个图片示例。
这段代码(在 javafx 中)让我抓狂的是,它消耗了大约 90% 到 100% 的 1 个处理器,并且内存使用量随着瓦片的增加而不断增加,直到代码缓冲区内存不足和程序在一段时间后崩溃。这在筹款晚宴上是不可取的。
完整的代码可用于测试 您需要将布尔拆分更改为真假,这将为您拆分图像(大约 3000 张图像);
https://github.com/rihani/Condel-Park-Fundraiser/tree/master/src/javafxapplication3
使用所有内存和 CPU 的罪魁祸首是下面显示的 AnimationTimer() 函数,我想知道是否有人可以帮助我减少这段代码中的内存和 CPU 使用率。
简单解释一下下面的代码是如何使用的,土地被分成2个窗格,当第一个grid_pane1被填满时,第二个窗格grid_pane2开始然后填满。 还有一个闪烁的磁贴用于显示当前进度。
我正在使用 total_donnation ++;测试代码,但通常会使用 mysql 来提取在 findraising 晚餐期间提出的新值
AnimationTimer() 代码:
translate_timer = new AnimationTimer() {
@Override public void handle(long now) {
if (now > translate_lastTimerCall + 10000_000_000l)
{
old_total_donnation = total_donnation;
try
{
// c = DBConnect.connect();
// SQL = "Select * from donations";
// rs = c.createStatement().executeQuery(SQL);
// while (rs.next())
// {total_donnation = rs.getInt("total_donnation");}
// c.close();
total_donnation ++;
if(total_donnation != old_total_donnation)
{
System.out.format("Total Donation: %s \n", total_donnation);
old_total_donnation = total_donnation;
if (!pane1_full)
{
grid_pane1.getChildren().clear();
grid_pane1.getChildren().removeAll(imageview_tile1,hBox_outter_last);
}
grid_pane2.getChildren().clear();
grid_pane2.getChildren().removeAll(imageview_tile2,hBox_outter_last);
for(i=0; i<=total_donnation; i++)
{
if (pane1_full){ System.out.println("Pane 1 has not been redrawn"); break;}
file1 = new File("pane1_img"+i+".png");
pane1_tiled_image = new Image(file1.toURI().toString(),image_Width,image_Height,false,false);
imageview_tile1 = new ImageView(pane1_tiled_image);
grid_pane1.add(imageview_tile1, current_column_pane1,current_row_pane1);
current_column_pane1 = current_column_pane1+1;
if (current_column_pane1 == max_columns_pane1 )
{
current_row_pane1 = current_row_pane1+1;
current_column_pane1 = 0;
}
if (i == max_donnation_pane1 ){ pane1_full = true; System.out.println("Pane 1 full"); break;}
if (i == total_donnation)
{
if (i != max_donnation_pane1)
{
hBox_outter_last = new HBox();
hBox_outter_last.setStyle(style_outter);
hBox_outter_last.getChildren().add(blink_image);
ft1 = new FadeTransition(Duration.millis(500), hBox_outter_last);
ft1.setFromValue(1.0);
ft1.setToValue(0.3);
ft1.setCycleCount(Animation.INDEFINITE);
ft1.setAutoReverse(true);
ft1.play();
grid_pane1.add(hBox_outter_last, current_column_pane1,current_row_pane1);
}
}
}
if (i < total_donnation)
{
total_donnation_left = total_donnation - max_donnation_pane1;
for(j=0; j<=total_donnation_left; j++)
{
file2 = new File("pane2_img"+j+".png");
pane2_tiled_image = new Image(file2.toURI().toString(),image_Width,image_Height,false,false);
imageview_tile2 = new ImageView(pane2_tiled_image);
grid_pane2.add(imageview_tile2, current_column_pane2,current_row_pane2);
current_column_pane2 = current_column_pane2+1;
if (current_column_pane2 == max_columns_pane2 )
{
current_row_pane2 = current_row_pane2+1;
current_column_pane2 = 0;
}
if (j == max_donnation_pane2 ){ System.out.println("Pane 2 full"); break;}
if (j == total_donnation_left)
{
if (j != max_donnation_pane2)
{
hBox_outter_last = new HBox();
hBox_outter_last.setStyle(style_outter);
hBox_outter_last.getChildren().add(blink_image);
ft = new FadeTransition(Duration.millis(500), hBox_outter_last);
ft.setFromValue(1.0);
ft.setToValue(0.3);
ft.setCycleCount(Animation.INDEFINITE);
ft.setAutoReverse(true);
ft.play();
grid_pane2.add(hBox_outter_last, current_column_pane2,current_row_pane2);
}
}
}
}
current_column_pane1 =0;
current_row_pane1=0;
current_column_pane2=0;
current_row_pane2=0;
}
}
catch (Exception ex) {}
translate_lastTimerCall = now;
}
}
};
【问题讨论】:
标签: image split grid javafx imageview