public class gallerymovetest extends activity {
private textview tv_hinttextview;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.gallerytest);
tv_hinttextview = (textview) findviewbyid(r.id.tv_hint);
new asynctask<string, integer, arraylist<hashmap<string, drawable>>>() {
@override
protected arraylist<hashmap<string, drawable>> doinbackground(
string... params) {
arraylist<hashmap<string, drawable>> applist = null;
applist = getapplist();
return applist;
}
protected void onpostexecute(
final arraylist<hashmap<string, drawable>> result) {
super.onpostexecute(result);
if (result != null) {
final gallery gallery = (gallery) findviewbyid(r.id.gallery1);
gallery.setpadding(10, 10, 10, 10);
gallery.setadapter(new imageadapter(gallerymovetest.this,
result));
final progressbar pbar = (progressbar) findviewbyid(r.id.pb_gallery);
pbar.setmax(result.size());
final handler handler = new handler() {
public void handlemessage(android.os.message msg) {
int what = msg.what;
pbar.setprogress(what + 1);
gallery.setselection(what);
tv_hinttextview.settext((what + 1) + "/"
+ result.size());
log.i("max", "max:" + result.size() + "prog "
+ (what + 1));
}
};
new thread(new runnable() {
private int progress = 0;
@override
public void run() {
while (progress < result.size()) {
try {
thread.sleep(500);
} catch (interruptedexception e) {
e.printstacktrace();
}
handler.sendemptymessage(progress++);
}
}).start();
}
}.execute();
}
private arraylist<hashmap<string, drawable>> getapplist() {
packagemanager pmanager = getpackagemanager();
list<applicationinfo> applications = pmanager
.getinstalledapplications(0);
arraylist<hashmap<string, drawable>> list = null;
if (applications != null && applications.size() > 0) {
list = new arraylist<hashmap<string, drawable>>();
hashmap<string, drawable> map = null;
for (applicationinfo applicationinfo : applications) {
map = new hashmap<string, drawable>();
string name = applicationinfo.loadlabel(pmanager).tostring();
drawable icon = applicationinfo.loadicon(pmanager);
map.put(name, icon);
list.add(map);
}
return list;
public class imageadapter extends baseadapter {
private context mcontext;
private arraylist<hashmap<string, drawable>> mlist;
public imageadapter(context context,
arraylist<hashmap<string, drawable>> list) {
this.mcontext = context;
this.mlist = list;
@override
public int getcount() {
return mlist.size();
public object getitem(int position) {
return position;
public long getitemid(int position) {
public view getview(int position, view convertview, viewgroup parent) {
hashmap<string, drawable> hashmap = mlist.get(position);
collection<drawable> values = hashmap.values();
imageview imageview = null;
for (drawable drawable : values) {
imageview = new imageview(mcontext);
imageview.setscaletype(scaletype.fit_xy);
gallery.layoutparams galleryparams = new gallery.layoutparams(
100, 100);
imageview.setlayoutparams(galleryparams);
imageview.setimagedrawable(drawable);
return imageview;
}
转载:http://blog.csdn.net/chaoyu168/article/details/49077223