Fresco圖片載入的簡單應用

Castertonone發表於2018-06-11

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"/>

相關文章