如何从圆形的URL中加载图像视图中的图像?

时间:2014-04-11 04:35:53

标签: android url imageview

我创建了一个Android应用程序,我希望在圆形图像视图中显示图像。

我的代码是:

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

我使用此代码,但加载Image需要很多负载。

那么,有没有选择让它快速?

7 个答案:

答案 0 :(得分:5)

尝试以下代码: -

首先下载通用加载程序库并编写下面的代码。

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

答案 1 :(得分:1)

使用下面的代码我在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);

答案 2 :(得分:0)

您必须将边框应用于图像视图:

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

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

<corners android:radius="30px"/> 

答案 3 :(得分:0)

你可以试试这个:

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

参考link link

答案 4 :(得分:0)

您可以在.xml文件夹中定义drawable文件,如下所示(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>

然后您可以设置背景:

android:background="@drawable/my_custom_"

答案 5 :(得分:0)

使用https://github.com/vinc3m1/RoundedImageView使用舍入的imageview, 希望它可以帮助你...... :)

答案 6 :(得分:0)

here下载最新的UIL库,并使用以下代码:

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

                    });

除此之外,您还需要为imageView提供固定的宽度和高度。

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

您可以根据需要显示的图像大小更改imageView的高度和宽度值。

修改

如果您不需要在imageView上显示进度条,可以使用以下代码:

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