一、前言
QPython 3c在大佬的改進下,擁有了基于sl4a的FullScreenWrapper2全屏框架,文章將用該框架制作我們的可視化應用【ONE一個】,
二、最終效果如下

三、準備作業
- AIDE: 使用布局助手生成xml布局代碼
- QPython 3C: 使用FullScreenWrapper2制作可視化應用
以上應用在后臺回復應用名稱即可獲取下載鏈接,如【AIDE】
四、實作思路
- 使用AIDE生成布局代碼
- 分析網站獲取ONE api
- 使用FullScreenWrapper完成可視化應用
使用AIDE生成布局代碼
在aide新建專案,在app/src/main/res/layout下新建xml,點擊右上角的圖片按鈕進入設計界面,按照以下進行設計,在qpython中展示可能需要做調整,

然后回傳,復制xml代碼,xml代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_height="fill_parent"
android:gravity="top"
android:orientation="vertical"
android:background="#FFF8F9FD">
<LinearLayout
android:orientation="horizontal"
android:layout_
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FF8BC6A7">
<TextView
android:layout_
android:layout_height="wrap_content"
android:layout_weight="6"
android:text="ONE?一個"
android:id="@+id/bar_title"
android:textSize="8sp"
android:layout_gravity="left|center_vertical"
android:textColor="#FFFFFFFF"
android:layout_marginLeft="10dp"/>
<Button
android:layout_
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="分享"
android:gravity="left"
android:layout_gravity="right"
android:id="@+id/btn_share"
android:textColor="#FFFFFFFF"
android:background="#FF8BC6A7"/>
<Button
android:layout_
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="right"
android:text="退出"
android:gravity="left"
android:id="@+id/btn_exit"
android:textColor="#FFFFFFFF"
android:background="#FF8BC6A7"
android:layout_marginLeft="12dp"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_
android:layout_height="0dp"
android:layout_weight="19"
android:background="#FFF8F9FD"
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<LinearLayout
android:layout_
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FFF8F9FD"
android:layout_marginTop="25dp">
<TextView
android:layout_
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="圖片標題"
android:textSize="5sp"
android:textColor="#FF4B4B4B"
android:id="@+id/title"
android:gravity="left|center_vertical"/>
<TextView
android:layout_
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="圖片時間"
android:textSize="5sp"
android:textColor="#FF4B4B4B"
android:id="@+id/date"
android:gravity="right|center_vertical"/>
</LinearLayout>
<ImageView
android:src="https://www.cnblogs.com/bushrose/archive/2023/02/21/@drawable/ic_delete"
android:layout_
android:layout_height="0dp"
android:id="@+id/pic"
android:scaleType="fitXY"
android:layout_weight="6"/>
<TextView
android:layout_
android:layout_height="0dp"
android:layout_weight="1"
android:text="圖片作者"
android:textSize="5sp"
android:textColor="#FF4B4B4B"
android:id="@+id/pic_author"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<TextView
android:layout_
android:layout_height="wrap_content"
android:text="內容"
android:textSize="7sp"
android:textColor="#FF000000"
android:id="@+id/content"
android:gravity="top|left"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"/>
<TextView
android:layout_
android:layout_height="0dp"
android:layout_weight="6"
android:text="文章作者"
android:textSize="5sp"
android:textColor="#FF4B4B4B"
android:layout_gravity="top|right"
android:id="@+id/text_author"
android:paddingLeft="20dp"/>
</LinearLayout>
<LinearLayout
android:background="#FFFFFFFF"
android:orientation="horizontal"
android:layout_
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<Button
android:layout_
android:layout_height="wrap_content"
android:text="上一個"
android:textColor="#FF37B1E8"
android:background="#FFFFFFFF"
android:layout_gravity="bottom"
android:layout_weight="1"
android:shadowDy="2"
android:id="@+id/btn_prev"/>
<Button
android:layout_
android:layout_height="wrap_content"
android:text="下一個"
android:textColor="#FF37B1E8"
android:background="#FFFFFFFF"
android:layout_gravity="bottom"
android:layout_weight="1"
android:id="@+id/btn_next"/>
</LinearLayout>
</LinearLayout>
分析網站請求獲取ONE api
- 分析步驟參考:https://www.jianshu.com/p/e9617107b748
將其中代碼改寫為python版本
class OneApi(object):
def __init__(self):
self.TOKEN=''
self.API='http://m.wufazhuce.com/one/ajaxlist/'
self.COOKIES=''
def getToken(self):
if self.TOKEN:
return self.TOKEN
url='http://m.wufazhuce.com/one'
try:
res=requests.get(url)
if res.status_code==200:
self.COOKIES=res.headers['Set-Cookie']
_token=res.text.split("One.token = '")[1].split("'")[0]
if _token and len(_token)==40:
self.TOKEN=_token
return _token
else:
print('未獲取到token')
return ""
except Exception:
pass
def getData(self,page=0):
token=self.getToken()
url=self.API + str(page) + '?_token=' + token
headers = {
'Cookie':self.COOKIES
}
res=requests.get(url,headers=headers)
if res.status_code==200:
return json.loads(res.text)['data']
else:
return None
使用FullScreenWrapper完成可視化應用
class OnePic(Layout):
def __init__(self):
self.api=api
self.index=0
self.page=0
self.articles=self.api.getData(self.page)
super(OnePic,self).__init__(xmldata,"ONE一個")
def on_show(self):
# 給按鈕注冊事件,以及初始化
self.add_event(key_EventHandler(handler_function=self.close_app))
self.views.btn_share.add_event(click_EventHandler(self.views.btn_share,self.share))
self.views.btn_exit.add_event(click_EventHandler(self.views.btn_exit,self.close_app))
self.views.btn_prev.add_event(click_EventHandler(self.views.btn_prev,self.btn_prev))
self.views.btn_next.add_event(click_EventHandler(self.views.btn_next,self.btn_next))
article=self.articles[self.index]
self.views.title.text=article['title']
self.views.date.text=article['date']
url=api.getPic(article["id"], article["img_url"])
self.views.pic.src="file://"+url
self.views.pic_author.text=article["picture_author"]
self.views.content.text=article["content"]
self.views.text_author.text=article["text_authors"]
def on_close(self):
# 關閉應用時執行的
pass
def close_app(self,view,event):
# 退出app
FullScreenWrapper2App.exit_FullScreenWrapper2App()
def btn_prev(self,view,event):
# 按鈕上一個的事件函式
article=self.prev()
if article:
self.views.title.text=article['title']
self.views.date.text=article['date']
url=api.getPic(article["id"], article["img_url"])
self.views.pic.src="file://"+url
self.views.pic_author.text=article["picture_author"]
self.views.content.text=article["content"]
self.views.text_author.text=article["text_authors"]
def btn_next(self,view,event):
# 按鈕下一個的事件函式
article=self.next()
if article:
self.views.title.text=article['title']
self.views.date.text=article['date']
url=api.getPic(article["id"], article["img_url"])
self.views.pic.src="file://"+url
self.views.pic_author.text=article["picture_author"]
self.views.content.text=article["content"]
self.views.text_author.text=article["text_authors"]
def share(self, view, event):
# 按鈕分享的事件函式,分享至微信
action="android.intent.action.SEND"
mime="text/plain"
article=self.articles[self.index]
extras={
"android.intent.extra.SUBJECT":"分享",
"android.intent.extra.TEXT":article["content"]+"——"+article["text_authors"]
}
flags=268435456
packageName="com.tencent.mm"
className="com.tencent.mm.ui.tools.ShareImgUI"
intent=droid.makeIntent(action=action,type=mime,extras=extras,flags=flags,packagename=packageName,classname=className)
droid.startActivityIntent(intent.result)
def prev(self):
if self.index==0:
if self.page!=0:
self.page=self.articles[0]['id']
self.articles=self.api.getData()
self.index=len(self.articles)-1
return self.articles[self.index]
else:
droid.makeToast("暫無更多資料")
return None
else:
self.index=self.index-1
return self.articles[self.index]
def next(self):
if self.index==len(self.articles)-1:
self.page=self.articles[len(self.articles)-1]["id"]
self.articles=self.api.getData(self.page)
self.index=0
return self.articles[self.index]
else:
self.index=self.index+1
return self.articles[self.index]
完整代碼
后臺回復【one一個】即可獲取原始碼下載鏈接,將
不足之處
- 下載的圖片未做清除處理,請求得越多,圖片會下載,占用手機空間
- 分享功能不完善,能分享多個渠道更好,比如qq、微博等
- 用戶的瀏覽資料未做配置化,都是從第一頁開始,不能從上次的位置接著看
注意:將fullscreenwrapper2_py3.py放置在storage/emulated/0/qpython/lib/python3.11/site-packages
五,總結
本文主要用于學習python知識,讓大家在實操中完成技能學習,如有不足之處,請大家評論區留言評論,
本文由【產品經理不是經理】gzh同步發布,歡迎關注
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/544580.html
標籤:其他
上一篇:Java+Jquer實作趨勢圖
下一篇:JUC學習-執行緒池部分
