如何在android中使用通用图像加载器添加圆形图像

时间:2017-05-02 05:22:45

标签: android

我正在尝试使用下面的Universal Image Loader将圆形图像添加到我的布局中imageUtil.class

import android.graphics.Bitmap.Config;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import zesteve.com.myapplication.R;


public class ImageUtil {

    public static void displayImage(ImageView view, String path, ImageLoadingListener listener) {
        ImageLoader loader = ImageLoader.getInstance();
        try {
            loader.displayImage(path, view, DEFAULT_DISPLAY_IMAGE_OPTIONS, listener);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            loader.clearMemoryCache();
        }
    }

    public static void displayRoundImage(ImageView view, String path, ImageLoadingListener listener) {
        ImageLoader loader = ImageLoader.getInstance();
        try {
            loader.displayImage(path, view, ROUND_DISPLAY_IMAGE_OPTIONS, listener);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            loader.clearMemoryCache();
        }
    }

    public static void loadImage(String path, ImageLoadingListener listener) {
        ImageLoader loader = ImageLoader.getInstance();
        try {
            loader.loadImage(path, DEFAULT_DISPLAY_IMAGE_OPTIONS, listener);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        }
    }

    //TODO Change default image
    private static final DisplayImageOptions.Builder DEFAULT_DISPLAY_IMAGE_OPTIONS_BUIDLER = new DisplayImageOptions.Builder()
            .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
            .displayer(new FadeInBitmapDisplayer(300, true, false, false))
            .showImageForEmptyUri(R.drawable.applogo)
            .showImageOnLoading(R.drawable.applogo)
            .showImageOnFail(R.drawable.applogo).cacheOnDisk(true)
            .cacheInMemory(true).bitmapConfig(Config.ARGB_8888);

    private static final DisplayImageOptions DEFAULT_DISPLAY_IMAGE_OPTIONS = DEFAULT_DISPLAY_IMAGE_OPTIONS_BUIDLER
            .build();
    private static final DisplayImageOptions ROUND_DISPLAY_IMAGE_OPTIONS = DEFAULT_DISPLAY_IMAGE_OPTIONS_BUIDLER
            .displayer(new RoundedBitmapDisplayer(500)).build();
}

我的布局包含以下代码

           <ImageView
            android:id="@+id/profimage"
            android:layout_width="72dp"
            android:layout_height="72dp"
            android:layout_margin="20dp" />

在我的主要活动中

ImageView profilepic;

profilepic = (ImageView) findViewById(R.id.profimage);
        String imageUri = "http://pengaja.com/uiapptemplate/newphotos/profileimages/0.jpg";
        ImageUtil.displayRoundImage(profilepic,imageUri,null);

在应用程序grandle

compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

这是我所拥有的代码。我关注的是抽屉布局,我想展示一个圆形的个人资料照片。

如果有任何疑问,请发表评论。

3 个答案:

答案 0 :(得分:0)

试试这个适合我的简短代码:

RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(Activity.this.getResources(), yourbitmap);
roundedBitmapDrawable.setCircular(true);
your imageview.setImageDrawable(roundedBitmapDrawable);

答案 1 :(得分:0)

试试这个会起作用

 <com.androidhub4you.crop.RoundedImageView
        android:id="@+id/imageView_round"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginTop="15dp"
        android:src="@drawable/ic_launcher" />

http://www.androidhub4you.com/2014/10/android-custom-shape-imageview-rounded.html

答案 2 :(得分:0)

由于OP希望我发布毕加索的代码..

添加此依赖性..

compile 'com.squareup.picasso:picasso:2.5.2'

使用此代码在Imageview上加载图像。

Picasso.with(context)
      .load(url)              //Url in string format
      .placeholder(R.drawable.user_placeholder)
      .error(R.drawable.user_placeholder_error)
      .into(imageView);

上面的代码不生成圆形图像,只是从网址加载图片并添加到图片视图中。要生成圆形图像,您可以将此圆形变换类添加到您的工具

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.squareup.picasso.Transformation;

public class CircleTransform 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 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(context)
  .load(url)              //Url in string format
  .placeholder(R.drawable.user_placeholder)
  .error(R.drawable.user_placeholder_error)
  .transform(new CircleTransform())
  .into(imageView);

现在图像加载为圆形图像

有关Picasso See this official Page

的更多信息