python写android的App(kivy框架)的实践(3)

前文的乒乓球弄完了,但还是缺少了很多东西,不知道该怎么做一个想要的App,所以,A Simple Paint App这一篇继续讲起了概念。

文中问题:

‎在创建应用程序时,您必须问自己三个重要问题:‎
‎我的应用程序会处理哪些数据?‎
‎如何直观地表示这些数据?‎
‎用户如何与这些数据交互?‎

基本概念:
组件化,什么功能的实现都是一个一个组件组合起来的效果。

通篇讲的是简单的画板以及清除按钮的实现。

知识点:

  1. 通过with self.canvas来实现图形的绑定和渲染,后续对图像的更改也会影响canvas中的效果,如同例子中的Line
  2. 通过touch.udkivy.input.providers.mouse.MouseMotionEvent.ud来在不同的事件监听中实现共享实例或数据
  3. 关于颜色,暗色调会和黑色背景混淆导致看不清,可以通过设置Color使用hsv模式而不是RGB来使用亮色调的颜色
  4. Button.bind用于绑定回调函数,也是练习中,清空画板按钮的绑定方式
  5. add_widget用于给组件添加其它组件作为父子组件存在
  6. 如果需要其它更详细的调整,就去看API的文档

教程代码如下:
paint.py

#!/usr/bin/env python
# -*- encoding: utf-8 -*-

from kivy.app import App
from random import random
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Ellipse, Line


class MyPaintWidget(Widget):

    def on_touch_down(self, touch):
        # color = (random(), random(), random())
        color = (random(), 1., 1.)
        with self.canvas:
            # Color(*color)
            Color(*color, mode="hsv")   # 为了避免色调太暗和黑色背景混淆,可以通过设置使用hsv模式,来使用亮色调的颜色
            d = 30.
            Ellipse(pos=(touch.x -d / 2, touch.y -d / 2), size=(d, d))
            touch.ud["line"] = Line(points=(touch.x, touch.y))  # ud是一个可以用于存放自定义数据的一个字典,利于共享和维护,此处在创建完Line之后,可以直接在on_touch_move中访问Line
    
    def on_touch_move(self, touch):
        touch.ud["line"].points += [touch.x, touch.y]


class MyPaintApp(App):
    def build(self):
        parent = Widget()
        self.painter = MyPaintWidget()
        clearbtn = Button(text="Clear")
        clearbtn.bind(on_release=self.clear_canvas)
        parent.add_widget(self.painter)
        parent.add_widget(clearbtn)
        return parent
    
    def clear_canvas(self, obj):
        self.painter.canvas.clear()


if __name__ == '__main__':
    MyPaintApp().run()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用 Python安卓应用程序,你可以使用 Kivy 框架Kivy 是一个开源的 Python 应用程序开发框架,可以帮助你创建跨平台的移动应用程序,包括 Android、iOS 和 Windows。 以下是使用 Kivy 创建一个简单的安卓应用程序的步骤: 1. 安装 Kivy 使用 pip 命令安装 Kivy: ```bash pip install kivy ``` 2. 创建应用程序 创建一个名为 main.py 的 Python 文件,并在其中编应用程序的代码。例如,以下代码创建一个简单的应用程序,显示一个标签和一个按钮: ```python import kivy from kivy.app import App from kivy.uix.label import Label from kivy.uix.button import Button class MyApp(App): def build(self): label = Label(text="Hello, world!") button = Button(text="Click me!") return label, button if __name__ == "__main__": MyApp().run() ``` 3. 构建 APK 文件 使用 Buildozer 工具将 Python 代码打包成 APK 文件。Buildozer 是一个开源的 Python 工具,可帮助你构建 Android 应用程序。 首先安装 Buildozer: ```bash pip install buildozer ``` 然后在应用程序目录中创建一个名为 buildozer.spec 的文件,其中包含应用程序的配置信息。以下是一个示例文件: ```ini [app] # (str) Title of your application title = My Application # (str) Package name package.name = myapp # (str) Package domain (needed for android/ios packaging) package.domain = org.example.myapp # (str) Source code where the main.py live source.dir = . # (list) Source files to include (let empty to include all the files) source.include_exts = py,png,jpg,kv,atlas # (list) Application requirements # comma separated e.g. requirements = sqlite3,kivy requirements = kivy [buildozer] # (int) Log level (0 = error only, 1 = info, 2 = debug (with command line option -v)) log_level = 1 # (str) Path to build artifact storage, absolute or relative to spec file # build_dir = ./.buildozer # (str) Path to build output (i.e. .apk, .ipa) storage # bin_dir = ./bin # (str) Path to build output for debug builds (.apk/.ipa) # debug = ./bin # (str) Path to build output for release builds (.apk/.ipa) # release = ./bin # (list) List of build dependencies (boost, openssl, etc.) # depends = # (str) Android NDK version to use # android.ndk_version = 19b # (int) Android SDK version to use # android.sdk_version = 24 # (str) Python for android distribution to use # python-for-android.branch = master # (str) python-for-android git clone directory (if not specified, it will be automatically cloned from github) # python-for-android.source_dir = # (str) The Android arch to build for, choices: armeabi-v7a, arm64-v8a, x86, x86_64 # arch = armeabi-v7a # (str) Sequence of toolchain to use, chooses the first one that exists. # toolchain = gcc # (str) NDK directory (if empty, it will be automatically downloaded.) # android.ndk_path = # (str) Android SDK directory (if empty, it will be automatically downloaded.) # android.sdk_path = # (str) ANT directory (if empty, it will be automatically downloaded.) # android.ant_path = # (str) android api to use # android.api = 27 # (bool) Use --private data storage (True) or --dir public storage (False) # android.private_storage = True # (str) Android NDK directory (if empty, it will be automatically downloaded) # android.ndk_path = /home/user/android/android-ndk-r19c # (str) Android SDK directory (if empty, it will be automatically downloaded) # android.sdk_path = /home/user/android/android-sdk-24 # (str) Build platform for python-for-android (ios, android, manylinux2010) # p4a.build_platform = android # (str) Path to a custom AndroidManifest.xml # android.manifest = # (str) Path to a custom source.properties # android.source_properties = # (str) Path to a custom ant.properties # android.ant_properties = # (str) Path to a custom androidtool.cfg # android.androidtool_cfg = # (str) Path to the android command to use. # android.cmd = adb ``` 最后,使用 Buildozer 命令打包 APK 文件: ```bash buildozer android debug ``` 4. 运行应用程序 在 Android 设备上安装 APK 文件,并启动应用程序。你应该可以看到一个显示标签和按钮的屏幕。 以上就是使用 Python 创建安卓应用程序的基本步骤。你可以在 Kivy 文档中找到更多相关信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值