picasso的使用

来源:互联网 发布:淘宝上外贸是正品吗 编辑:程序博客网 时间:2024/06/05 23:31
导入方法: 下载 picasso.jar 导入libs文件夹下即可。  AS配置:   compile 'com.squareup.picasso:picasso:2.5.2'1.一般使用格式        支持本地资源图片(drawable文件夹下),文件,资产(assets文件夹下),内容提供商(contacts photo),网页链接作为图片来源。
//在下载出错的图像被设置前,Picasso会尝试三次请求,三次都失败才会显示errorPicasso.with(context)        .load(url)        //.fit()  //让图片的宽高恰好等于组件宽高.前提是组件必须有大小使用了fit方法,就不能调用resize        //.resize(80, 80).centerCrop() | .centerInside()             //图片裁剪        //.resizeDimen(R.dimen.width,R.dimen.height)        //.transform(new CropSquareTransformation())    //自定义实现裁剪        //.placeholder(R.drawable.user_placeholder)       //下载完成前的图像        //.error(R.drawable.user_placeholder_error)       //出错时的图像        .into(imageView);//要想显示不拉伸的图,组件可设置scaleType=centerInsideclass CropSquareTransformation implements Transformation {    @Override    public Bitmap transform(Bitmap source) {        //自定义图片裁剪        int size = Math.min(source.getWidth(), source.getHeight());        int x = (source.getWidth() - size) / 2;        int y = (source.getHeight() - size) / 2;        Bitmap result = Bitmap.createBitmap(source, x, y, size, size);        if (result != source) {            source.recycle();        }        return result;    }    @Override    public String key() {        return "square()";    }}//通常在滑动列表的时候需要暂停请求,例如在RecyclerView滑动时监听,处理不同的表现:public class RecyclerViewScrollListener implements AbsListView.OnScrollListener {    private final Context context;        public RecyclerViewScrollListener(Context context) {        this.context = context;    }    @Override    public void onScrollStateChanged(AbsListView view, int scrollState) {        final Picasso picasso = Picasso.with(context);        if (scrollState == SCROLL_STATE_IDLE || scrollState == SCROLL_STATE_TOUCH_SCROLL) {            picasso.resumeTag(context);        } else {            picasso.pauseTag(context);        }    }    @Override    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,                         int totalItemCount) {        // Do nothing.    }}
2.调试使用    调用setIndicatorsEnabled(true);不同来源的图片可自动在左上角显示一个不同颜色的三角形。如:红色--网络,黄色--磁盘缓存,绿色--内存。

推荐一篇相关的博客:Picasso框架实战演练
0 0
原创粉丝点击