我的 MainActivity 在使用 android 旧版本(例如 oreo)时滞后,但在最新版本中运行良好,如何解决?

Posted

技术标签:

【中文标题】我的 MainActivity 在使用 android 旧版本(例如 oreo)时滞后,但在最新版本中运行良好,如何解决?【英文标题】:My MainActivity lags while using android older versions (oreo for example) but works smoothly in the latest version , how to fix it? 【发布时间】:2021-12-15 04:03:20 【问题描述】:

在使用 android 旧版本(例如 oreo)时,我的 MainActivity 滞后/滚动不顺畅,但在最新版本中运行顺畅,我只在主页上遇到这个问题,主要布局是协调器布局。

这是主要的:

公共类 MainActivity 扩展 AppCompatActivity

 // Menu options :
 public boolean onCreateOptionsMenu(Menu menu) 
     MenuInflater inflater = getMenuInflater();
     inflater.inflate(R.menu.mainmenu, menu);
     return true;
 

dataBase db =新数据库(this);

 Toolbar toolbar;
 CoordinatorLayout coordinatorLayout;
 boolean ischecked =false;

 LottieAnimationView lotti ;
 AppCompatImageView logo ;
 Animation animation ;

// 主要选项:

 AppCompatImageView imageView1 ;
 AppCompatImageView imageView2 ;
 AppCompatImageView imageView3 ;
 AppCompatImageView imageView4 ;


@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
protected void onCreate(Bundle savedInstanceState) 

    super.onCreate(savedInstanceState);

 //Tablet Screen Configuration : 

    int seze = getResources().getConfiguration().screenLayout;
    if ((seze & Configuration.SCREENLAYOUT_SIZE_XLARGE)== Configuration.SCREENLAYOUT_SIZE_XLARGE ) 

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE);
    
    
  //end of configuration

    setContentView(R.layout.activity_main);



   imageView1 = findViewById(R.id.imageView1);
   imageView2 = findViewById(R.id.imageView2);
   imageView3 = findViewById(R.id.imageView3);
   imageView4 = findViewById(R.id.imageView4);

// 数据库资料:

    SharedPreferences shrd2 = getSharedPreferences("forbidden",MODE_PRIVATE);
    int e=shrd2.getInt("d",0);


    if (e==0) 
        db.createDataBase();
        SharedPreferences shrd= getSharedPreferences("forbidden",MODE_PRIVATE);
        SharedPreferences.Editor editor=shrd.edit();
        editor.putInt("d",1);
        editor.apply();
    
 // end of DataBase Stuff



// Images and main layout Yoyo animation Effect :

    YoYo.with(Techniques.Bounce).duration(1100).delay(500).playOn(imageView1) ;
    YoYo.with(Techniques.Bounce).duration(1100).delay(1100).playOn(imageView2) ;
    YoYo.with(Techniques.Bounce).duration(1100).delay(2100).playOn(imageView3) ;
    YoYo.with(Techniques.Bounce).duration(1100).delay(3100).playOn(imageView4) 


 // Logo amimation :

    logo = findViewById(R.id.logo) ;
    animation = AnimationUtils.loadAnimation(this,R.anim.floating);
    logo.startAnimation(animation) ;



 // CoordiantorLayout animated background  :

    coordinatorLayout = findViewById(R.id.CoordinatorLayout);
    AnimationDrawable animationDrawable = (AnimationDrawable) coordinatorLayout.getBackground();
    animationDrawable.setEnterFadeDuration(2500);
    animationDrawable.setExitFadeDuration(5000);
    animationDrawable.start();


 // Toolbar Menu :

    toolbar = (androidx.appcompat.widget.Toolbar) findViewById(R.id.MaToolBar);
    toolbar.inflateMenu(R.menu.mainmenu);

