PS:在開發中我們會遇到一些圖片處理問題,比如說緩存圖片了、限制圖片大小了、查看圖片了等。上一篇文章介紹了圖片的全景效果查看,今天介紹一個圖片縮放,我們如果有時間的話,可以自己寫一個屬于自己的庫,里面會用到view的按壓、事件分發、手勢等一些知識,如果沒有時間或者不會其他的方法,不妨來看看這個PhotoView。這是一個圖片縮放庫,對于這樣的還有GitView等,下面我就介紹一些用法。
功能:
- 正常加載圖片
- 雙擊放大
- 手勢隨意縮放
- 隨意拖動查看圖片每一個角落
- 結合其他設置可實現翻轉
效果圖
1:本地圖片加載
1
2
3
4
5
6
7
8
9
10
11
|
<ImageView android:layout_width= "match_parent" android:layout_height= "300dp" android:id= "@+id/id_loc" android:scaleType= "fitXY" /> <uk.co.senab.photoview.PhotoView android:layout_width= "match_parent" android:layout_height= "400dp" android:src= "@mipmap/ic_launcher" android:id= "@+id/id_myimg" /> |
第一種方法:
1
2
3
4
5
6
7
|
//本地加載方法一 // 設置圖片 Drawable bitmap = getResources().getDrawable(R.mipmap.ic_launcher); loc.setImageDrawable(bitmap); // 連接在photoview中 PhotoViewAttacher mAttacher = new PhotoViewAttacher(loc); mAttacher.update(); //更新 |
第二種
1
2
3
4
5
6
|
//本地方法加載二 PhotoViewAttacher mAttacher; mAttacher = new PhotoViewAttacher(loc); iv.setImageBitmap(bitmap); Glide.with( this ).load(R.mipmap.ic_launcher).asBitmap().into(loc); mAttacher.update(); |
2:網絡圖片加載
對于網絡也是可以用ImageView和PhotoView兩種
把ImageView或者PhotoView的對象名直接添加到display中就OK 了。
1
2
3
4
|
//加載網絡圖片 ImageLoader loader= ImageLoader.getInstance(); loader.init(ImageLoaderConfiguration.createDefault(ImageTest. this )); //loader初始化 loader.displayImage( "https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1529211252&di=1414331e22239ecb5730cbbd0f3793eb&src=http://pic154.nipic.com/file/20180114/26629113_090329120799_2.jpg" ,loc);//展示圖片 |
下面我們可以看一下源碼,其實他也是繼承了ImageView
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/** * Adds display image task to execution pool. Image will be set to ImageView when it's turn. <br/> * Default {@linkplain DisplayImageOptions display image options} from {@linkplain ImageLoaderConfiguration * configuration} will be used.<br /> * <b>NOTE:</b> {@link #init(ImageLoaderConfiguration)} method must be called before this method call * * @param uri Image URI (i.e. "http://site.com/image.png", "file:///mnt/sdcard/image.png") * @param imageView {@link ImageView} which should display image * @throws IllegalStateException if {@link #init(ImageLoaderConfiguration)} method wasn't called before * @throws IllegalArgumentException if passed <b>imageView</b> is null */ public void displayImage(String uri, ImageView imageView) { displayImage(uri, new ImageViewAware(imageView), null , null , null ); } |
總結
以上所述是小編給大家介紹的Android 圖片處理縮放功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.cnblogs.com/cmusketeer/archive/2018/06/17/9192687.html