Created
February 14, 2016 08:25
-
-
Save codenameone/305c3f5426b0e2e80833 to your computer and use it in GitHub Desktop.
ImageViewer fetching image data dynamically from the internet in Codename One
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Form hi = new Form("ImageViewer", new BorderLayout()); | |
final EncodedImage placeholder = EncodedImage.createFromImage( | |
FontImage.createMaterial(FontImage.MATERIAL_SYNC, s). | |
scaled(300, 300), false); | |
class ImageList implements ListModel<Image> { | |
private int selection; | |
private String[] imageURLs = { | |
"http://awoiaf.westeros.org/images/thumb/9/93/AGameOfThrones.jpg/300px-AGameOfThrones.jpg", | |
"http://awoiaf.westeros.org/images/thumb/3/39/AClashOfKings.jpg/300px-AClashOfKings.jpg", | |
"http://awoiaf.westeros.org/images/thumb/2/24/AStormOfSwords.jpg/300px-AStormOfSwords.jpg", | |
"http://awoiaf.westeros.org/images/thumb/a/a3/AFeastForCrows.jpg/300px-AFeastForCrows.jpg", | |
"http://awoiaf.westeros.org/images/7/79/ADanceWithDragons.jpg" | |
}; | |
private Image[] images; | |
private EventDispatcher listeners = new EventDispatcher(); | |
public ImageList() { | |
this.images = new EncodedImage[imageURLs.length]; | |
} | |
public Image getItemAt(final int index) { | |
if(images[index] == null) { | |
images[index] = placeholder; | |
Util.downloadUrlToStorageInBackground(imageURLs[index], "list" + index, (e) -> { | |
try { | |
images[index] = EncodedImage.create(Storage.getInstance().createInputStream("list" + index)); | |
listeners.fireDataChangeEvent(index, DataChangedListener.CHANGED); | |
} catch(IOException err) { | |
err.printStackTrace(); | |
} | |
}); | |
} | |
return images[index]; | |
} | |
public int getSize() { | |
return imageURLs.length; | |
} | |
public int getSelectedIndex() { | |
return selection; | |
} | |
public void setSelectedIndex(int index) { | |
selection = index; | |
} | |
public void addDataChangedListener(DataChangedListener l) { | |
listeners.addListener(l); | |
} | |
public void removeDataChangedListener(DataChangedListener l) { | |
listeners.removeListener(l); | |
} | |
public void addSelectionListener(SelectionListener l) { | |
} | |
public void removeSelectionListener(SelectionListener l) { | |
} | |
public void addItem(Image item) { | |
} | |
public void removeItem(int index) { | |
} | |
}; | |
ImageList imodel = new ImageList(); | |
ImageViewer iv = new ImageViewer(imodel.getItemAt(0)); | |
iv.setImageList(imodel); | |
hi.add(BorderLayout.CENTER, iv); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample usage of the ImageViewer class.
From the Codename One project