我有一个简单的布局,我想要一个背景图像。首先,我尝试通过 xml 放置背景图像,但延迟很大。
所以我关注了Set image through AsyncTask但仍然没有运气。
图片分辨率为 1131 X 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) {}
}
}
最佳答案
你的图片很大。将图像设置为背景的一个问题是图像大小而不适合分辨率。我有同样的问题。经过搜索和测试多种方法,我发现 glide 是一个很好的设置图像的库。使用这个库,你的设备大小并不重要。滑行将设置它。
对于从你的drawable中设置图像,你可以这样编码:
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 的大小
关于android - 导致延迟和跳帧的背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40659693/