通过Glide加载背景图像时的OOM

时间:2018-03-25 13:41:12

标签: android drawable android-glide

我正在从drawable加载Glide图片,少数用户(1%)与OOM崩溃 Zodiac活动中的代码

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

 (ZodiacActivity:34)       loadBackground(findViewById(R.id.relativeLayout), getResources().getDrawable(R.drawable.zodiac_act_zodiac_activity_bg));

设置背景的活动中的方法

private void loadBackground(final View view ,Drawable drawable){
        if (getApplication()==null ){
            return;
        }
        DisplayMetrics dpm = ((App)getApplication()).getDisplayMetics(this);
        RequestOptions requestOptions = new RequestOptions()
                .override(dpm.widthPixels,dpm.heightPixels);
        Glide.with(this)

                .load(drawable)

                .apply(requestOptions)
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        view.setBackground(resource);
                    }
                });
    }
用于显示尺寸的

方法

private DisplayMetrics dpm;
public  DisplayMetrics getDisplayMetics(Activity activity){
    if (dpm==null){
        dpm= new DisplayMetrics();
        activity.getWindowManager()
                .getDefaultDisplay()
                .getMetrics(dpm);
    }
    return dpm;
}

有没有办法优化加载过程?

0 个答案:

没有答案