2016-10-03 7 views
0

私の主な活動は主にボタンとして使われるイメージで構成されています。画像は適切なサイズに縮小されます。Percent Relative Layoutイメージ重いサムスンGS7ではMainActivityが遅れますが古代の電話ではありません

画像自体は、ドロウアブルフォルダ内の.pngファイルです。彼らは私が仮定しているものそれぞれ約120〜140キロバイトは、彼らの圧縮形式です。

これは、一度に画面上のあまりにも多くの画像と関連があると思っていましたが、私のサムスンGS7の画面のサイズの3分の1のデバイスにロードすると、遅れを最小限に抑えてlogcatはほとんどすべてのフレームレートをスキップします。

現在の理論では、おそらく高解像度の画面がリソースを増やしていると考えられますが、GS7のメモリは古いデバイスを矮小化するはずです。

そして、エミュレータはゴミのように走ります。どの物理デバイスよりも100倍遅いので、私はそれを気にしません。

唯一のことは、それぞれImageViewonClickListenerです。このページでは他の作業は行われていません。

XML

 <android.support.percent.PercentRelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:text="@string/abc" 
       android:textSize="27sp" 
       android:id="@+id/textMainTitle" 
       android:layout_gravity="center" 
       android:layout_alignParentTop="true" 
       android:textColor="#ffffff" 
       android:gravity="center_horizontal" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:text="def" 
       android:id="@+id/textMainSubTitle" 
       android:layout_gravity="center_horizontal" 
       android:layout_below="@id/textMainTitle" 
       android:gravity="center_horizontal" /> 


      <ImageView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:id="@+id/btn1" 
       android:src="@drawable/1" 
       app:layout_widthPercent="33%" 
       android:layout_below="@id/textMainSubTitle" 
       android:adjustViewBounds="true" 
      /> 

      <ImageView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:id="@+id/btn2" 
       android:src="@drawable/2" 
       app:layout_widthPercent="33%" 
       android:layout_below="@id/textMainSubTitle" 
       android:layout_toRightOf="@id/btn1" 
       android:adjustViewBounds="true" 
       /> 

      <ImageView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:id="@+id/btn3" 
       android:src="@drawable/3" 
       app:layout_widthPercent="33%" 
       android:layout_below="@id/textMainSubTitle" 
       android:layout_toRightOf="@id/btn2" 
       android:adjustViewBounds="true" 
       /> 

// Other buttons removed. But there are six rows each with 
// 3 button as there are above 

関連するJava

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     btn1 = (ImageView) findViewById(R.id.btn1); 
     btn1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       startActivity(new Intent(getApplicationContext(), activity1.class)); 
      } 
     }); 

     btn2 = (ImageView) findViewById(R.id.btn2); 
     btn2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       startActivity(new Intent(getApplicationContext(), activity2.class)); 
      } 
     }); 

     btn3 = (ImageView) findViewById(R.id.btn3); 
     btn3.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       startActivity(new Intent(getApplicationContext(), activity2.class)); 
      } 
     }); 

//As in the XML layout, all other buttons have been removed for visual 

Logcat

I/Choreographer: Skipped 31 frames! The application may be doing too much work on its main thread. 
I/Choreographer: Skipped 38 frames! The application may be doing too much work on its main thread. 

//and so on. 

アドバイスや援助をいただければ幸いです。ありがとう

+0

ご回答いただきありがとうございます。私は離れて引っ張られたが、私は戻ってupvote /コメント/マークasap – BR89

答えて

1

メインスレッドのすべての画像を読み込むのが悪いです。非同期にロードする必要があります。 GlideまたはPicassoのような画像読み込みライブラリを使用してください。それらは通常、リモートソースから画像をダウンロードして表示するために使用されますが、ローカルの非同期読み込みをすばやく処理することもできます。

btn1の非同期画像読み込みを行いましょう。

Glide.with(this) // any Context subclass, depending on lifecycle needs .load(R.drawable.1) .into(btn1)

は、すべてのImageViewsのためにこれを行うと、あなたは黄金だ:グライド(それだから私のお気に入り)を使用し、それは簡単なことではありません。 android:src = ... xml属性を削除することを忘れないでください。

エミュレータの低速性については、WindowsおよびVTをサポートするIntel CPUの場合は、SDKマネージャから「Intel x86エミュレータアクセラレータ(HAXMインストーラ)」をダウンロードしてインストールしてください。BIOSでVT-xが有効になっていることを確認してください。

+0

私は本当にそれを理解していないので、私はこれがどのように動作するかについてもっと読む必要があります。しかし、それは働いた。後の読者のためのドキュメント:Installed My Gradleに直接移動し、ProGuardを指定した設定で更新しました。すべてのボタンと遅れのためにこの答えに記載されているJavaコードに使用されました:)ありがとうトン!私はそれらを持っていた場合、私はあなたにもっとアップフォートを与えるだろう! – BR89

0

これは、メインスレッドに画像を読み込むのが悪い理由です。画面が遅れる(またはフレームをスキップする)。 120kbの画像サイズは、読み込んだ画像の数を考慮すると大きすぎます。

イメージをサーバーに保存し、非同期タスクを使用して取得します。

0
startActivity(new Intent(getApplicationContext(), activity1.class)); 

to 

startActivity(new Intent(ActivityName.this, activity1.class)); 

//他の2つのアクティビティと同じですか?

+0

こんにちは、好奇心として、なぜこれをお勧めですか? – BR89

関連する問題