如何在使用 Chaquopy 时在 android 中显示加载动画

Posted

技术标签:

【中文标题】如何在使用 Chaquopy 时在 android 中显示加载动画【英文标题】:How to show loading animation in android while using Chaquopy 【发布时间】:2021-10-13 02:26:47 【问题描述】:

我在我的应用程序中使用 python,但是当我单击按钮时,我面临加载动画的问题,比如进度条不起作用,应用程序移动到 python 代码和 java 文件卡住并且当时没有响应显示正在加载 pyhon 代码的动画,谁能帮我找出代码在下面...

        if (!Python.isStarted()) 
            Python.start(new androidPlatform(documentupload.this));
        
                b2.setOnClickListener(new View.OnClickListener() 
                    @Override
                    public void onClick(View view) 
                        pgb.setVisibility(View.VISIBLE);
                        if(isConnected())
                        



                        String strUserName = et.getText().toString();
                        String numofdocword=docnum.getText().toString();

                       if(TextUtils.isEmpty(strUserName) && TextUtils.isEmpty(numofdocword))
                       
                            et.setError("Enter total number");
                            docnum.setError("Enter maximum number of words for document");
                            return;
                       
                       else if(Integer.parseInt(strUserName)>100)
                           Toast.makeText(documentupload.this,"Number must be less than 100",Toast.LENGTH_SHORT).show();
                       


                       else
                           if(upload.length()==0) 
                               Toast.makeText(documentupload.this, "Fill the required fields", Toast.LENGTH_SHORT).show();

                           
                           else if(adapter.getItemCount()==0)
                               Toast.makeText(documentupload.this, "Words must be present", Toast.LENGTH_SHORT).show();

                           
                           else
                               py = Python.getInstance();
                               final PyObject pyobj = py.getModule("count");
                               final PyObject pyspellcheck = py.getModule("grammer2");
                               final PyObject pywordsimilarity=py.getModule("forwordsimilarity");
                               final PyObject pysentencesimilarity = py.getModule("newsentencesimilarity");
                               final PyObject sencount=py.getModule("word2vec");
                               final PyObject pywordspell=py.getModule("spellcheck");



                              PyObject obj =  pyobj.callAttr("main", sf);
                               String count=obj.toString();

                               PyObject spellobjgrammer = pyspellcheck.callAttr("main", sf);
                               PyObject spellwordobj = pywordspell.callAttr("main", sf);

                               List<PyObject> wordsimilarityobj = py.getModule("forwordsimilarity").callAttr("main",sf,Words.toString()).asList();
                               String s=wordsimilarityobj.get(0).toString();
                               String res2 = wordsimilarityobj.get(1).toString();
                               String res3= wordsimilarityobj.get(2).toString();

                               List<PyObject> sentencesimilarityobj = (List<PyObject>) pysentencesimilarity.callAttr("main",sf,Definitions.toString()).asList();
                               String similarsent=sentencesimilarityobj.get(0).toString();
                               String docsentcount=sentencesimilarityobj.get(1).toString();

                               PyObject sentence_countobj = sencount.callAttr("main",Definitions.toString());
                               //Toast.makeText(documentupload.this, "uploaded" + sf, Toast.LENGTH_LONG).show();
                               //String pobj = obj.toString();
                               String grammermistake = spellobjgrammer.toString();
                               //String sentencesimilar = sentencesimilarityobj.toString();
                               String sentence_count=sentence_countobj.toString();
                               String wordspellingmistake= spellwordobj.toString();

                              // Toast.makeText(documentupload.this, res3, Toast.LENGTH_LONG).show();

                               Intent intent = new Intent(documentupload.this, result.class);
                               intent.putExtra("numberwords", count);
                               intent.putExtra("spellmistake", grammermistake);
                               intent.putExtra("defitionsimilarity", similarsent);
                               intent.putExtra("wordsimilarity",s);
                               intent.putExtra("stop",res2);
                               intent.putExtra("datacount",res3);
                               intent.putExtra("sentence_count",sentence_count);
                               intent.putExtra("totalnumber",et.getText().toString());
                               intent.putExtra("wordspelling",wordspellingmistake);
                               intent.putExtra("docsentcount",docsentcount);
                               intent.putExtra("filenumberofwords",docnum.getText().toString());
                               pgb.setVisibility(View.GONE);
                               startActivity(intent);
                               finish();

                           

                       


                        
                        else 
                            Toast.makeText(documentupload.this,"No internet connection",Toast.LENGTH_SHORT).show();
                        
                    


                );


【问题讨论】:

【参考方案1】:

进度条或任何其他动画只有在 UI 线程未阻塞时才会起作用。因此,如果您的代码需要很长时间才能运行,您将不得不在后台线程上运行代码并将 UI 更新发布到 UI 线程,如 this answer 中所述。

【讨论】:

以上是关于如何在使用 Chaquopy 时在 android 中显示加载动画的主要内容,如果未能解决你的问题,请参考以下文章

在 Android Studio 中使用 Chaquopy 将值插入到 python 文件中

Android Studio Chaquopy 吐司消息

使用 chaquopy 的 android studio 不支持 Python 模块

Chaquopy 如何导入模式库?

在 android studio 中使用 chaquopy 运行 python 脚本

使用 chaquopy 在 android studio 中集成 python 代码(对象检测代码)