Java 检测要显示的图像大小并通过延迟加载图像使用它
我想定制的图像大小用来掩盖真实的图像显示。在UIL(通用图像加载器)配置中,我们可以使用Java 检测要显示的图像大小并通过延迟加载图像使用它,java,android,bitmap,universal-image-loader,Java,Android,Bitmap,Universal Image Loader,我想定制的图像大小用来掩盖真实的图像显示。在UIL(通用图像加载器)配置中,我们可以使用.showStubImage(R.drawable.temp\u drawable)作为显示选项 我的问题是,我正在实现一个类似pinterest的视图。当我从图像中滚动时,图像的位置不稳定,因为它们仍在下载,并被临时绘图覆盖。例如,临时可绘制为45 X 45,则要显示的图像为100 X 50。当显示真实图像时,由于临时图像被真实图像替换,因此滚动时图像会产生这种位移效应。是否有任何方法可以在下载时检测要显示
.showStubImage(R.drawable.temp\u drawable)
作为显示选项
我的问题是,我正在实现一个类似pinterest的视图。当我从图像中滚动时,图像的位置不稳定,因为它们仍在下载,并被临时绘图覆盖。例如,临时可绘制为45 X 45,则要显示的图像为100 X 50。当显示真实图像时,由于临时图像被真实图像替换,因此滚动时图像会产生这种位移效应。是否有任何方法可以在下载时检测要显示的真实图像高度和宽度,并使用temp图像可以使用的宽度和高度临时显示图像大小
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisc(true)
更新来自Universal-Image-Loader.jar的代码 我喜欢使用“.showStubImage(getImageDimension())” 附加的
new DownloadFilesTask().execute(imageUrls[position]);
私有类下载文件任务扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成的方法存根
URL uri=null;
试一试{
uri=新URL(参数[0].toString());
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
InputStream in=null;
尝试
{
//URL oracle=新URL(URL);
URLConnection URLConnection=uri.openConnection();
in=new BufferedInputStream(urlConnection.getInputStream());
final BitmapFactory.Options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeStream(in,null,选项);
//返回选项;
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
最后
{
if(in!=null)
IoUtils.closesilent(in);
}
返回null;
}
}
这与Android没有真正的关系。这是关于你的应用程序的设计
如果是您的服务器,您应该决定图像的大小以及如何管理它们,并相应地放置正确的占位符
如果不是您的服务器,您无法在不以任何方式联系服务器的情况下知道图像的大小,因此您必须设置图像将适合的静态大小,或者在显示占位符之前获取图像的大小
使用服务器文件获得图像分辨率的方法与使用其他文件的方法相同,在解码时使用
编辑:以下是如何从internet获取位图大小的示例代码:
public static BitmapFactory.Options getBitmapOptions(final String url)
{
InputStream in=null;
try
{
URLConnection urlConnection = url.openConnection();
in = new BufferedInputStream(urlConnection.getInputStream());
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in , null, options);
return options;
}
finally
{
if(in!=null)
IOUtils.closeQuietly(in);
}
return null;
}
这与安卓系统没有太大关系。这是关于你的应用程序的设计 如果是您的服务器,您应该决定图像的大小以及如何管理它们,并相应地放置正确的占位符 如果不是您的服务器,您无法在不以任何方式联系服务器的情况下知道图像的大小,因此您必须设置图像将适合的静态大小,或者在显示占位符之前获取图像的大小 使用服务器文件获得图像分辨率的方法与使用其他文件的方法相同,在解码时使用 编辑:以下是如何从internet获取位图大小的示例代码:
public static BitmapFactory.Options getBitmapOptions(final String url)
{
InputStream in=null;
try
{
URLConnection urlConnection = url.openConnection();
in = new BufferedInputStream(urlConnection.getInputStream());
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in , null, options);
return options;
}
finally
{
if(in!=null)
IOUtils.closeQuietly(in);
}
return null;
}
通常,您只允许根据下面的库提供资源id
options = new DisplayImageOptions.Builder().showStubImage(R.drawable.stubImage)....
如果您需要放置动态更改大小的存根图像,则需要在源代码中进行调整,以便您的选项应该接受大小为位图而不是int。为此,我刚刚查看了源代码。以下是步骤:
public void displayImage(字符串uri、ImageView ImageView、DisplayImageOptions选项、ImageLoadingListener侦听器)
方法中,替换此行ImageView.setImageResource(options.getImageForEmptyUri())代码>与imageView.setImageBitmap(sizedBitmap)代码>
showStubImage(int stubImageRes)
中,将参数更改为位图,并在该类中进行必要的更改 Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.stub_image);
bitmap = Bitmap.createScaledBitmap(stub, width, height, false);
通常,您只允许根据下面的库提供资源id
options = new DisplayImageOptions.Builder().showStubImage(R.drawable.stubImage)....
如果您需要放置动态更改大小的存根图像,则需要在源代码中进行调整,以便您的选项应该接受大小为位图而不是int。为此,我刚刚查看了源代码。以下是步骤:
public void displayImage(字符串uri、ImageView ImageView、DisplayImageOptions选项、ImageLoadingListener侦听器)
方法中,替换此行ImageView.setImageResource(options.getImageForEmptyUri())代码>与imageView.setImageBitmap(sizedBitmap)代码>
showStubImage(int stubImageRes)
中,将参数更改为位图,并在该类中进行必要的更改 Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.stub_image);
bitmap = Bitmap.createScaledBitmap(stub, width, height, false);