// 浮动按钮爆炸过渡:

     final boolean[]r=new boolean[1] ;
     r[0]=false;
     
     final FloatingActionButton button = findViewById(R.id.fab);
    
    final int[] x =new int[1];
    final int[] y=new int[1];
    button.post(() -> 
        // Values should no longer be 0
        int[] point = new int[2];
        button.getLocationInWindow(point); // or getLocationInWindow(point)
         x[0] = point[0];
         y[0] = point[1];
        r[0]=true;
        

    final int[] xx=new int[1];
    final int[] yy=new int[1];

        xx[0] = x[0];
        yy[0] = y[0];


        
        button.setOnClickListener(new View.OnClickListener() 

            @Override
            public void onClick(View v) 
                if (r[0]==true) 
                    // startActivity(new Intent(MainActivity.this, BookMark_Activity.class));
                    Intent getPosition = new Intent(MainActivity.this, BookMark_Activity.class);
                    getPosition.putExtra("x", xx[0]);
                    getPosition.putExtra("y", yy[0]);
                    getPosition.putExtra("ShouldRun",true);
                    startActivity(getPosition);
                
            

        );
);
// end of Floating button explosion transition 




  // Lottie Animation bottom :

    LottieAnimationView lottieAnimationView= findViewById(R.id.lottiebottom);
    lottieAnimationView.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            if (ischecked)
                lottieAnimationView.setSpeed(-1);
                lottieAnimationView.playAnimation();
                ischecked=false;
            else 
                lottieAnimationView.setSpeed(1);
                lottieAnimationView.playAnimation();
                ischecked=true;
            
        
    );




 // end of OnCreate method 

//浮动按钮动画的东西

 private int getDips(int dps) 
     Resources resources = getResources();
     return (int) TypedValue.applyDimension(
             TypedValue.COMPLEX_UNIT_DIP,
             dps,
             resources.getDisplayMetrics());
 

//意图的图像选项:

public void options (View view)


        if(view.getTag().toString().equals("Is101")) 
            Intent intent = new Intent(this,welcome.class) ;
            startActivity(intent);
            finish();
            overridePendingTransition(R.anim.slide_in_right , R.anim.slide_out_left);

        else if (view.getTag().toString().equals("Ar")) 

            Intent intent = new Intent(this,welcome_ar.class) ;
            startActivity(intent);
            finish();
            overridePendingTransition(R.anim.slide_in_right , R.anim.slide_out_left);
        else if (view.getTag().toString().equals("HRLC")) 

            Intent intent = new Intent(this,welcome_hrlc.class) ;
            startActivity(intent);
            finish();
            overridePendingTransition(R.anim.slide_in_right , R.anim.slide_out_left);

        else if (view.getTag().toString().equals("HIST")) 

            Intent intent = new Intent(this,welcome_hist.class) ;
            startActivity(intent);
            finish();
            overridePendingTransition(R.anim.slide_in_right , R.anim.slide_out_left);
        





 public void onBackPressed() 

     // Dialog box :

     ViewDialogMain alert2 = new ViewDialogMain();
     alert2.showDialog(MainActivity.this, "Bahrain Exams");



 

// 类结束

这是 XML:

<?xml version="1.0" encoding="utf-8"?>

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/CoordinatorLayout"
layout_
android:layout_
android:layout_
android:background="@drawable/gradient_list">

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_
    android:layout_
    android:layout_marginEnd="32dp"
    android:src="@drawable/bookmarkpngg"

    app:backgroundTint="#AFAEAE"
    app:layout_anchor="@id/appbarLayout"
    app:layout_anchorGravity="bottom|right|end" />


<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbarLayout"
    android:layout_
    android:layout_
    android:background="@drawable/transpa_colorerd"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:layout_
        android:layout_
        android:fitsSystemWindows="true"
        app:contentScrim="@color/app_color"
        app:layout_scrollFlags="scroll|snap|exitUntilCollapsed"
        app:title="Bahrain Exams"
        app:titleEnabled="true"

        >


        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_
            android:layout_>

            <com.airbnb.lottie.LottieAnimationView
                android:id="@+id/lottiesecond"
                android:layout_
                android:layout_
                android:layout_marginTop="40dp"
                android:layout_marginEnd="35dp"
                android:layout_marginBottom="13dp"
                android:rotation="-7.1"
                android:scaleType="fitEnd"
                android:scaleX="1.3"
                android:scaleY="1.3"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.796"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="1.0"
                app:lottie_autoPlay="true"
                app:lottie_loop="true"
                app:lottie_rawRes="@raw/flyingman" />

            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/logo"
                android:layout_
                android:layout_
                android:layout_marginStart="25dp"
                android:layout_marginEnd="249dp"

                android:layout_marginBottom="40dp"
                android:src="@drawable/officiallogosecond"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/MaToolBar"
            android:layout_
            android:layout_
            android:theme="@style/ThemeOverlay.AppCompat.Dark"
            app:layout_collapseMode="pin" />


    </com.google.android.material.appbar.CollapsingToolbarLayout>


