Android 快速将图像url转换为位图
我需要在列表页面中显示api中的图像列表。为此,我使用了两种方法 第一种方法:Android 快速将图像url转换为位图,android,bitmap,imageview,imageurl,imagehandler,Android,Bitmap,Imageview,Imageurl,Imagehandler,我需要在列表页面中显示api中的图像列表。为此,我使用了两种方法 第一种方法: 通过将url转换为字节数组,然后将其转换为位图。请查找以下代码 URL imageUrl = new URL(url); URLConnection ucon = imageUrl.openConnection(); InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); Byte
通过将url转换为字节数组,然后将其转换为位图。请查找以下代码
URL imageUrl = new URL(url);
URLConnection ucon = imageUrl.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(500);
int current = 0;
while ((current = bis.read()) != -1) {
/* This approach slowdown the process*/
baf.append((byte) current);
}
byte[] img_ary= baf.toByteArray();
将字节数组转换为位图:
ByteArrayInputStream imageStream = new ByteArrayInputStream(
imgUrl);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
第二种方法:基于高度和宽度的图像缩放
private static final String TAG_iamge = "Image";
private static final int IO_BUFFER_SIZE = 4 * 1024;
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(),
IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inDither = false;
options.inPurgeable = true;
options.inInputShareable = true;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,
options);
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
int reqHeight = 500;
int reqWidth = 500;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2
// and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
int scale = inSampleSize;
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
o2.inDither = false;
o2.inPurgeable = true;
o2.inInputShareable = true;
return BitmapFactory.decodeByteArray(data, 0, data.length, o2);
} catch (Exception e) {
Log.e(TAG_iamge, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
private static void copy(InputStream in, OutputStream out)
throws IOException {
byte[] b = new byte[IO_BUFFER_SIZE];
int read;
while ((read = in.read(b)) != -1) {
out.write(b, 0, read);
}
}
private static void closeStream(Closeable stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
android.util.Log.e("", "Could not close stream", e);
}
}
}
这两种方法都使应用程序速度非常慢。这是我的问题
有一个名叫毕加索的图书馆。它可以有效地从url加载图像。它还可以从文件中加载图像。你想做的就是写一行代码 示例
Picasso.with(context) //Context
.load("http://i.imgur.com/DvpvklR.png") //URL/FILE
.into(imageView)//an ImageView Object to show the loaded image;
它还可以缓存您的图像,因此下次加载的图像可以更快地加载,而不会浪费数据
毕加索还有更多的选择
如果需要圆角位图
Picasso.with(mContext)
.load("your-image-url-or-file-or-drawable")
.transform(new RoundedTransformation(200, 0))
.fit()
.into(imageView);
RoundedTransformation.java
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
// enables hardware accelerated rounded corners
// original idea here : http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/
public class RoundedTransformation implements com.squareup.picasso.Transformation {
private final int radius;
private final int margin; // dp
// radius is corner radii in dp
// margin is the board in dp
public RoundedTransformation(final int radius, final int margin) {
this.radius = radius;
this.margin = margin;
}
@Override
public Bitmap transform(final Bitmap source) {
final Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
canvas.drawRoundRect(new RectF(margin, margin, source.getWidth() - margin, source.getHeight() - margin), radius, radius, paint);
if (source != output) {
source.recycle();
}
return output;
}
@Override
public String key() {
return "rounded";
}
}
有一些开源库专注于将图像加载到
ImageView
中。例如,它非常易于使用,例如:
// Load image, decode it to Bitmap and display Bitmap in ImageView (or any other view
// which implements ImageAware interface)
imageLoader.displayImage(imageUri, imageView);
或:
或:
例如,您可以这样使用它:
public void displayImg(View view){
ImageView imageView = (ImageView)this.findViewById(R.id.image_view);
RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
ImageLoader imageLoader = new ImageLoader(mQueue, new BitmapCache());
ImageListener listener = ImageLoader.getImageListener(imageView,R.drawable.default_image, R.drawable.default_image);
imageLoader.get("http://developer.android.com/images/home/aw_dac.png", listener);
//指定图片允许的最大宽度和高度
//imageLoader.get("http://developer.android.com/images/home/aw_dac.png",listener, 200, 200);
}
这些库被广泛使用,更重要的是,它们是开源的。无需重复执行此类功能。
希望这能对你有所帮助。你忘了在第二次面试中描述你做了什么。为什么?
将url转换为字节数组
??一个人不会做这样的事情。请更好地描述发生的情况。然后将其转换为位图。你不是在第一步就这么做的,他们用的是装卸货。为此,您可以使用第三方库。请查看我编辑的代码嗨,这里我需要更多的澄清。。我想将图像显示为圆形图像。最初我将url转换为位图,然后将该位图转换为圆形位图。。我如何使用这个库来完成这个任务?这可以用毕加索轻松完成。等一下,我给你密码。谢谢。你节省了我的时间。嗨,我要在我的应用程序中实现这个库。与通用图像加载器相比,该库是否更先进?。这是最好的方法吗?因为我将给出我们项目的基线。。如果可以的话,我会继续。。请引导我。每样东西都有自己的特长,请检查以下答案。除了毕加索,我从来没有使用过任何imageHelper库,所以我无法说出它们。毕加索总是适合我。
// Load image, decode it to Bitmap and return Bitmap synchronously
Bitmap bmp = imageLoader.loadImageSync(imageUri);
public void displayImg(View view){
ImageView imageView = (ImageView)this.findViewById(R.id.image_view);
RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
ImageLoader imageLoader = new ImageLoader(mQueue, new BitmapCache());
ImageListener listener = ImageLoader.getImageListener(imageView,R.drawable.default_image, R.drawable.default_image);
imageLoader.get("http://developer.android.com/images/home/aw_dac.png", listener);
//指定图片允许的最大宽度和高度
//imageLoader.get("http://developer.android.com/images/home/aw_dac.png",listener, 200, 200);
}