-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathMiniPhotoView.py
93 lines (83 loc) · 3.16 KB
/
MiniPhotoView.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# coding: utf-8
import ui, photos, console
class MyPictureView(ui.View):
def __init__(self, width, height):
self.frame = (0, 0, width, height)
self.iwidth = 200.0
self.iheight = 200.0
framesize = 10
iw = self.iwidth - 2 * framesize
ih = self.iheight - 2 * framesize
ratio = ih / iw
self.img = []
ass = photos.get_assets(media_type='image')
self.imgcount = min(len(ass), 100)
console.hud_alert(
"Please wait while {} photos are loading...".format(self.imgcount)
)
for i in range(self.imgcount):
img = ui.Image.from_data(ass[i].get_image_data().read())
w, h = img.size
rat = h / w
x_ratio = 1.0
y_ratio = 1.0
x = framesize
y = framesize
if ratio < 1: # landscape canvas
if rat <= ratio: # full width
y = ((ih - iw * rat) / 2) + framesize
y_ratio = iw * rat / ih
else: # full height
x = ((iw - ih / rat) / 2) + framesize
x_ratio = ih / rat / iw
elif ratio > 1: # portrait canvas
if rat > ratio: # full height
x = ((iw - ih / rat) / 2) + framesize
x_ratio = ih / rat / iw
else: # full width
y = ((ih - iw * rat) / 2) + framesize
y_ratio = iw * rat / ih
else: # cubic canvas
if rat < 1: # full width
y = ((ih - iw * rat) / 2) + framesize
y_ratio = iw * rat / ih
elif rat > 1: # full height
x = ((iw - ih / rat) / 2) + framesize
x_ratio = ih / rat / iw
else: # cubic
pass # x_ratio = y_ratio = 1.0
with ui.ImageContext(self.iwidth, self.iheight) as ctx:
img.draw(x, y, iw * x_ratio, ih * y_ratio)
self.img.append(ctx.get_image())
def draw(self):
i = 0
if self.imgcount < 100:
endrow = self.imgcount // 10 + 1
else:
endrow = 10
for row in range(endrow):
for column in range(10):
if i == self.imgcount:
break
if self.img[i]:
x = column * self.iwidth
y = row * self.iheight
self.img[i].draw(x, y, self.iwidth, self.iheight)
i += 1
class MiniPhotoView(ui.View):
def __init__(self):
self.view = ui.View(background_color="lightyellow")
self.view.name = "MiniPhotoView"
scrollview1 = ui.ScrollView()
scrollview1.name = "scrollview1"
scrollview1.flex = "WH"
scrollview1.content_size = (2000, 2000)
self.view.add_subview(scrollview1)
self.view.present("full_screen")
self.sv1 = self.view["scrollview1"]
width, height = self.sv1.content_size
self.sv1.add_subview(MyPictureView(width, height))
if photos.get_count():
MiniPhotoView()
else:
print("Sorry no access or no pictures.")