[关闭]
@linux1s1s 2016-11-21T09:56:35.000000Z 字数 2914 阅读 1760

Android 开源库 - Fresco

AndroidRefine 2016-11


系列博文
Android 开源库 - OkHttp
Android 开源库 - Retrofit
Android 开源库 - Okio
Android 开源库 - Fresco

这里仅仅给个如何使用的示例,至于源码剖析可以移步Fresco源码分析的系列文章。

配置

  1. //Fresco
  2. compile 'com.facebook.fresco:fresco:0.14.1'
  3. //Maven
  4. // https://mvnrepository.com/artifact/com.facebook.fresco/
  5. //WebP Not Animated
  6. //compile group: 'com.facebook.fresco', name: 'webpsupport', version: '0.14.1'
  7. //WebP Animated
  8. compile group: 'com.facebook.fresco', name: 'webpsupport', version: '0.14.1'
  9. compile group: 'com.facebook.fresco', name: 'animated-webp', version: '0.14.1'
  10. // GIF
  11. compile group: 'com.facebook.fresco', name: 'animated-gif', version: '0.14.1'

TIPS: 虽然这里没有加入okhttp的依赖,但是fresco 0.14.1 会自动依赖okhttp(3.3.0)
当然如果你手动指定了okhttp的版本,这里会自动替换
比如这样指定

  1. // OkHttp
  2. compile 'com.squareup.okhttp3:okhttp:3.4.2'
  3. // OkIo
  4. compile 'com.squareup.okio:okio:1.11.0'
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="okhttp.mutex.com.okhttpdemo">
  4. <uses-permission android:name="android.permission.INTERNET" />
  5. <application
  6. android:name=".OkhttpApplication"
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:supportsRtl="true"
  11. android:theme="@style/AppTheme">
  12. <activity android:name=".MainActivity">
  13. <intent-filter>
  14. <action android:name="android.intent.action.MAIN" />
  15. <category android:name="android.intent.category.LAUNCHER" />
  16. </intent-filter>
  17. </activity>
  18. </application>
  19. </manifest>

资源文件activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/activity_main"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="okhttp.mutex.com.okhttpdemo.MainActivity">
  8. <com.facebook.drawee.view.SimpleDraweeView
  9. android:id="@+id/simple_drawee_view"
  10. android:layout_width="141dp"
  11. android:layout_height="146dp"
  12. android:layout_centerHorizontal="true" />
  13. </ScrollView>

Java原文件MainActivity.java

  1. public class MainActivity extends AppCompatActivity {
  2. private static final String WEBP = "http://7xinl9.com1.z0.glb.clouddn.com/Boston-City-Flow.webp";
  3. private static final String WEBP_ANIMATED = "http://7xinl9.com1.z0.glb.clouddn.com/BladeRunner.webp";
  4. private static final String JPG = "http://7xinl9.com1.z0.glb.clouddn.com/20160425150855.png";
  5. private static final String GIF = "http://7xinl9.com1.z0.glb.clouddn.com/10.gif";
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. initContentView();
  11. }
  12. private void initContentView() {
  13. SimpleDraweeView view = (SimpleDraweeView) findViewById(R.id.simple_drawee_view);
  14. DraweeController controller = Fresco.newDraweeControllerBuilder()
  15. .setUri(Uri.parse(GIF))
  16. .setAutoPlayAnimations(true)
  17. .build();
  18. view.setController(controller);
  19. }
  20. }

验证结果

此处输入图片的描述

经验证,Fresco-0.14.1支持JPG,PNG,GIF,WebpAnimated WebP等常见的图片格式。
ToolsJPG Converter to Webp

参考文章:
OkHttp GitHub Doc
Retrofit GitHub DocR Doc
Fresco GitHub Doc
Okio GitHub Video

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注