Fresco图片加载框架使用

**前言:**该文章记录本人开发使用Fresco框架遇到的坑和解决的方案
**建议:**选用框架原则,完善、低耦合、高性能、易封装,在这点上,Fresco图片框架的耦合性就比较高了,对开发需求和维护的成本也高了。在这点上,本人建议使用谷歌推荐的Glide图片加载框架。

引入方式:

  1. 直接引入:(纯洁的项目中可以这样做)
dependencies {
  compile 'com.facebook.fresco:fresco:0.9.0+'
}

2.使用Fresco类库:这种方式更加灵活,可以查看源码,还可以对冲突包进行处理
注意:过程中,工作目录最好不要存在中文,然后一次性编译并引入Fresco框架,这样不容易出错

项目中:

  1. 在 Application 初始化时,在应用调用 setContentView() 之前,进行初始化:Fresco.initialize(context);
  2. 活动中使用,如下属性是常用的,抽取出来了:
    根布局添加:xmlns:fresco=“http://schemas.android.com/apk/res-auto”
<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/sdv_copy_titleImage"
    android:layout_width="match_parent"
    android:layout_height="180dp"
    fresco:actualImageScaleType="focusCrop"
    fresco:fadeDuration="20"

    fresco:placeholderImage="@mipmap/unfinished_rect_bg"
    fresco:placeholderImageScaleType="centerCrop"   fitCenter

    fresco:roundAsCircle="false"
    fresco:roundBottomLeft="false"
    fresco:roundBottomRight="false"
    fresco:roundTopLeft="false"
    fresco:roundTopRight="false"
    fresco:roundedCornerRadius="0dp"
    fresco:roundingBorderColor="#FFFFFF"
    fresco:roundingBorderWidth="0px" />

主活动中:

Uri uri = Uri.parse("http://pic.nipic.com/2008-05-23/2008523164110586_2.jpg");
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
draweeView.setImageURI(uri);

遇到问题及解决方案:
在导入Fresco框架时,遇到如下问题:

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_51\bin\java.exe'' finished with non-zero exit value 2

解决方案:造成原因就是jar出现重复了,导致编译转化失败

  1. 在项目中,同时使用Fresco框架+vollery框架(自己修改的),需要把Fresco中libs目录下的vollery删除,依赖自己的vollery类库,然后app引用这个Fresco类库
  2. 如果你项目工程中使用了nineoldandroids兼容3.0的动画,那么fresco中也使用了该jar包,需要将该包移除。
  3. support-v7包中的support-annotations和fresco也同样的导致了冲突,包重复,所以还要去掉v7下的。
    看下图:
    这里写图片描述
    这样,就完美的实现了在Android Studio中集成fresco尽情的玩转图片吧!

补充:
import org.apache.http.client.HttpClient;
在23版本中没有这个包,需要自己手动在gradle引入

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.0.1'

    compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'}

项目中vollery类库引入:

android {
    useLibrary 'org.apache.http.legacy'
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile 'org.apache.httpcomponents:httpcore:4.4.2'
}

Fresco混淆配置

# Keep our interfaces so they can be used by other ProGuard rules.
# See http://sourceforge.net/p/proguard/bugs/466/
-keep,allowobfuscation @interface com.facebook.common.internal.DoNotStrip

# Do not strip any method/class that is annotated with @DoNotStrip
-keep @com.facebook.common.internal.DoNotStrip class *
-keepclassmembers class * {
    @com.facebook.common.internal.DoNotStrip *;
}

# Keep native methods
-keepclassmembers class * {
    native ;
}

-dontwarn okio.**
-dontwarn javax.annotation.**
-dontwarn com.android.volley.toolbox.**

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值