1. 程式人生 > >Android圖片處理--縮放

Android圖片處理--縮放

div 大小 cep public date() andro file .get loader

PS:在開發中我們會遇到一些圖片處理問題,比如說緩存圖片了、限制圖片大小了、查看圖片了等。上一篇文章介紹了圖片的全景效果查看,今天介紹一個圖片縮放,我們如果有時間的話,可以自己寫一個屬於自己的庫,裏面會用到view的按壓、事件分發、手勢等一些知識,如果沒有時間或者不會其他的方法,不妨來看看這個PhotoView。這是一個圖片縮放庫,對於這樣的還有GitView等,下面我就介紹一些用法。

功能:

  • 正常加載圖片
  • 雙擊放大
  • 手勢隨意縮放
  • 隨意拖動查看圖片每一個角落
  • 結合其他設置可實現翻轉

效果圖

技術分享圖片

1:本地圖片加載

<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"/>

第一種方法:

 //本地加載方法一
        // 設置圖片
        Drawable bitmap = getResources().getDrawable(R.mipmap.ic_launcher);
        loc.setImageDrawable(bitmap);

        // 連接在photoview中
        PhotoViewAttacher mAttacher = new PhotoViewAttacher(loc);
        mAttacher.update();//更新

第二種

//本地方法加載二
        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 了。

        //加載網絡圖片
        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

/**
	 * 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圖片處理--縮放