Fresco图片加载的简单应用

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"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值