简体   繁体   English

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

[英]How to add Round shaped image using universal image loader in android

i am trying to add Round shaped image to my to my layout using Universal Image Loader below is my imageUtil.class 我正在尝试使用通用图像加载器将圆形图像添加到我的布局中,下面是我的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();
}

and my layout contain the bellow code 而且我的布局包含以下代码

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

in my main activity 在我的主要活动中

ImageView profilepic;

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

in app grandle 在应用程序中

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

this is the code which i am having. 这是我所拥有的代码。 my concern is in drawer layout i want to show a Rounded profile pic. 我关心的是抽屉布局,我想显示一个圆形的轮廓图片。

please comment if there any doubt. 如有任何疑问,请发表评论。

Try this short code it works for me: 试试这个简短的代码对我有用:

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

Try this it will work 试试这个会起作用

 <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 http://www.androidhub4you.com/2014/10/android-custom-shape-imageview-rounded.html

As the OP wants I'm Posting the code for picasso.. 正如OP希望的那样,我正在发布毕加索的代码。

Add this dependancy.. 添加此依赖项。

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

Use this code to load image on Imageview. 使用此代码在Imageview上加载图像。

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

Above code does not produce circle image its just load image from url and add to image view.. To produce circle image you can add this circle transform class to your utils 上面的代码不会产生圆形图像,它只是从url加载图像并添加到图像视图中。要产生圆形图像,您可以将此圆形转换类添加到utils中

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";
    }
}

Now just little modification in the above code for getting circle image 现在只需对上面的代码进行少量修改即可获取圆形图像

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);

Now Image is load as a circle image 现在图像被加载为圆形图像

For more info about Picasso See this official Page 有关毕加索的更多信息, 请参见此官方页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM