-1

I have created an android application in that I want to display image in image view from in round shape.

My code is:

    private class LoadImage extends AsyncTask<String, Void, String> 
    {
        Bitmap image=null;
        @Override
        protected String doInBackground(String... params) 
        {
            try 
            {
                URL url = new URL(imgUrl);
                image = BitmapFactory.decodeStream(url.openConnection().getInputStream());              
                image=Global.getRoundedShape(image);                                
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }
            return "Executed";
        }
        @Override
        protected void onPostExecute(String code) 
        {
            try
            {
                profilepic.setImageBitmap(image);
            }
            catch (Exception e) 
            {
                e.printStackTrace();
            }
        }    
    }


public static Bitmap getRoundedShape(Bitmap scaleBitmapImage) 
{
    int targetWidth = 168;
    int targetHeight = 166;
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, 
                        targetHeight,Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(((float) targetWidth - 1) / 2,
        ((float) targetHeight - 1) / 2,
        (Math.min(((float) targetWidth), 
        ((float) targetHeight)) / 2),
        Path.Direction.CCW);

    canvas.clipPath(path);
    Bitmap sourceBitmap = scaleBitmapImage;
    canvas.drawBitmap(sourceBitmap, 
        new Rect(0, 0, sourceBitmap.getWidth(),
        sourceBitmap.getHeight()), 
        new Rect(0, 0, targetWidth, targetHeight), null);
    return targetBitmap;
}

I use this code, but it takes much load for load Image.

So, is there any option to make it fast ?

Lucifer
  • 29,392
  • 25
  • 90
  • 143
Anjali Pandya
  • 83
  • 2
  • 9

7 Answers7

5

try below code:-

First download universal loader lib and write below code.

https://github.com/nostra13/Android-Universal-Image-Loader/downloads

int rounded_value = 120;    

DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true).displayer(new RoundedBitmapDisplayer(rounded_value)).build();

ImageLoader.getInstance().displayImage(strUrl1, imgThumb1,options);
duggu
  • 37,851
  • 12
  • 116
  • 113
1

Using below code I get round shape Image in ImageView-

int rounded_value = 123;
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true).displayer(new RoundedBitmapDisplayer(rounded_value)).build();             
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).defaultDisplayImageOptions(options).build(); 
ImageLoader.getInstance().init(config);                 
ImageLoader.getInstance().displayImage(photo, vholder.profilepic, options);
Anjali Pandya
  • 83
  • 2
  • 9
0

You have to apply border to image view :

<stroke android:width="3dp"
        android:color="#ff000000"/>

<padding android:left="1dp"
         android:top="1dp"
         android:right="1dp"
         android:bottom="1dp"/> 

<corners android:radius="30px"/> 
KOTIOS
  • 11,177
  • 3
  • 39
  • 66
0

You can try this:

public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
    int targetWidth = 50;
    int targetHeight = 50;
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, 
                        targetHeight,Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(((float) targetWidth - 1) / 2,
        ((float) targetHeight - 1) / 2,
        (Math.min(((float) targetWidth), 
        ((float) targetHeight)) / 2),
        Path.Direction.CCW);

    canvas.clipPath(path);
    Bitmap sourceBitmap = scaleBitmapImage;
    canvas.drawBitmap(sourceBitmap, 
        new Rect(0, 0, sourceBitmap.getWidth(),
        sourceBitmap.getHeight()), 
        new Rect(0, 0, targetWidth, targetHeight), null);
    return targetBitmap;
}

Refer link link

Community
  • 1
  • 1
Lokesh
  • 5,180
  • 4
  • 27
  • 42
0

You can define a .xml file in drawable folder like this(my_custom_):

<?xml version="1.0" encoding="UTF-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
   <corners android:radius="90dp"/> 
   <padding android:left="10dp" android:right="10dp" android:top="4dp" android:bottom="4dp"/>
   <solid android:color="#3F7CB2"/>
 </shape>

and then you can set your background to this :

android:background="@drawable/my_custom_"
CompEng
  • 7,161
  • 16
  • 68
  • 122
0

Use rounded imageview using https://github.com/vinc3m1/RoundedImageView, Hope it helps you... :)

Richa
  • 3,165
  • 1
  • 22
  • 26
0

Download the latest UIL library from here and use below code:

ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
DisplayImageOptions options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.doctor_logo_big_transparent)
            .showImageForEmptyUri(R.drawable.doctor_logo_big_transparent)
            .showImageOnFail(R.drawable.doctor_logo_big_transparent)
            .cacheInMemory(true)
            .cacheOnDisc(true)
            .considerExifParams(true)
            .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
            .bitmapConfig(Bitmap.Config.ARGB_8888)
            .displayer(new RoundedBitmapDisplayer(125))
            .build();

final ProgressBar mSpnr = (ProgressBar) convertView.findViewById(R.id.xSpinnr);

                    imageLoader.displayImage(imgURL, yourImageView, options, new SimpleImageLoadingListener()
                    {
                        @Override
                        public void onLoadingStarted(String imageUri, View view)
                        {
                        }
                        @Override
                        public void onLoadingFailed(String imageUri, View view, FailReason failReason)
                        {
                            //mHolder.mSpnr.setVisibility(View.GONE);
                            String message = null;
                            switch (failReason.getType())
                            {
                                case IO_ERROR:
                                    message = "Input/Output error";
                                    break;
                                case DECODING_ERROR:
                                    message = "Image can't be decoded";
                                    break;
                                case NETWORK_DENIED:
                                    message = "Downloads are denied";
                                    break;
                                case OUT_OF_MEMORY:
                                    message = "Out Of Memory error";
                                    break;
                                case UNKNOWN:
                                    message = "Unknown error";
                                    break;
                            }
                            try
                            {
                                //Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
                                Log.w("TAG", ""+message);
                                mSpnr.setVisibility(View.GONE);
                            }
                            catch (Exception e)
                            {
                                e.printStackTrace();
                                mSpnr.setVisibility(View.GONE);
                            }
                        }
                        @Override
                        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
                        {
                            mSpnr.setVisibility(View.GONE);
                        }

                    });

Along with this, you also need to provide fixed width and height to your imageView.

yourImageView.getLayoutParams().height = 150;
yourImageView.getLayoutParams().width = 150;

You may change the value of height and width of the imageView according to the size of image you need to display.

EDIT

If you do not need to display progress-bar over the imageView, you may use below code:

private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
DisplayImageOptions options = new DisplayImageOptions.Builder()
        .showImageOnLoading(R.drawable.doctor_logo_big_transparent)
        .showImageForEmptyUri(R.drawable.doctor_logo_big_transparent)
        .showImageOnFail(R.drawable.doctor_logo_big_transparent)
        .cacheInMemory(true)
        .cacheOnDisc(true)
        .considerExifParams(true)
        .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
        .bitmapConfig(Bitmap.Config.ARGB_8888)
        .displayer(new RoundedBitmapDisplayer(125))
        .build();
imageLoader.displayImage(imgUrl, yourImageView, options, animateFirstListener);

class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

        List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }