python for android : 显示图片
参考: https://code.google.com/p/android-scripting/wiki/FullScreenUI
这是一个简单的图片查看小程序 ,只有基本功能: back 选图片 next ,还有一个没能实现的功能:横屏显示。
imageview.py
# -*- coding: utf-8 -*- import android import os,sys reload(sys) sys.setdefaultencoding(‘utf-8‘) droid = android.Android() base_dir = ‘/mnt/sdcard/DCIM/‘ if not os.path.isdir(base_dir): print base_dir,‘is not dir‘ sys.exit(4) def show_dir(path=base_dir): """Shows the contents of a directory in a list view.""" nodes = sorted(os.listdir(path)) # Make a way to go up a level. if path != base_dir: nodes.insert(0, ‘..‘) droid.dialogCreateAlert(os.path.basename(path).title()) droid.dialogSetItems(nodes) droid.dialogShow() # Get the selected file or directory. result = droid.dialogGetResponse().result droid.dialogDismiss() if ‘item‘ not in result: return target = nodes[result[‘item‘]] target_path = os.path.join(path, target) if target == ‘..‘: target_path = os.path.dirname(path) if os.path.isdir(target_path): return show_dir(target_path) elif os.path.splitext(target)[1].lower() in (‘.jpg‘,‘.png‘): return target_path # inform the user. else: droid.makeToast(‘Only .jpg .png files are currently supported!‘) return target_path = None target = ‘‘ path =‘‘ alist =[] def init(): global target_path, target,path,nodes target_path = show_dir() path = os.path.dirname(target_path) alist = imagelist(path) show_image() return def show_image(): global target_path, target,path if target_path is None: return if os.path.exists(target_path): target = os.path.basename(target_path) img = ‘file://‘ + target_path droid.fullSetProperty("imageView","src",img) else: droid.makeToast(‘Error: %s not exists‘,path) return def imagelist(path): global alist nodes = sorted(os.listdir(path)) alist =[] for f in nodes: if os.path.splitext(f)[1].lower() in (‘.jpg‘,‘.png‘): alist.append(f) return def back(): global target_path,target, alist j = None for i in range(0,len(alist)): if alist[i]==target: j=i break if j>0: target = alist[j-1] target_path = os.path.join(path, target) show_image() else: droid.makeToast(‘this is begin image‘) return def next(): global target_path,target, alist j = None len1 = len(alist) for i in range(0,len1): if alist[i]==target: j=i break if j < len1-1: target = alist[j+1] target_path = os.path.join(path, target) show_image() else: droid.makeToast(‘this is end image‘) return def landscape(): droid.makeToast(‘this is landscape function‘) return def eventloop(): while True: event=droid.eventWait().result if event["name"]=="click": id=event["data"]["id"] if id=="image": init() if id=="next": next() if id=="back": back() if id=="landscape": landscape() if id=="Exit": return elif event["name"]=="screen": if event["data"]=="destroy": return layout = """<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/background" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff000000"> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/Exit" android:layout_width="60dp" android:layout_height="wrap_content" android:text="退出" /> <Button android:id="@+id/back" android:layout_width="60dp" android:layout_height="wrap_content" android:text="back" /> <Button android:id="@+id/image" android:layout_width="60dp" android:layout_height="wrap_content" android:text="图片" /> <Button android:id="@+id/next" android:layout_width="60dp" android:layout_height="wrap_content" android:text="next" /> <Button android:id="@+id/landscape" android:layout_width="60dp" android:layout_height="wrap_content" android:text="目" /> </LinearLayout> <ImageView android:id="@+id/imageView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> """ droid.fullShow(layout) eventloop() droid.fullDismiss()
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。