首页 > 解决方案 > KivyMD UI 和动画在 Android 上运行缓慢且滞后

问题描述

我正在与KivyMD 合作。用户界面太慢了。动画很慢而且经常卡住。即使在厨房水槽示例中。虽然在我的 Windows 和 Linux 上都很好。问题是在为 android 构建包之后。

这正是我的问题和开发人员给出的答案:

https://github.com/HeaTTheatR/KivyMD/issues/77

不幸的是,它没有帮助,用户界面很慢

主要.py:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen,ScreenManager
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
from kivy.properties import StringProperty

from kivymd.theming import ThemeManager
from kivymd.utils.cropimage import crop_image
from kivymd.tabs import MDTabs,MDTabsBase
from kivymd.button import MDTextButton

class Scr_Mng(ScreenManager):
    pass

################################ 

class MainPage(Screen):

    def __init__(self, **kwargs):
        super(MainPage, self).__init__(**kwargs)
        Window_width, window_height = Window.size
        background_size = (int(Window_width),int(window_height))
        #crop_image(background_size , "bg.jpg" , "bg_croped.jpg"  )


##############################
class Ask_tab(MDTabsBase):
    profile_pic = StringProperty('assets/bg.jpg')
    profile_name = StringProperty('Sina')
    def new_question(self,question):
        print(question)
##############################
class Answer_tab(MDTabsBase):
    def load_questions(self):
        for x in range(20):
            self.ids.question_items_box.add_widget( Question_items() )

##############################

class Question_items(BoxLayout):
    pass

##############################
class Me_tab(MDTabsBase):
    pass
##############################

class Myapp(App):
    theme_cls = ThemeManager()
    theme_cls.primary_palette = 'DeepPurple'
    title = "Navigation Drawer"
    theme_cls.theme_style = "Light"
    mainpage_title = "Ask/Answer"

    def build(self):
        return Scr_Mng()


Myapp().run()

.kv 文件:

#:import MDToolbar kivymd.toolbar.MDToolbar
#:import MDTabs kivymd.tabs.MDTabs
#:import MDLabel kivymd.label.MDLabel
#:import MDTextField kivymd.textfields.MDTextField
#:import MDTextFieldRound kivymd.textfields.MDTextFieldRound
#:import MDRoundFlatButton kivymd.button.MDRoundFlatButton

#:import MDCard kivymd.cards.MDCard  

<Scr_Mng>:
    MainPage:

<Ask_tab>:
    text: 'Ask'
    FloatLayout:
        size: root.width, root.height
        ScrollView:
            BoxLayout:
                orientation: 'vertical'
                size_hint_y: None
                height: self.minimum_height
                padding: dp(48)
                spacing: dp(15)

                BoxLayout:
                    size_hint: None,None
                    size: root.width*0.2,root.width*0.2
                    pos_hint: {'center_x': .5}
                    canvas.before:
                        Color:
                            rgba: 1,1,1,1
                        Ellipse:
                            pos: self.pos
                            size: self.size
                            source: 'assets/unknown.jpg'

                MDLabel:
                    pos_hint: {'center_x': .5 }
                    size_hint: 1, None
                    height: root.width*0.1
                    text: root.profile_name
                    text_size: self.size
                    halign: 'center'
                    font_style: 'H6'   

                MDTextField:
                    id: question_field
                    pos_hint: {'center_x': .5 }
                    size_hint_x: 0.8
                    multiline: True
                    hint_text: "What question is in your mind?"
                    helper_text: ""
                    helper_text_mode: "persistent"
                    max_text_length: 1000
                MDRoundFlatButton:
                    text: "Post"
                    icon: "login"
                    pos_hint: {'center_x': .5}
                    width: 170
                    on_release: root.new_question(question_field.text)


<Answer_tab>:
    text: 'Answer'
    on_kv_post: root.load_questions()
    FloatLayout:
        size: root.width, root.height
        ScrollView:
            BoxLayout:
                id: question_items_box
                orientation: 'vertical'
                size_hint_y: None
                height: self.minimum_height
                padding: dp(15)
                spacing: dp(15)

<Question_items>:
    orientation: 'vertical'
    spacing: dp(15)
    size_hint: 1,None
    height: 300
    pos_hint: {'center_x': .5}
    BoxLayout:
        canvas.before:
            Color:
                rgba: 0.3,0.5,0.2,1
            Rectangle:
                pos: self.pos
                size: self.size
    MDSeparator:


<Me_tab>:
    text: 'Me'

<MainPage>:
    name: 'main'
    # on_kv_post: root.load_main_tabs() 
    BoxLayout:
        orientation: 'vertical'
        MDToolbar:
            id: toolbar
            title: app.mainpage_title
            md_bg_color: app.theme_cls.primary_color
            background_palette: 'Primary'
            background_hue: '500'
            elevation: 10
            right_action_items:
                [['dots-vertical', lambda x: None]]

        MDTabs:
            id: main_tabs
            Ask_tab:
            Answer_tab:
            Me_tab:

标签: pythonandroidkivykivy-language

解决方案


There are a few things you should do to solve the issue:

  1. Make sure the icon of the app is not too large, for example, if the icon is a 1200x1800 image (png, jpg, etc) that will make the apk to lag before you install it, during you install it, and when you attempt to start the app, even if the icon is 200x300 it may also cause lagging (this has to be setup in your "buildozer.spec" file), for example mine is:

    (str) Presplash of the application

    presplash.filename = /home/jbsidis/Escritorio/_a/Suministros/fs.png

    (str) Icon of the application

    icon.filename = /home/jbsidis/Escritorio/_a/Suministros/fs_icon.png

  2. Sometimes the current standard version of p4a, kivy and kivyMD are used, to make sure you have the latest but stable versions of everything, I recommend the following in the requirements of your spec file (doing this will generate a fluent app like this one https://youtu.be/WxsL1pdzpYc ):

    requirements = kivy==2.0.0,kivymd,python3,pyjnius,plyer,requests,urllib3,chardet,idna,pip,Image,PIL

So recommended version of kivy is version 2.0.0 and recommended kivyMD is the current stable version (not master branch because icons do not work in that one)

Everything should be solved.

Just do the command in the terminal to create the apk:

python3 -m buildozer -v android debug

Or:

buildozer -v android debug

推荐阅读