Android 优化广告图加载

       Glide.with(context).asBitmap().load(url).into(new SimpleTarget() {
           @Override
           public void onResourceReady(@androidx.annotation.NonNull Bitmap resource, @Nullable Transition transition) {
               imageView.setImageBitmap(resource);
           }

           @Override
           public void onLoadFailed(Drawable errorDrawable) {
               super.onLoadFailed(errorDrawable);
           }
       });

作用是:下载完成后,把图片显示在你广告图上,由于glide有三级缓存机制,因此,最好是提前在接口返回的时候,进行预加载,然后进入到广告启动的流程中的时候就可以直接复用上一次的bitmap,不会浪费相应的时间。因此思路如下:

接口数据--->开始预加载广告图---》loadSplash---〉加载广告图并且无兜底图

等宽显示广告图:

    public static void scaleImage(final Activity activity, final ImageView view, Bitmap bitmap) {
        // 获取屏幕的高宽
        Point outSize = new Point();
        activity.getWindow().getWindowManager().getDefaultDisplay().getSize(outSize);

        // 解析将要被处理的图片
        final Bitmap resourceBitmap = bitmap;

        if (resourceBitmap == null) {
            return;
        }

        // 开始对图片进行拉伸或者缩放

        // 使用图片的缩放比例计算将要放大的图片的高度
        final int bitmapScaledHeight = Math.round(resourceBitmap.getHeight() * outSize.x * 1.0f / resourceBitmap.getWidth());

        // 以屏幕的宽度为基准,如果图片的宽度比屏幕宽,则等比缩小,如果窄,则放大
        final Bitmap scaledBitmap = Bitmap.createScaledBitmap(resourceBitmap, outSize.x, bitmapScaledHeight, false);
        //图宽度
        Log.e("MainActivity", "resourceBitmap.getHeight:" + resourceBitmap.getHeight());
        //图片高度
        Log.e("MainActivity", "resourceBitmap.getWidth:" + resourceBitmap.getWidth());
        //屏幕宽度
        Log.e("MainActivity", "outSize.x:" + outSize.x);

        //传入到下面的bitmap
        Log.e("MainActivity", "scaledBitmap.getHeight():" + bitmapScaledHeight);

        view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                try {
                    view.getViewTreeObserver().removeOnPreDrawListener(this);
                    //这里防止图像的重复创建,避免申请不必要的内存空间
                    if (scaledBitmap.isRecycled())
                        //必须返回true
                        return true;
                    // 当UI绘制完毕,我们对图片进行处理
                    int viewHeight = view.getMeasuredHeight();
                    //最终显示的图片
                    Bitmap finallyBitmap = null;
                    Log.e("MainActivity", "viewHeight:" + viewHeight);
                    //y + height must be <= scaledBitmap.getHeight()
                    if (scaledBitmap.getHeight() > viewHeight) {
                        int offset = (scaledBitmap.getHeight() - viewHeight) / 2;// 计算将要裁剪的图片的顶部以及底部的偏移量
                        int finallyBitmapHeight = scaledBitmap.getHeight() - (offset * 1); // 对图片以中心进行裁剪,裁剪出的图片就是非常适合做引导页的图片了
                        Log.e("MainActivity", "offset:" + offset);
                        Log.e("MainActivity", "finallyBitmapHeight:" + finallyBitmapHeight);
                        finallyBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), finallyBitmapHeight);
                    } else {
                        int finallyBitmapHeight = scaledBitmap.getHeight();
                        Log.e("MainActivity", "finallyBitmapHeight:" + finallyBitmapHeight);
                        finallyBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), finallyBitmapHeight);
                    }
                    if (null != finallyBitmap) {
                        if (!finallyBitmap.equals(scaledBitmap)) {//如果返回的不是原图,则对原图进行回收
                            scaledBitmap.recycle();
                            System.gc();
                        }
                    }
                    // 设置图片显示
                    view.setImageBitmap(finallyBitmap);
                } catch (Exception e) {
                    Log.e("MainActivity", "图片异常e:" + e.toString());
                }
                if (resourceBitmap != null && !resourceBitmap.isRecycled())
                    resourceBitmap.recycle();
                return true;
            }
        });
    }
@Override
public boolean onResourceReady(Drawable resource, Object model, Target target, 
    DataSource dataSource, boolean isFirstResource) {
    // 得到当前imageView的宽度(我设置的是屏幕宽度),获取到imageView与图片宽的比例,然后通过这个比例去设置imageView的高
    ViewGroup.LayoutParams params = mAd.getLayoutParams();
    int vw = mAd.getWidth();
    float scale = (float) vw / (float) resource.getIntrinsicWidth();
    int vh = Math.round(resource.getIntrinsicHeight() * scale);
    params.height = vh;
    mAd.setLayoutParams(params);
    return false;
}

转自:Android 启动白屏优化及公告闪图秒加载 - 简书

你可能感兴趣的:(android,首页广告图)