Fresco使用PhotoDraweeView 实现图片的手势缩放

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());
  }

}

猜你喜欢

转载自blog.csdn.net/sunshine_0707/article/details/84306650