我需要在列表页面中显示来自api的图像列表.为此,我使用了两种方法.
第一种方法:
通过将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();
Run Code Online (Sandbox Code Playgroud)
将字节数组转换为位图:
ByteArrayInputStream imageStream = new ByteArrayInputStream(
imgUrl);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
Run Code Online (Sandbox Code Playgroud)
第二种方法:
基于高度和宽度的图像缩放
private static final String TAG_iamge = "Image";
private static final int IO_BUFFER_SIZE = 4 * 1024;
public static …Run Code Online (Sandbox Code Playgroud) 我必须在android中使用单例模式.为了获得更好的性能,使用单例类是很好的.在我的应用程序API调用更多,所以我决定创建一个通用的解析类,用于解析值.我想将它设为单例,因此每个活动都使用此类,最后创建此类的单个实例.请就此提出建议.
public class parsingclass {
Context context;
public parsingclass(Context context) {
this.context = context;
}
public void parsing methods()
{
//methods for parsing values
}
}
Run Code Online (Sandbox Code Playgroud)
//更新的代码
public class Singleton {
private static Singleton instance = null;
//a private constructor so no instances can be made outside this class
private Singleton() {}
//Everytime you need an instance, call this
public static Singleton getInstance() {
if(instance == null)
instance = new Singleton();
return instance;
}
public List<String> parsing_home()
{
List<String> set=new …Run Code Online (Sandbox Code Playgroud)