QPython实例03-制作【ONE一个】可视化应用

一、前言

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 同步发布,欢迎关注

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,682评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,277评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,083评论 0 355
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,763评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,785评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,624评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,358评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,261评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,722评论 1 315
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,900评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,030评论 1 350
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,737评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,360评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,941评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,057评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,237评论 3 371
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,976评论 2 355

推荐阅读更多精彩内容