Fresco简单的使用—SimpleDraweeView

  • 百学须先立志—学前须知:

    在我们平时加载图片(不管是下载还是加载本地图片…..)的时候,我们经常会遇到这样一个需求,那就是当图片正在加载时应该呈现正在加载时的图像,当图片加载失败时应该呈现图片加载时的图像,当我们重新加载这张图片时,应该呈现重试时图像,直到这张图片加载完成。这些繁琐并且重复的如果得不到简化的话,那将是一个开发人员的噩梦,现在好了,我们用 Facebook 出品的一个强大的图片加载组件 Fresco 几行代码就可以搞定以上问题了。

  • 尽信书,不如无书—能学到什么?

    1、SimpleDraweeView最基本的使用 
    2、SimpleDraweeView的圆形图 
    3、SimpleDraweeView的圆角图 
    4、SimpleDraweeView的缩放类型

  • 工欲善其事必先利其器—下载Fresco并导入到项目

    Fresco中文说明:http://www.fresco-cn.org/

    Fresco项目GitHub地址:https://github.com/facebook/fresco

    第一步进入 Fresco项目GitHub地址 :

    第二步添加Fresco到项目工程:

    第三步服务及权限:

    <!-- 访问网络的权限 -->
    <uses-permission android:name="android.permission.INTERNET"/>
    • 1
    • 2
    • 1
    • 2

  • 常见问题:

    初次使用,我们就先简单书写我们的 activity_main.xml :

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"> <com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/main_adv"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_centerInParent="true"
    ></com.facebook.drawee.view.SimpleDraweeView> </RelativeLayout>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    代码分析:

    MainActivity 实现代码:

    public class MainActivity extends AppCompatActivity {
    
        @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    简单的书写完布局和实现代码之后我们来运行一下。

            java.lang.RuntimeException: Unable to start activity ComponentInfo{scp.com.frescodemo/scp.com.frescodemo.MainActivity}: 
    
    android.view.InflateException: Binary XML file line #5: Error inflating class com.facebook.drawee.view.SimpleDraweeView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2264)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313)
    at android.app.ActivityThread.access$1100(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5336)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class com.facebook.drawee.view.SimpleDraweeView
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    运行报错了!怎么回事呢?这里啊,是因为我们没有在应用调用 setContentView() 之前进行初始化Fresco造成的;解决办法:

    修改我们的 MainActivity 里代码:

    public class MainActivity extends AppCompatActivity {
    
        @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Fresco.initialize(this);
    setContentView(R.layout.activity_main);
    }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    再运行就没有错误发生了。

  • 占位图—placeholderImage:

    在此之前我们需要一张占位图 icon_placeholder.png 大家右键另存为即可:

    修改我们的 activity_main.xml :

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"> <com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/main_adv"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_centerInParent="true"
    fresco:placeholderImage="@mipmap/icon_placeholder"
    fresco:placeholderImageScaleType="fitCenter"
    ></com.facebook.drawee.view.SimpleDraweeView> </RelativeLayout>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    代码说明:

    MainActivity 无需修改,运行一下:

  • 正在加载图—progressBarImage:

    在此之前我们需要一张正在加载图 icon_progress_bar.png 大家右键另存为即可:

    修改我们刚刚书写的 activity_main.xml :

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"> <com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/main_sdv"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_centerInParent="true"
    fresco:actualImageScaleType="focusCrop"
    fresco:placeholderImage="@mipmap/icon_placeholder"
    fresco:placeholderImageScaleType="fitCenter"
    fresco:progressBarImage="@mipmap/icon_progress_bar"
    fresco:progressBarImageScaleType="centerInside"
    fresco:progressBarAutoRotateInterval="5000"
    ></com.facebook.drawee.view.SimpleDraweeView> </RelativeLayout>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    代码说明:

    更改我们的 MainActivity 里代码:

    public class MainActivity extends AppCompatActivity {
    
        private SimpleDraweeView simpleDraweeView;
    
        @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Fresco.initialize(this);
    setContentView(R.layout.activity_main);
    initView();
    } private void initView() {
    //创建SimpleDraweeView对象
    simpleDraweeView = (SimpleDraweeView) findViewById(R.id.main_sdv);
    //创建将要下载的图片的URI
    Uri imageUri = Uri.parse("http://my.csdn.net/uploads/avatar/4/E/8/1_y1scp.jpg");
    //开始下载
    simpleDraweeView.setImageURI(imageUri);
    }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    代码分析:

    运行效果图:

    正在加载图本身是不可以转的,但是呢,加上了 fresco:progressBarAutoRotateInterval="5000" 属性,那么它就可以旋转了;可以看到,正在加载图一闪而过,这是因为我们加载的图片很小,网络也很好。

  • 失败图—failureImage:

    在此之前我们需要一张正在加载图 icon_failure.png 大家右键另存为即可:

    修改我们刚刚书写的 activity_main.xml :

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"> <com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/main_sdv"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_centerInParent="true"
    fresco:actualImageScaleType="focusCrop"
    fresco:placeholderImage="@mipmap/icon_placeholder"
    fresco:placeholderImageScaleType="fitCenter"
    fresco:progressBarImage="@mipmap/icon_progress_bar"
    fresco:progressBarImageScaleType="centerInside"
    fresco:progressBarAutoRotateInterval="5000"
    fresco:failureImage="@mipmap/icon_failure"
    fresco:failureImageScaleType="centerInside"
    ></com.facebook.drawee.view.SimpleDraweeView> </RelativeLayout>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    代码分析:

    修改我们的 MainActivity 里代码:

    public class MainActivity extends AppCompatActivity {
    
        private SimpleDraweeView simpleDraweeView;
    
        @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Fresco.initialize(this);
    setContentView(R.layout.activity_main);
    initView();
    } private void initView() {
    //创建SimpleDraweeView对象
    simpleDraweeView = (SimpleDraweeView) findViewById(R.id.main_sdv);
    //创建将要下载的图片的URI
    Uri imageUri = Uri.parse("http://my.csdn.net/uploads/avatar_y1scp.jpg");
    //开始下载
    simpleDraweeView.setImageURI(imageUri);
    }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    代码说明:

    运行效果:

  • 重试图—retryImage:

    在此之前我们需要一张正在加载图 icon_retry.png 大家右键另存为即可:

    修改我们刚刚书写的 activity_main.xml :

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"> <com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/main_sdv"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_centerInParent="true"
    fresco:actualImageScaleType="focusCrop"
    fresco:placeholderImage="@mipmap/icon_placeholder"
    fresco:placeholderImageScaleType="fitCenter"
    fresco:progressBarImage="@mipmap/icon_progress_bar"
    fresco:progressBarImageScaleType="centerInside"
    fresco:progressBarAutoRotateInterval="5000"
    fresco:failureImage="@mipmap/icon_failure"
    fresco:failureImageScaleType="centerInside"
    fresco:retryImage="@mipmap/icon_retry"
    fresco:retryImageScaleType="centerCrop"
    ></com.facebook.drawee.view.SimpleDraweeView> </RelativeLayout>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    代码分析:

    修改我们的 MainActivity 里代码:

    public class MainActivity extends AppCompatActivity {
    
        private SimpleDraweeView simpleDraweeView;
    
        @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Fresco.initialize(this);
    setContentView(R.layout.activity_main);
    initView();
    } private void initView() {
    //创建SimpleDraweeView对象
    simpleDraweeView = (SimpleDraweeView) findViewById(R.id.main_sdv);
    //创建将要下载的图片的URI
    Uri imageUri = Uri.parse("http://my.csdn.net/uploads/avatar_y1scp.jpg");
    //开始下载
    simpleDraweeView.setImageURI(imageUri); //创建DraweeController
    DraweeController controller = Fresco.newDraweeControllerBuilder()
    //加载的图片URI地址
    .setUri(imageUri)
    //设置点击重试是否开启
    .setTapToRetryEnabled(true)
    //设置旧的Controller
    .setOldController(simpleDraweeView.getController())
    //构建
    .build(); //设置DraweeController
    simpleDraweeView.setController(controller);
    }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    代码说明:

    运行效果:

    注意:

    重复加载4次还是没有加载出来的时候才会显示 failureImage(失败图) 的图片

  • 淡入淡出动画—fadeDuration:

    修改我们刚刚书写的 activity_main.xml :

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"> <com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/main_sdv"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_centerInParent="true"
    fresco:actualImageScaleType="focusCrop"
    fresco:placeholderImage="@mipmap/icon_placeholder"
    fresco:placeholderImageScaleType="fitCenter"
    fresco:progressBarImage="@mipmap/icon_progress_bar"
    fresco:progressBarImageScaleType="centerInside"
    fresco:progressBarAutoRotateInterval="5000"
    fresco:failureImage="@mipmap/icon_failure"
    fresco:failureImageScaleType="centerInside"
    fresco:retryImage="@mipmap/icon_retry"
    fresco:retryImageScaleType="centerCrop"
    fresco:fadeDuration="5000"
    ></com.facebook.drawee.view.SimpleDraweeView> </RelativeLayout>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    代码说明:

    MainActivity 中的代码无需修改。

    运行效果:

    重试+进度图+失败图 进度图+正确图
  • 背景图—backgroundImage:

    这里呢,我们的背景图采用的是一个系统所提供的颜色中的一种。

    修改我们刚刚书写的 activity_main.xml :

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"> <com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/main_sdv"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_centerInParent="true"
    fresco:actualImageScaleType="focusCrop"
    fresco:fadeDuration="5000"
    fresco:backgroundImage="@android:color/holo_orange_light"
    ></com.facebook.drawee.view.SimpleDraweeView> </RelativeLayout>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    代码说明:

    MainActivity 中的代码无需修改,运行效果:

  • 叠加图—overlayImage:

    这里呢,我们的背景图采用的是一个系统所提供的颜色中的一种。

    修改我们刚刚书写的 activity_main.xml :

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"> <com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/main_sdv"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_centerInParent="true"
    fresco:actualImageScaleType="focusCrop"
    fresco:placeholderImage="@mipmap/icon_placeholder"
    fresco:placeholderImageScaleType="fitCenter"
    fresco:progressBarImage="@mipmap/icon_progress_bar"
    fresco:progressBarImageScaleType="centerInside"
    fresco:progressBarAutoRotateInterval="5000"
    fresco:failureImage="@mipmap/icon_failure"
    fresco:failureImageScaleType="centerInside"
    fresco:retryImage="@mipmap/icon_retry"
    fresco:retryImageScaleType="centerCrop"
    fresco:fadeDuration="5000"
    fresco:backgroundImage="@android:color/holo_orange_light"
    fresco:pressedStateOverlayImage="@android:color/holo_green_dark"
    fresco:overlayImage="@android:color/black"
    ></com.facebook.drawee.view.SimpleDraweeView> </RelativeLayout>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    代码说明:

    MainActivity 中的代码无需修改。

    运行效果:

    从运行效果来看,叠加图在最上面,覆盖了下面的图。

  • 圆形图—roundAsCircle:

    一行代码搞定圆形图:设置roundAsCircle为true;

    修改我们刚刚书写的 activity_main.xml :

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"> <com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/main_sdv"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_centerInParent="true"
    fresco:actualImageScaleType="focusCrop"
    fresco:placeholderImage="@mipmap/icon_placeholder"
    fresco:placeholderImageScaleType="fitCenter"
    fresco:progressBarImage="@mipmap/icon_progress_bar"
    fresco:progressBarImageScaleType="centerInside"
    fresco:progressBarAutoRotateInterval="5000"
    fresco:failureImage="@mipmap/icon_failure"
    fresco:failureImageScaleType="centerInside"
    fresco:retryImage="@mipmap/icon_retry"
    fresco:retryImageScaleType="centerCrop"
    fresco:fadeDuration="5000"
    fresco:backgroundImage="@android:color/holo_orange_light"
    fresco:roundAsCircle="true"
    ></com.facebook.drawee.view.SimpleDraweeView> </RelativeLayout>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    代码说明:

    MainActivity 中的代码无需修改。

    运行效果:

    可以看到,从图片开始加载一直到图片下载完毕,整个图像都是圆形的。

  • 圆角图—roundedCornerRadius:

    修改我们刚刚书写的 activity_main.xml :

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"> <com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/main_sdv"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_centerInParent="true"
    fresco:actualImageScaleType="focusCrop"
    fresco:placeholderImage="@mipmap/icon_placeholder"
    fresco:placeholderImageScaleType="fitCenter"
    fresco:progressBarImage="@mipmap/icon_progress_bar"
    fresco:progressBarImageScaleType="centerInside"
    fresco:progressBarAutoRotateInterval="5000"
    fresco:failureImage="@mipmap/icon_failure"
    fresco:failureImageScaleType="centerInside"
    fresco:retryImage="@mipmap/icon_retry"
    fresco:retryImageScaleType="centerCrop"
    fresco:fadeDuration="5000"
    fresco:backgroundImage="@android:color/holo_orange_light"
    fresco:roundedCornerRadius="30dp"
    ></com.facebook.drawee.view.SimpleDraweeView> </RelativeLayout>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    代码说明:

    MainActivity 中的代码无需修改。

    运行效果:

    可以看到,从图片开始加载一直到图片下载完毕,整个图像都是圆角的。

    圆角属性 圆角属性
    左上角是否为圆角fresco:roundTopLeft="false" 右上角是否为圆角fresco:roundTopRight="false"
    左下角是否为圆角fresco:roundBottomLeft="false" 右下角是否为圆角fresco:roundBottomRight="false"

    注意:

    当我们同时设置图像显示为圆形图像和圆角图像时,只会显示为圆形图像。

  • 圆形圆角边框宽度及颜色—roundingBorder:

    修改我们刚刚书写的 activity_main.xml :

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"> <com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/main_sdv"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_centerInParent="true"
    fresco:actualImageScaleType="focusCrop"
    fresco:placeholderImage="@mipmap/icon_placeholder"
    fresco:placeholderImageScaleType="focusCrop"
    fresco:progressBarImage="@mipmap/icon_progress_bar"
    fresco:progressBarImageScaleType="focusCrop"
    fresco:progressBarAutoRotateInterval="5000"
    fresco:failureImage="@mipmap/icon_failure"
    fresco:failureImageScaleType="focusCrop"
    fresco:retryImage="@mipmap/icon_retry"
    fresco:retryImageScaleType="focusCrop"
    fresco:fadeDuration="5000"
    fresco:backgroundImage="@android:color/holo_orange_light"
    fresco:roundAsCircle="true"
    fresco:roundedCornerRadius="30dp"
    fresco:roundTopLeft="true"
    fresco:roundTopRight="true"
    fresco:roundBottomLeft="true"
    fresco:roundBottomRight="true"
    fresco:roundingBorderWidth="10dp"
    fresco:roundingBorderColor="@android:color/black"
    ></com.facebook.drawee.view.SimpleDraweeView> </RelativeLayout>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    代码说明:

    MainActivity 中的代码无需修改。

    运行效果(左边显示的是带边框的圆形图像,右边显示的是带边框的圆角图像):

  • 圆形或圆角图像底下的叠加颜色—roundWithOverlayColor:

    修改我们刚刚书写的 activity_main.xml :

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"> <com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/main_sdv"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_centerInParent="true"
    fresco:actualImageScaleType="focusCrop"
    fresco:placeholderImage="@mipmap/icon_placeholder"
    fresco:placeholderImageScaleType="focusCrop"
    fresco:progressBarImage="@mipmap/icon_progress_bar"
    fresco:progressBarImageScaleType="focusCrop"
    fresco:progressBarAutoRotateInterval="5000"
    fresco:failureImage="@mipmap/icon_failure"
    fresco:failureImageScaleType="focusCrop"
    fresco:retryImage="@mipmap/icon_retry"
    fresco:retryImageScaleType="focusCrop"
    fresco:fadeDuration="5000"
    fresco:backgroundImage="@android:color/holo_orange_light"
    fresco:roundWithOverlayColor="@android:color/darker_gray"
    fresco:roundAsCircle="true"
    ></com.facebook.drawee.view.SimpleDraweeView> </RelativeLayout>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    代码说明:

    MainActivity 中的代码无需修改。

    运行效果(左边为圆形效果,右边为圆角效果):

  • 缩放类型—ScaleType:

    类型 描述
    center 居中,无缩放
    centerCrop 保持宽高比缩小或放大,使得两边都大于或等于显示边界。居中显示。
    focusCrop 同centerCrop, 但居中点不是中点,而是指定的某个点
    centerInside 使两边都在显示边界内,居中显示。如果图尺寸大于显示边界,则保持长宽比缩小图片。
    fitCenter 保持宽高比,缩小或者放大,使得图片完全显示在显示边界内。居中显示
    fitStart 同上。但不居中,和显示边界左上对齐
    fitEnd 同fitCenter, 但不居中,和显示边界右下对齐
    fitXY 不保存宽高比,填充满显示边界
    none 如要使用tile mode显示, 需要设置为none

    推荐使用:focusCrop 类型

    Fresco中文说明对这一点也有详情的说明: 缩放

  • 总结:

    XML属性 意义
    fadeDuration 淡入淡出动画持续时间(单位:毫秒ms)
    actualImageScaleType 实际图像的缩放类型
    placeholderImage 占位图
    placeholderImageScaleType 占位图的缩放类型
    progressBarImage 进度图
    progressBarImageScaleType 进度图的缩放类型
    progressBarAutoRotateInterval 进度图自动旋转间隔时间(单位:毫秒ms)
    failureImage 失败图
    failureImageScaleType 失败图的缩放类型
    retryImage 重试图
    retryImageScaleType 重试图的缩放类型
    backgroundImage 背景图
    overlayImage 叠加图
    pressedStateOverlayImage 按压状态下所显示的叠加图
    roundAsCircle 设置为圆形图
    roundedCornerRadius 圆角半径
    roundTopLeft 左上角是否为圆角
    roundTopRight 右上角是否为圆角
    roundBottomLeft 左下角是否为圆角
    roundBottomRight 右下角是否为圆角
    roundingBorderWidth 圆形或者圆角图边框的宽度
    roundingBorderColor 圆形或者圆角图边框的颜色
    roundWithOverlayColor 圆形或者圆角图底下的叠加颜色(只能设置颜色)
    viewAspectRatio 控件纵横比
  • GitHub:

    本教程最终项目GitHub地址:https://github.com/scp504677840/Fresco

 
11

0

【转】Android图片加载神器之Fresco-加载图片基础[详细图解Fresco的使用]的更多相关文章

  1. Android图片加载神器之Fresco-加载图片基础[详细图解Fresco的使用](秒杀imageloader)

    Fresco简单的使用—SimpleDraweeView 百学须先立志—学前须知: 在我们平时加载图片(不管是下载还是加载本地图片…..)的时候,我们经常会遇到这样一个需求,那就是当图片正在加载时应该 ...

  2. Android图片加载神器之Fresco,基于各种使用场景的讲解

    Fresco是Facebook开源Android平台上一个强大的图片加载库,也是迄今为止Android平台上最强大的图片加载库. 优点:相对于其他开源的第三方图片加载库,Fresco拥有更好的内存管理 ...

  3. Android图片加载神器之Fresco, 基于各种使用场景的讲解

    Fresco是Facebook开源Android平台上一个强大的图片加载库,也是迄今为止Android平台上最强大的图片加载库. 优点:相对于其他开源的第三方图片加载库,Fresco拥有更好的内存管理 ...

  4. Android项目实战(三十):Fresco加载gif图片并播放

    前言: 项目中图文混合使用的太多太多了,但是绝大部分都是静态图片. 然而项目开发中有这么一个需求:显示一个出一个简短的动画(一般都不超过3秒)演示 比如说:一个功能提供很多步骤来教用户做广播体操,那么 ...

  5. fresco加载本地图片、gif资源

    首先 来看看fresco 是个神马东西 https://github.com/facebook/fresco 这个是fresco的一个官方gifhub 官网为http://frescolib.org/ ...

  6. Android 框架练成 教你打造高效的图片加载框架(转)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/41874561,本文出自:[张鸿洋的博客] 1.概述 优秀的图片加载框架不要太多, ...

  7. 使用 Fresco加载图片

    概念: ImagePipeline ——负责从网络.本地图片.Content Provider(内容提供者)或者本地资源那里获取图片,压缩保存在本地存储中和在内存中保存为压缩的图片 Drawee——处 ...

  8. Android 框架练成 教你打造高效的图片加载框架

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/41874561,本文出自:[张鸿洋的博客] 1.概述 优秀的图片加载框架不要太多, ...

  9. Android 高清加载巨图方案 拒绝压缩图片

    Android 高清加载巨图方案 拒绝压缩图片 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/49300989: 本文出自:[张 ...

随机推荐

  1. 将数据库中的表注册到K2服务中,并封装为Smart Object

    转:http://www.cnblogs.com/dannyli/archive/2011/08/15/2139550.html K2 blackpearl项目中经常需要将其他数据中的表注册到K2服务 ...

  2. JS URL编码

    JS URL编码escape() 方法: 采用ISO Latin字符集对指定的字符串进行编码.所有的空格符.标点符号.特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在 ...

  3. ACE的 日志

    http://wenku.baidu.com/link?url=dK6j9_0pICRjxWW7usBlkCxPTa8zFSPyUe_uWAkwMPFDU4ip_tEfxpOitxjkl3RuPy3D ...

  4. visual asssit 过期提示

    把目录下的VA_X.dll文件复制到上面所说的文件夹下覆盖源文件即可 对于vs2010的朋友需要额外注意,使用2010的朋友,是需要覆盖到Visual Studio 2010的Visual Assis ...

  5. Eclipse安装配置PyDev插件

    Eclipse安装配置PyDev插件 关于PyDev PyDev是一个功能强大的 Eclipse插件,使用户可用 Eclipse 来进行 Python 应用程序的开发和调试.PyDev 插件的出现方便 ...

  6. STL中用erase()方法遍历删除元素 .xml

    pre{ line-height:1; color:#f0caa6; background-color:#2d161d; font-size:16px;}.sysFunc{color:#e54ae9; ...

  7. WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5

    以下是我的程序(取自headfirst Java): import javax.sound.midi.*; public class MiniMiniMusicApp { public static ...

  8. 排列组合+组合数取模 HDU 5894

    // 排列组合+组合数取模 HDU 5894 // 题意:n个座位不同,m个人去坐(人是一样的),每个人之间至少相隔k个座位问方案数 // 思路: // 定好m个人 相邻人之间k个座位 剩下就剩n-( ...

  9. This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME 错误解决

    在一个Mysql表达式中使用嵌套查询,出现了这个错误.原因是内层select语句带有limit子句.   在网上查了下,有文章指出: 比如这样的语句是不能正确执行的. select * from ta ...

  10. SQL Server 索引 之 书签查找 <第十一篇>

    一.书签查找的概念 书签可以帮助SQL Server快速从非聚集索引条目导向到对应的行,其实这东西几句话我就能说明白. 如果表有聚集索引(区段结构),那么书签就是从非聚集索引找到聚集索引后,利用聚集索 ...