主 package com.example.fresco1; import android.graphics.drawable.Animatable; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import com.facebook.drawee.backends.pipeline.Fresco; import com.facebook.drawee.controller.AbstractDraweeController; import com.facebook.drawee.generic.GenericDraweeHierarchy; import com.facebook.drawee.view.SimpleDraweeView; public class MainActivity extends AppCompatActivity { private boolean s = true; private SimpleDraweeView simpleDraweeView; public static final String mImageUrl = "http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg"; public static final String mGifUrl = "http://img.zcool.cn/community/0139505792e5fc0000018c1bbb7271.gif"; public static final String mjpegUrl = "http://og3u5glro.bkt.clouddn.com/%E6%B8%90%E8%BF%9B%E5%BC%8F%E5%9B%BE%E7%89%87.jpg"; //这是渐进式的图片 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); simpleDraweeView = findViewById(R.id.frc_im); /*GenericDraweeHierarchy draweeHierarchy = new GenericDraweeHierarchyBuilder(getResources()) .setFailureImage(R.drawable.icon_failure) .setFailureImageScaleType(ScalingUtils.ScaleType.CENTER_CROP) .build(); simpleDraweeView.setHierarchy(draweeHierarchy);*/ GenericDraweeHierarchy hierarchy = simpleDraweeView.getHierarchy(); hierarchy.setFailureImage(R.drawable.icon_failure); AbstractDraweeController controller = Fresco.newDraweeControllerBuilder() .setUri(Uri.parse(mGifUrl)) .setTapToRetryEnabled(true) .setOldController(simpleDraweeView.getController()) .setAutoPlayAnimations(s) .build(); simpleDraweeView.setController(controller); //simpleDraweeView.setImageURI(Uri.parse(mImageUrl)); simpleDraweeView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Animatable animatable = simpleDraweeView.getController().getAnimatable(); if (animatable!=null){ if (s){ if (animatable.isRunning()){ animatable.stop(); } }else{ animatable.start(); } s = !s; } } }); } }
布局
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/frc_im" android:layout_width="200dp" android:layout_height="200dp" android:layout_centerInParent="true" app:retryImage="@drawable/icon_retry" app:roundingBorderWidth="2dp" app:roundAsCircle="true" app:roundingBorderColor="@color/colorAccent" app:roundTopLeft="true" app:roundBottomRight="false" app:roundedCornerRadius="16dp" app:placeholderImage="@drawable/icon_placeholder" app:actualImageScaleType="centerCrop" app:placeholderImageScaleType="centerCrop" app:progressBarImage="@drawable/icon_progress_bar" app:progressBarImageScaleType="centerCrop" /> </RelativeLayout>
应用 package com.example.fresco1; import android.app.Application; import com.facebook.cache.disk.DiskCacheConfig; import com.facebook.drawee.backends.pipeline.Fresco; import com.facebook.imagepipeline.core.ImagePipelineConfig; /** * Created by 红鼻子小黑 on 2018/10/30. */ public class App extends Application { @Override public void onCreate() { super.onCreate(); //Fresco初始化DiskCacheConfig / ImagePipelineConfig DiskCacheConfig.Builder builder = DiskCacheConfig.newBuilder(this); //设置自定义缓存路径 builder.setBaseDirectoryPath(getCacheDir()); DiskCacheConfig diskCacheConfig = builder.build(); ImagePipelineConfig config = ImagePipelineConfig.newBuilder(this) .setMainDiskCacheConfig(diskCacheConfig) .build(); //初始化fresco 自定义imagepipeline属性 Fresco.initialize(this,config); } }
Frasco动画
AbstractDraweeController controller = Fresco.newDraweeControllerBuilder() .setUri(Uri.parse(URL)) .setTapToRetryEnabled(true) .setOldController(simDt.getController()) .setAutoPlayAnimations(true) .build(); simDt.setController(controller);
private String URL = "http://img.zcool.cn/community/01dfca56ab06446ac7256cb0977d00.gif";