1.先注册
public class MyApplication extends Application{ @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); } }
2.开始显示图片
public class MainActivity extends AppCompatActivity { private SimpleDraweeView sdv,iv,icon; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { sdv = (SimpleDraweeView)findViewById(R.id.main_simple_drawee_view); // iv = (SimpleDraweeView)findViewById(R.id.iv); icon = (SimpleDraweeView)findViewById(R.id.icon); initData(); isGif(); } /** * 支持gif图 */ private void isGif() { Uri uri = Uri.parse("http://p0.ifengimg.com/pmop/2018/0211/34BB8479C0B68ADA393B11E0FFA12AFA73C41CC4_size330_w423_h236.gif"); DraweeController controller = Fresco.newDraweeControllerBuilder() .setUri(uri) .setOldController(icon.getController())//内存优化 .setAutoPlayAnimations(true) .build(); icon.setController(controller); } private void initData() { Uri uri = Uri.parse("http://dynamic-image.yesky.com/740x-/uploadImages/2015/163/50/690V3VHW0P77.jpg"); // Uri uri = Uri.parse("http://p0.ifengimg.com/pmop/2018/0211/34BB8479C0B68ADA393B11E0FFA12AFA73C41CC4_size330_w423_h236.gif"); DraweeController controller = Fresco.newDraweeControllerBuilder() .setUri(uri) .setAutoPlayAnimations(true) .build(); sdv.setController(controller); } public void start(View view){ Animatable animatableStart = icon.getController().getAnimatable(); if(animatableStart != null&&!animatableStart.isRunning()){ animatableStart.start(); } } public void stop(View view){ Animatable animatableStop = icon.getController().getAnimatable(); if(animatableStop != null && animatableStop.isRunning()){ animatableStop.stop(); } } }
3.5 对应的布局
<
LinearLayout
android
:layout_width=
"match_parent"
android
:layout_height=
"match_parent"
android
:orientation=
"horizontal"
xmlns:
fresco
=
"http://schemas.android.com/apk/res-auto"> <
LinearLayout
android
:orientation=
"vertical"
android
:layout_width=
"0dp"
android
:layout_weight=
"3"
android
:layout_height=
"wrap_content"> <
com.facebook.drawee.view.SimpleDraweeView
android
:layout_width=
"400dp"
android
:layout_height=
"200dp"
android
:id=
"@+id/icon"
fresco
:placeholderImage=
"@mipmap/ic_launcher"/> <
com.facebook.drawee.view.SimpleDraweeView
android
:layout_width=
"400dp"
android
:layout_height=
"500dp"
android
:id=
"@+id/main_simple_drawee_view"
fresco
:placeholderImage=
"@mipmap/ic_launcher"/> </
LinearLayout> <
LinearLayout
android
:orientation=
"vertical"
android
:layout_width=
"0dp"
android
:layout_weight=
"1"
android
:layout_height=
"wrap_content"> <
Button
android
:onClick=
"start"
android
:layout_width=
"wrap_content"
android
:layout_height=
"wrap_content"
android
:text=
"开始"/> <
Button
android
:onClick=
"stop"
android
:layout_width=
"wrap_content"
android
:layout_height=
"wrap_content"
android
:text=
"暂停"/> </
LinearLayout>
</LinearLayout>
4.添加的依赖
compile
'com.facebook.fresco:fresco:0.11.0' compile
'com.facebook.fresco:animated-gif:0.12.0'
别忘了注册
android
:name=
".
MyApplication
"
<uses-permission android:name="android.permission.INTERNET"/>