</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
    android:id="@+id/scroll2"
    android:layout_
    android:layout_
    android:background="@drawable/transpa_scroll"
    android:scrollbarStyle="outsideOverlay"
    android:scrollbars="none"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/alconstreain2"
        android:layout_
        android:layout_
        app:layout_behavior="@string/appbar_scrolling_view_behavior">


        <androidx.appcompat.widget.LinearLayoutCompat
            android:id="@+id/linearLayout3"
            android:layout_
            android:layout_
            android:layout_margin="16dp"
            android:fitsSystemWindows="true"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">


            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/imageView1"
                android:layout_
                android:layout_
                android:layout_marginTop="10dp"

                android:layout_marginBottom="5dp"
                android:background="@drawable/is"
                android:foreground="@drawable/is"
                android:onClick="options"
                android:scaleType="fitXY"
                android:src="@drawable/uob"
                android:tag="Is101" />

            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/imageView2"
                android:layout_
                android:layout_
                android:layout_marginBottom="5dp"
                android:foreground="@drawable/Ar"
                android:onClick="options"

                android:scaleType="fitXY"
                android:src="@drawable/eglish"
                android:tag="Ar" />

            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/imageView3"
                android:layout_
                android:layout_
                android:layout_marginBottom="5dp"
                android:foreground="@drawable/hrlc"
                android:onClick="options"

                android:scaleType="fitXY"
                android:src="@drawable/maths"
                android:tag="HRLC" />


            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/imageView4"
                android:layout_
                android:layout_
                android:layout_marginBottom="5dp"
                android:foreground="@drawable/hist"
                android:onClick="options"

                android:scaleType="fitXY"
                android:src="@drawable/hist"
                android:tag="HIST" />


        </androidx.appcompat.widget.LinearLayoutCompat>

        <com.airbnb.lottie.LottieAnimationView
            android:id="@+id/lottiebottom"
            android:layout_
            android:layout_
            android:layout_marginStart="20dp"
            android:padding="0dp"
            android:scaleType="fitStart"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout3"
            app:lottie_autoPlay="true"
            app:lottie_loop="true"
            app:lottie_rawRes="@raw/taskman" />

    </androidx.constraintlayout.widget.ConstraintLayout>


</androidx.core.widget.NestedScrollView>

【问题讨论】:

请提供足够的代码,以便其他人更好地理解或重现问题。 【参考方案1】:

这很可能是兼容性问题。 确保您的所有项目结构都利用了 Android Jetpack 架构。

例如: 使用AppCompatButton 而不是 Button 使用LinearLayoutCompat 而不是LinearLayout ...等等。

【讨论】:

我试过了,不幸的是什么都没发生:( 如果你能发布你的``` MainActivity```和相应的.XML布局代码,那会很有帮助 刚刚发布了它们

以上是关于我的 MainActivity 在使用 android 旧版本(例如 oreo)时滞后,但在最新版本中运行良好,如何解决?的主要内容,如果未能解决你的问题,请参考以下文章

我的 Android 小部件被杀死,“不再需要 bellander.andro ....”

如何从Fragment到DataBinding访问Activity视图?

我的 MainActivity 在使用 android 旧版本(例如 oreo)时滞后,但在最新版本中运行良好,如何解决?

android笔记

如果使用导航控制器,如何删除某些片段中的底部导航视图和工具栏?

在android中每隔几分钟我应该用啥来读取json?