Fresco图片框架简单使用方法

导依赖

    //Fresco,无论使用哪个模块的功能,都必须要添加的基础依赖
    implementation 'com.facebook.fresco:fresco:0.14.1'
    // 在 API < 14 上的机器支持 WebP 时,需要添加
    implementation 'com.facebook.fresco:animated-base-support:0.14.1'
    // 支持 GIF 动图,需要添加
    implementation 'com.facebook.fresco:animated-gif:0.14.1'
    implementation 'com.facebook.fresco:webpsupport:0.14.1'
    // 支持 WebP 静态图及 WebP 动图,需要添加
    implementation 'com.facebook.fresco:animated-webp:0.14.1'
    //如果项目中使用了OkHttp需要进行替换(因为Fresco底层网络用的是okhttp,不做修改会出现jar包冲突)
    implementation 'com.facebook.fresco:imagepipeline-okhttp3:0.14.1'

在application中初始化Fresco(记得在清单文件里配置application)

   Fresco.initialize(this);

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    //自动查找 要换成fresco

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        />
    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/image2"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        fresco:roundAsCircle="true"
        //圆形图
        fresco:roundedCornerRadius="1dp"
        //圆角角度
        fresco:roundingBorderColor="#fff"
        //边框颜色
        fresco:roundingBorderWidth="2dp"
        //边框宽度
        />

</RelativeLayout>

main

public class MainActivity extends AppCompatActivity {

    private SimpleDraweeView draweeView1,draweeView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       String url="http://a.hiphotos.baidu.com/image/pic/item/55e736d12f2eb938d3de795ad0628535e4dd6fe2.jpg";
       Uri uri=Uri.parse(url);
        draweeView2=findViewById(R.id.image2);
        draweeView2.setImageURI(uri);
        draweeView1 = findViewById(R.id.image);
        showUrlBlur(draweeView1,url,2,2);

    }

    /**
     * @Params
     * draweeView 确定哪个控件需要做高斯模糊处理
     * url 图片的资源
     * iterations  
     * blurRadius 自己感觉是模糊程度  数字越大 模糊程度越大
     */
    public static void showUrlBlur(SimpleDraweeView draweeView, String url, int iterations, int blurRadius) {
        try {
            Uri uri = Uri.parse(url);
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                    .setPostprocessor(new IterativeBoxBlurPostProcessor(iterations, blurRadius))
                    .build();
            AbstractDraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setOldController(draweeView.getController())
                    .setImageRequest(request)
                    .build();
            draweeView.setController(controller);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/xieyu1999/article/details/85261143