QPython實例03-製作【ONE一個】可視化應用

来源:https://www.cnblogs.com/bushrose/archive/2023/02/21/17142576.html
-Advertisement-
Play Games

一、前言 QPython 3c在大佬的改進下,擁有了基於sl4a的FullScreenWrapper2全屏框架。文章將用該框架製作我們的可視化應用【ONE一個】。 二、最終效果如下 三、準備工作 AIDE: 使用佈局助手生成xml佈局代碼 QPython 3C: 使用FullScreenWrappe ...


一、前言

QPython 3c在大佬的改進下,擁有了基於sl4a的FullScreenWrapper2全屏框架。文章將用該框架製作我們的可視化應用【ONE一個】。

二、最終效果如下

三、準備工作

  • AIDE: 使用佈局助手生成xml佈局代碼
  • QPython 3C: 使用FullScreenWrapper2製作可視化應用

以上應用在後臺回覆應用名稱即可獲取下載鏈接,如【AIDE】

四、實現思路

  1. 使用AIDE生成佈局代碼
  2. 分析網站獲取ONE api
  3. 使用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_width="fill_parent"
	android:layout_height="fill_parent"
	android:gravity="top"
	android:orientation="vertical"
	android:background="#FFF8F9FD">
	
	<LinearLayout
		android:orientation="horizontal"
		android:layout_width="match_parent"
		android:layout_height="0dp"
		android:layout_weight="1"
		android:background="#FF8BC6A7">

		<TextView
			android:layout_width="wrap_content"
			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_width="0dp"
			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_width="0dp"
			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_width="match_parent"
		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_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#FFF8F9FD"
            android:layout_marginTop="25dp">
		
            <TextView
                android:layout_width="0dp"
                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_width="0dp"
                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="@drawable/ic_delete"
			android:layout_width="wrap_content"
			android:layout_height="0dp"
			android:id="@+id/pic"
			android:scaleType="fitXY"
			android:layout_weight="6"/>

		<TextView
			android:layout_width="wrap_content"
			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_width="wrap_content"
			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_width="wrap_content"
			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_width="match_parent"
		android:layout_height="wrap_content"
		android:layout_gravity="bottom">

		<Button
			android:layout_width="wrap_content"
			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_width="wrap_content"
			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

將其中代碼改寫為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同步發佈,歡迎關註


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一、背景 遠程服務將電腦程式的工作範圍從單機擴展到網路,從本地延伸至遠程,是構建分散式系統的首要基礎。遠程服務調用(Remote Procedure Call,RPC)在電腦科學中已經存在了超過四十年時間。但很多人無法明確區分RPC與Rest。本文就講一講RPC和Rest的本質區別。 二、分析 ...
  • 談到java中的併發,我們就避不開線程之間的同步和協作問題,談到線程同步和協作我們就不能不談談jdk中提供的AbstractQueuedSynchronizer(翻譯過來就是抽象的隊列同步器)機制; (一)、AQS中的state和Node含義: AQS中提供了一個int volatile state ...
  • RabbitMQ 配置環境 安裝 erlang環境以及RabbitMQ RabbitMQ埠號: 5672 去官網下載 https://www.rabbitmq.com 然後重啟RabbitMQ服務 RabbitMQ安裝教程 開放埠15672 這裡,通過http://IP地址:15672 進行We ...
  • 在如 rails 這樣的開源庫中,我們常常見到這樣的一類寫法: class_eval <<-RUBY, lxx_file, lxx_line + 1 def xxx # do something here end RUBY 令人困惑不已。 不過這裡的知識點非常簡單,只要掌握了 heredoc 與 e ...
  • 前言 文章主要說明在FA中的中文函數的代碼實現,不僅要知道用法,更要知其實現的原理。前面的用法為FA中的用法,僅支持在FA中使用,源碼可以在其它app中使用。 非原創,代碼收集整理於網路。 進入子頁面 進入子頁面("頁面名稱") lua源碼: function 進入子頁面(name,param) i ...
  • 0、引言 我們在嵌入式開發的過程中,經常可以碰到在一些巨集定義或者是代碼段中使用了do {...} while(0)的語句,從語義上理解,do {...} while(0)內的邏輯就只執行一次,並沒有迴圈執行,粗略看來,似乎畫蛇添足了,那麼為什麼還需要在只執行一次的邏輯外面加上一層do {...} w ...
  • 隊列的概念 在說隊列之前,先回憶一下棧是什麼,我們一般說棧是一個先進後出的數據結構,而隊列就是先進先出的數據結構。 隊列是定在表的一端進行插入,表的另一端進行刪除。 通常,我們稱進數據的一端為隊尾,出數據的一端為隊首(這邊需要註意,經常會記反起碼我是這樣的),數據元素進隊列的過程稱為入隊,出隊列的過 ...
  • 自定義線程池 package com.appletree24; import java.util.ArrayDeque; import java.util.Deque; import java.util.HashSet; import java.util.concurrent.ExecutionEx ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...