首页 > 代码库 > Picasso第三方图片框架显示图片

Picasso第三方图片框架显示图片

1:资源:

https://github.com/square/picasso](https://github.com/square/picasso

2:添加权限:

<uses-permission android:name="android.permission.INTERNET"/>

3.布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="www.itcast.com.testpicasso.MainActivity">    <Button        android:id="@+id/btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Picasso" />    <ImageView        android:id="@+id/iv"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

  4:显示普通图片

//显示普通图片                Picasso                        .with(this)// 指定Context                        .load(url) //指定图片URL                        .placeholder(R.mipmap.ic_launcher) //指定图片未加载成功前显示的图片                        .error(R.mipmap.ic_launcher)// 指定图片加载失败显示的图片                        .resize(400, 600)// 指定图片的尺寸                        //.fit()// 指定图片缩放类型为fit                        //.centerInside()// 指定图片缩放类型为centerInside                       // .centerCrop()// 指定图片缩放类型为centerCrop                        .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)// 指定内存缓存策略                        .priority(Picasso.Priority.HIGH)// 指定优先级                        .into(iv1); // 指定显示图片的ImageView

  5:显示圆形图片

  //显示圆形图片                Transformation transform = new 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 squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);                        if (squaredBitmap != source) {                            source.recycle();                        }                        Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());                        Canvas canvas = new Canvas(bitmap);                        Paint paint = new Paint();                        BitmapShader shader = new BitmapShader(squaredBitmap,                                BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);                        paint.setShader(shader);                        paint.setAntiAlias(true);                        float r = size / 2f;                        canvas.drawCircle(r, r, r, paint);                        squaredBitmap.recycle();                        return bitmap;                    }                    @Override                    public String key() {                        return "circle";                    }                };                Picasso                        .with(this)// 指定Context                        .load(url) //指定图片URL                        .transform(transform) // 指定图片转换器                        .into(iv2); // 指定显示图片的ImageView

  6:显示圆角图片

 //显示圆角图片                class RoundedTransformation implements com.squareup.picasso.Transformation {                    private final int radius;                    private final int margin;  // dp                    // radius is corner radii in dp                    // margin is the board in dp                    public RoundedTransformation(final int radius, final int margin) {                        this.radius = radius;                        this.margin = margin;                    }                    @Override                    public Bitmap transform(final Bitmap source) {                        final Paint paint = new Paint();                        paint.setAntiAlias(true);                        paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));                        Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);                        Canvas canvas = new Canvas(output);                        canvas.drawRoundRect(new RectF(margin, margin, source.getWidth() - margin, source.getHeight() - margin), radius, radius, paint);                        if (source != output) {                            source.recycle();                        }                        return output;                    }                    @Override                    public String key() {                        return "rounded(radius=" + radius + ", margin=" + margin + ")";                    }                }                Picasso                        .with(this)// 指定Context                        .load(url) //指定图片URL                        .transform(new RoundedTransformation(360,0)) // 指定图片转换器                        .into(iv3); // 指定显示图片的ImageView

  demo:http://download.csdn.net/detail/tom91/9631371

Picasso第三方图片框架显示图片