Fresco使用PhotoDraweeView 實現圖片的手勢縮放
阿新 • • 發佈:2018-12-17
1.gradle引入Fresco
compile 'com.facebook.fresco:fresco:0.11.0+'
2.在application中初始化Fresco
Fresco.initialize(this, ImagePipelineConfigFactory.getImagePipelineConfig(this));
3.PhotoDraweeView的下載地址
https://github.com/ongakuer/PhotoDraweeView
4.PhotoDraweeView結構
5.新增PhotoDraweeView控制元件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:fresco="http://schemas.android.com/apk/res-auto" android:orientation="vertical"> <me.relex.photodraweeview.PhotoDraweeView android:id="@+id/pv_picture" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
6.實現
PipelineDraweeControllerBuilder controller = Fresco.newDraweeControllerBuilder(); controller.setUri( Uri.parse(photopath)); controller.setOldController(pv_picture.getController()); controller.setControllerListener(new BaseControllerListener<ImageInfo>() { @Override public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) { super.onFinalImageSet(id, imageInfo, animatable); if (imageInfo == null) { return; } pv_picture.update(imageInfo.getWidth(), imageInfo.getHeight()); } }); pv_picture.setController(controller.build());
7.在PhotoDraweeView下面的方法裡。可以使用Matrix物件修改圖片顯示屬性
@Override protected void onDraw( Canvas canvas) { int saveCount = canvas.save(); //這裡改顯示比例 canvas.concat(mAttacher.getDrawMatrix()); super.onDraw(canvas); canvas.restoreToCount(saveCount); }
8.上面第二點ImagePipelineConfigFactory,是關於Fresco的初始化配置的,程式碼如下:
import android.content.Context; import com.facebook.cache.disk.DiskCacheConfig; import com.facebook.common.internal.Supplier; import com.facebook.imagepipeline.cache.MemoryCacheParams; import com.facebook.imagepipeline.core.ImagePipelineConfig; /** * Creates ImagePipeline configuration for the sample app */ public class ImagePipelineConfigFactory { private static final String IMAGE_PIPELINE_CACHE_DIR = "imagepipeline_cache"; private static ImagePipelineConfig sImagePipelineConfig; /** * Creates config using android http stack as network backend. */ public static ImagePipelineConfig getImagePipelineConfig(Context context) { if (sImagePipelineConfig == null) { ImagePipelineConfig.Builder configBuilder = ImagePipelineConfig.newBuilder(context); configureCaches(configBuilder, context); sImagePipelineConfig = configBuilder.build(); } return sImagePipelineConfig; } /** * Configures disk and memory cache not to exceed common limits */ private static void configureCaches( ImagePipelineConfig.Builder configBuilder, Context context) { //記憶體快取配置 final MemoryCacheParams bitmapCacheParams = new MemoryCacheParams( ConfigConstants.MAX_MEMORY_CACHE_SIZE, // Max total size of elements in the cache ConfigConstants.MAX_CACHE, // Max entries in the cache ConfigConstants.MAX_MEMORY_CACHE_SIZE, // Max total size of elements in eviction queue ConfigConstants.MAX_CACHE, // Max length of eviction queue ConfigConstants.MAX_CACHE); // Max cache entry size configBuilder //記憶體圖片快取數量 .setBitmapMemoryCacheParamsSupplier( new Supplier<MemoryCacheParams>() { @Override public MemoryCacheParams get() { return bitmapCacheParams; } }) .setMainDiskCacheConfig( //磁碟快取設定 DiskCacheConfig.newBuilder() .setBaseDirectoryPath(context.getApplicationContext().getCacheDir()) //基本路徑名 .setBaseDirectoryName(IMAGE_PIPELINE_CACHE_DIR) //基本路徑名名稱 .setMaxCacheSize(ConfigConstants.MAX_DISK_CACHE_SIZE)//磁碟快取的最大大小 .build()); } }