Glide加載時等比例縮放圖片至屏幕寬度(通過加載下來的圖片動態設置)


首先我們需要吧圖片的控件修改為全屏尺寸

  <ImageView
            android:id="@+id/iv_thumb1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:src="@drawable/ic_tiktok_shu"
            android:visibility="visible" />

然后就是代碼設置寬高

Glide.with(activity).load(StrUrl).asBitmap().into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        int imageWidth = resource.getWidth();
                        int imageHeight = resource.getHeight();
                        int height = ScreenUtils.getScreenWidth() * imageHeight / imageWidth;
                        ViewGroup.LayoutParams para = imageView.getLayoutParams();
                        para.height = height;
                        para.width = ScreenUtils.getScreenWidth();//屏幕的寬度,沒有工具類自己從網上搜
                        imageView.setImageBitmap(resource);
                    }
                });

這里說下如果需要可以設置圖片裁剪的方式

.centerCrop()//設置圖片加載居中裁剪

原理:先用Glide按圖片原始大小加載一次圖片,再獲取加載的圖片寬度和高度及屏幕寬度,計算縮放后的高度再賦值給對應的imageview,最后再把加載得到的圖片設置到賦值后的imageview中以完成等比例縮放

 

by leileitua


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM