Anj*_*dya -1 url android 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;
}
Run Code Online (Sandbox Code Playgroud)
我使用此代码,但加载Image需要很多负载.
那么,有没有选择让它快速?
尝试以下代码: -
首先下载通用加载器lib并编写下面的代码.
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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5678 次 |
| 最近记录: |