2016-11-17 10 views
1

私は背景イメージが必要な単純なレイアウトを持っています。最初にxmlを通して背景画像を入れようとしましたが、非常に遅れがありました。背景とフレームのスキップを引き起こす背景イメージ

だから私はSet image through AsyncTaskに続きましたが、運がまだありません。

画像は1131×1800の解像度で、サイズは369kbです。

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

Javaファイル

public class MainActivity extends flights{ 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    new LoadDrawable().execute(); 
} 

private class LoadDrawable extends AsyncTask<Drawable, Void, Drawable> { 
    @Override 
    protected Drawable doInBackground(Drawable... params) { 
     //Loading the drawable in the background 
     final Drawable image = getResources().getDrawable(R.drawable.as); 
     //After the drawable is loaded, onPostExecute is called 
     return image; 
    } 

    @Override 
    protected void onPostExecute(Drawable loaded) { 
     //Hide the progress bar 
     //ProgressBar progress = (ProgressBar) findViewById(R.id.progress_bar); 
     //progress.setVisibility(View.GONE); 
     //Set the layout background with your loaded drawable 
     RelativeLayout layout = (RelativeLayout) findViewById(R.id.root); 
     layout.setBackgroundDrawable(loaded); 
    } 

    @Override 
    protected void onPreExecute() {} 

    @Override 
    protected void onProgressUpdate(Void... values) {} 
} 

} 

答えて

1

あなたの画像サイズが大きいです。背景に設定された画像の問題は、画像のサイズであり、解像度に合わない。私は同じ問題を抱えていた。検索と多くのメソッドをテストした後、私はグライドが画像を設定するための良いライブラリであることがわかった。このライブラリでは、デバイスのサイズが重要ではありません。滑空がそれを設定します。

あなたの描画可能から設定された画像のために、あなたは次のようにコーディングすることができます。

Glide.with(this).load(R.drawable.backtwo).asBitmap().into(new SimpleTarget<Bitmap>(size.x, size.y) { 
      @Override 
      public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) { 
       Drawable drawable = new BitmapDrawable(resource); 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
        iv_background.setBackground(drawable); 
       } 
      } 
     }); 

別のアプローチは、異なるデバイスの画面に異なる画像解像度を使用しています。このアプローチの弱点はapkサイズを増加させています

0

onPostExecute()はメインスレッドで発生するため、表示されているものが発生する可能性があります(ただし、ここでは回避策はありません)。

あなたの問題については、イメージはそれほど大きくはないので、このような問題が発生しないように見えます。ただし、小さな画像に置き換えると何が起こるかをテストする価値があるかもしれません(BTWでは、解像度が均等な画像を持つ方が良い - ここでは1131が悪い番号です)。

別のオプションは、非同期タスクの内側(時間がかかり)ビットマップをデコードすることです -

private class LoadDrawable extends AsyncTask<Drawable, Void, Drawable> { 
    @Override 
    protected Drawable doInBackground(Drawable... params) { 
     return new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.as)); 
    } 
    ... 
} 
+0

私はサイズ12.14キロバイトの1125 X 1442解像度の画像で私の方法を試してみました。そして、同じ遅れとスキッピングのフレームがあります。 P.S 私はクリック時に非常に小さい画像を回転させる。アニメート機能を使用しています。私は背景として色を選択すると、ラグの問題はありませんが、私はバックグラウンドとしてイメージを持っているので、ラグがあります。 – Swarnveer

+0

BitmapDrawableでも参照しましたが、同じ問題があります。 – Swarnveer

+0

アニメーションなしで試しましたか? –

関連する問題