Fresco加载图片

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_43668889/article/details/85252976

1.MainActivity

package com.example.frescodemo;

import android.Manifest;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
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.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.postprocessors.IterativeBoxBlurPostProcessor;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

public class MainActivity extends AppCompatActivity {

    private SimpleDraweeView simpleDraweeView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        simpleDraweeView = findViewById(R.id.image);
        SimpleDraweeView one = findViewById(R.id.one);
        SimpleDraweeView two= findViewById(R.id.two);
        SimpleDraweeView three= findViewById(R.id.three);
        SimpleDraweeView four = findViewById(R.id.four);
        if (ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
        }
        showUrlBlur(simpleDraweeView,
                "https://img4q.duitang.com/uploads/item/201304/27/20130427043538_wAfHC.jpeg",
                1,
                10);
        //加载gif
        DraweeController draweeController = Fresco.newDraweeControllerBuilder()
                //加载assests包下的gif
                .setUri(Uri.parse("asset:///aa.gif"))
                .setAutoPlayAnimations(true)
                .build();
        one.setController(draweeController);

        //one.setImageURI(Uri.parse("https://img4q.duitang.com/uploads/item/201304/27/20130427043538_wAfHC.jpeg"));
        //加载drawable包下的图片
        two.setImageURI("res:///"+R.drawable.f);
        three.setImageURI(Uri.parse("https://img4q.duitang.com/uploads/item/201304/27/20130427043538_wAfHC.jpeg"));
        four.setImageURI(Uri.parse("https://img4q.duitang.com/uploads/item/201304/27/20130427043538_wAfHC.jpeg"));

    }
//高斯模糊
    public static void showUrlBlur(SimpleDraweeView draweeView, String url, int iterations, int blurRadius) {
        try {
            Uri uri = Uri.parse(url);//图片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();
        }
    }
}

2.xml页面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/one"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:minHeight="50dp"
    android:layout_margin="10dp"
    app:fadeDuration="1000"/>
  <com.facebook.drawee.view.SimpleDraweeView
      android:id="@+id/two"
      app:roundAsCircle="true"
      android:layout_width="100dp"
      android:layout_height="wrap_content"
      android:adjustViewBounds="true"
      android:minHeight="50dp"
      android:layout_margin="10dp"
      app:fadeDuration="1000"/>
  <com.facebook.drawee.view.SimpleDraweeView
      android:layout_width="100dp"
      android:layout_height="wrap_content"
      android:id="@+id/image"
      android:minHeight="50dp"
      android:adjustViewBounds="true"
      app:roundedCornerRadius="20dp"
      app:roundAsCircle="true"
      app:roundingBorderWidth="5dp"
      app:roundingBorderColor="#ff0"/>
  <com.facebook.drawee.view.SimpleDraweeView
      android:id="@+id/three"
      android:layout_width="100dp"
      android:layout_height="wrap_content"
      android:adjustViewBounds="true"
      android:minHeight="50dp"
      android:layout_margin="10dp"
      app:fadeDuration="1000"
      app:roundedCornerRadius="5dp"
      app:roundTopLeft="true"
      app:roundTopRight="true"
      app:roundBottomLeft="true"
      app:roundBottomRight="true"/>
  <com.facebook.drawee.view.SimpleDraweeView
      android:id="@+id/four"
      android:layout_width="100dp"
      android:layout_height="wrap_content"
      android:adjustViewBounds="true"
      android:minHeight="50dp"
      android:layout_margin="10dp"
      app:fadeDuration="1000"
      app:roundedCornerRadius="5dp"
      app:roundTopLeft="false"
      app:roundTopRight="false"
      app:roundBottomLeft="true"
      app:roundBottomRight="true"/>

</LinearLayout>
<!--fresco:actualImageScaleType="focusCrop"           //加载得到的图片的缩放类型-->
    <!--fresco:fadeDuration="1000"                          -->
    <!--fresco:failureImage="@drawable/imgbg"               //加载失败之后显示的图片-->
    <!--fresco:failureImageScaleType="centerInside"         //图片缩放类型-->
    <!--fresco:placeholderImage="@drawable/imgbg"           //占位图片(未加载之前显示的图片)-->
    <!--fresco:placeholderImageScaleType="fitCenter"    -->
    <!--fresco:progressBarAutoRotateInterval="1000"         //加载进度条图片旋转周期-->
    <!--fresco:progressBarImage="@drawable/progress_bar"    //加载进度条图片-->
    <!--fresco:progressBarImageScaleType="centerInside"-->
    <!--fresco:retryImage="@mipmap/ic_launcher"             //提示重新加载的图片资源-->
    <!--fresco:retryImageScaleType="centerCrop"-->
    <!--fresco:backgroundImage="@color/colorWhite"          //背景图片-->
    <!--fresco:roundAsCircle="false"                        //是否要将图片剪切成圆形-->
    <!--fresco:viewAspectRatio="1"                          //图片宽高比-->
    <!--fresco:overlayImage="@drawable/overlay"             //在图片上方覆盖一个图片资源-->
    <!--fresco:pressedStateOverlayImage="@color/colorBlack"-->
    <!--fresco:roundedCornerRadius="20dp"                   //圆角角度,-->
    <!--fresco:roundTopLeft="true"                          //设置哪个角需要变成圆角-->
    <!--fresco:roundTopRight="false"-->
    <!--fresco:roundBottomLeft="false"-->
    <!--fresco:roundBottomRight="true"-->
    <!--fresco:roundWithOverlayColor="@color/colorWhite"    //圆角部分填充色-->
    <!--fresco:roundingBorderWidth="2dp"                    //边框宽度-->
    <!--fresco:roundingBorderColor="@color/colorBlack"      //边框填充色-->

3.自定义一个类继承Application

package com.example.app;

import android.app.Application;
import android.os.Environment;

import com.facebook.cache.disk.DiskCacheConfig;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipelineConfig;

import java.io.File;

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        File file = new File(Environment.getExternalStorageDirectory()+File.separator+"frescocache");
        Fresco.initialize(this,ImagePipelineConfig.newBuilder(this).
                    setMainDiskCacheConfig(
                DiskCacheConfig.newBuilder(this)
                        .setBaseDirectoryPath(file)
                        .build()
        ).build());

    }
}

4.清单文件中添加权限

<uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

5.需导入的依赖

 compile 'com.facebook.fresco:fresco:1.10.0'
 implementation 'com.facebook.fresco:animated-gif:1.10.0'

效果图如下

展开阅读全文

没有更多推荐了,返回首页