srplab's blog

Discuss about multi language programming of c/c ,java,c#,python,etc. Distribute object middleware

Post sort order : index

9/28 POSTS
preview
7-9/28 POSTS

close

Writing android gui using python(gallery)

Apr 22, 2012 7:39 PMPublicPageviews 234 0

Introduction


Wrapandroid version is updated to 0.8.2. Please uses this version.

We might write python code outside eclipse. In this case, we have no logcat window to show print message from python. Do not worry about, cle supports syslog. The message can be output to syslog server. Using the following sentence to open syslog client:
SrvGroup._SetOutputPort("192.168.0.129",514)
The ipaddress should be adjusted based on your environment.

Output likes this:



Android gallery is a little more complicate gui element. In order to use gallery, we should first create an adapter, override its functions. And then, gallery object uses the adapter to obtain the content to be drawn.

Create adapter object

    
    Wrapandroid’s adpterClass encapsulates android java class adapter. It has functions can be override, such as getCount, getItem, getItemId, getView,etc. Detailed explains of these functions, please refer to android documents. 
1.    Create bitmap object
Bitmap0 = StarActivity.getBitmapDrawable(StarActivity.getResource("drawable/aqua02")).getBitmap();
Bitmap1 = StarActivity.getBitmapDrawable(StarActivity.getResource("drawable/aqua03")).getBitmap();
Bitmap2 = StarActivity.getBitmapDrawable(StarActivity.getResource("drawable/aqua04")).getBitmap();
Bitmap3 = StarActivity.getBitmapDrawable(StarActivity.getResource("drawable/aqua05")).getBitmap();
Bitmap resources locates in project directory res\drawable-hdpi.
2.    getCount, getItem,and getItemId function
MyAdapter = Service.AdapterClass._New()
def MyAdapter_getCount(self) :
    return 4;
MyAdapter.getCount = MyAdapter_getCount;  
def MyAdapter_getItem(self,position) :
    return position;
MyAdapter.getItem = MyAdapter_getItem;  
def MyAdapter_getItemId(self,position)  :
    return position;
MyAdapter.getItemId = MyAdapter_getItemId;

3.    getView function
def MyAdapter_getView(self,position,convertView,parent) :
    global Service;
    i = Service.ImageViewClass._New();
    if( position == 0 ) :
        i.setImageBitmap(Bitmap0);
    if( position == 1 ) :
        i.setImageBitmap(Bitmap1);
    if( position == 2 ) :
        i.setImageBitmap(Bitmap2);
    if( position == 3 ) :
        i.setImageBitmap(Bitmap3);  
    i.setGalleryLayoutParams(Service.FILL_PARENT,Service.FILL_PARENT);
    i.setScaleType("FIT_XY");
    i.setBackgroundResource(StarActivity.getResource("mGalleryItemBackground"));
    i._LockGC();
    return i;    
MyAdapter.getView = MyAdapter_getView;

Because imageview object is created by python, when function returns. the object will be freed by GC. So, we should call _LockGC before function return.

Create Gallery object


MyGallery = Service.GalleryClass._New(MyLayout)
def MyGallery_onItemClick(self,event,objid,position,id ):
    Service.ToastClass._New().makeText("[MyGallery] event on onItemClick is trigger "+objid+str(position)+str(id),0).show();
MyGallery.onItemClick = MyGallery_onItemClick;
MyGallery.setAdapter(MyAdapter);
MyGallery.setLinearLayoutParams(Service.FILL_PARENT,Service.FILL_PARENT);

Screenshot:



Examples download:

http://www.srplab.com/android/pythongui_gallery.rar

Report abuse for this article

Copyright © 2013 Yahoo!, Inc. All rights reserved