java - 编舞问题

标签 java android

我的应用程序出现问题。它给了我警告:我/编舞:跳过了 43 帧!应用程序可能在其主线程上做了太多工作。

我的应用程序有 2 个非常简单的 Activity ,事实上其中一个 Activity 我不编写其代码。其中 1 个是带有进度条的启动屏幕,另一个仅显示一些元素。

SplashScreen.java

   public class SplashScreen extends AppCompatActivity {

    //Variables declaration
    private ProgressBar progressBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);

        //Fill in variables
        TextView textView = findViewById(R.id.text_splash);
        progressBar = findViewById(R.id.splash_screen_progress_bar);

        //Change the textView font
        Typeface face = Typeface.createFromAsset(getAssets(),"fonts/ExpletusSans-BoldItalic.ttf");
        textView.setTypeface(face);

        //Call to the background execution
        new UpdateProgressBar().execute();
    }

    /* Auxiliary class to operate in background:
    * First parameter: in data type of function doInBackground()
    * Second parameter: data type of function onProgressUpdate()
    * Third parameter: out data type of function doInBackground()
    * */
    public class UpdateProgressBar extends AsyncTask<Void, Integer, Void> {

        //Execution in background (in a different thread)
        @Override
        protected Void doInBackground(Void... params) {
            //I want to show splashScreen for 4 seconds
            for(int i = 1; i <= 4 ; i++){
                oneSecond();
                publishProgress(i*25);
                if(isCancelled()){
                    break;
                }
            }
            return null;
        }

        //It executes in the main thread, when I call publishProgress() in doInBackground().
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            progressBar.setProgress(values[0]);
        }

        //Execute after doInBackground() in the main thread.
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            Intent change_screen = new Intent(SplashScreen.this, MeasureScreenActivity.class);
            startActivity(change_screen);
            //Effect of transition
            overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
            finish();
        }
    }

    //Auxiliary method to simulate one second
    public void oneSecond(){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

splash_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/app_background">

    <TextView
        android:id="@+id/text_splash"
        android:layout_width="match_parent"
        android:layout_height="60sp"
        android:layout_alignParentStart="true"
        android:gravity="center"
        android:layout_marginTop="35dp"
        android:text="Breath Frequecy"
        android:textColor="#fff"
        android:textSize="30sp" />

    <ImageView
        android:id="@+id/splash_screen_image"
        android:layout_width="match_parent"
        android:layout_height="300sp"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:src="@drawable/img_loadpage" />

    <ProgressBar
        android:id="@+id/splash_screen_progress_bar"
        android:layout_centerHorizontal="true"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_width="350dp"
        android:layout_height="25dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="50dp"
        android:progressDrawable="@drawable/custom_progressbar_horizontal"/>

</RelativeLayout>

MeasureScreenActivity.java

public class MeasureScreenActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.measure_screen);
    }
}

measure_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.appsgarci.prueba.MeasureScreenActivity"
    android:orientation="vertical"
    android:background="@drawable/app_background">

    <TextView
        android:id="@+id/text_measure"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:paddingEnd="10dp"
        android:paddingStart="10dp"
        android:text="Elija la actividad que ha realizado:"
        android:textAlignment="center"
        android:textColor="#fff"
        android:textSize="25sp" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="350dp"
        android:layout_height="50dp"
        android:layout_alignStart="@+id/spinner_measure"
        android:layout_alignBottom="@+id/spinner_measure"
        android:background="@drawable/stroke_spinner_selected"
        android:id="@+id/linearLayout"
        android:visibility="visible"/>

    <Spinner
        android:id="@+id/spinner_measure"
        android:layout_width="350dp"
        android:layout_height="50dp"
        android:layout_below="@+id/text_measure"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp" />

    <Button
        android:id="@+id/button_measure"
        android:layout_width="140sp"
        android:layout_height="140sp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"
        android:layout_marginBottom="50dp"
        android:background="@drawable/action_button_shape"
        android:text="Iniciar"
        android:textStyle="bold"
        android:textSize="20dp"
        android:visibility="visible"/>

 </RelativeLayout>

只有这段代码我已经遇到了Choreographer的问题,我不知道为什么,因为它只有一个操作,那就是升级进度条。

我的可绘制文件夹有这个:

  • action_button_shape.xml (v24)
  • app_background.webp
  • boton_measurement.webp (v24)
  • boton_measurement_pressed.webp(v24)
  • custom_progressbar.xml (v24)
  • custom_progressbar_horizo​​ntal.xml
  • img_loader.webp

我不知道drawable和drawable-24之间的区别。

我做了几次测试:

  • 在 android studio 模拟器中完美运行(nexus_5x API 24)
  • 在我的手机中,无论​​有无drawable-24元素(xiaomi mi max API 24),都会出现编排问题
  • 在另一部手机上我无法使用drawable-24,但改为drawable,它工作正常(xiaomi redmi 3s API 23)

我的手机可能发生什么情况?

最佳答案

正如这里提到的The application may be doing too much work on its main thread 。您还应该检查可绘制对象的大小。

暂时尝试减小图像大小 (@drawable/app_background @drawable/img_loadpage) 或将其移动到 drawable-xxxhdpi

关于java - 编舞问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47310633/

相关文章:

android - Android 中的处理程序行为

java - : <<?下面的标志是什么

java - 在Redis中更新哈希的单个字段

Java:异常处理

Android 打开通讯录应用(不是选择联系人)

java - 打开文件时出现异常

java - 读取 BufferedReader

java - 如何在Eclipse中使用 "Infer Generic Type Arguments..."

java - 安卓 keystore

android - Cordova Splashscreen 插件 - Android 上的空白屏幕而不是启动图像