首先,我已经做了一些事情来在 gridview 中显示图像,使滚动时平滑
<强>1。在后台线程上从互联网加载图像
AsyncTask acyncTask ;
HandlerThread handlerThread ;
URL imageUrl = new URL(link);<br>
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is=conn.getInputStream();
final Bitmap bitmap = BitmapFactory.decodeStream(is);
<强>2。将位图制作成样本大小以节省内存
final BitmapFactory.Options option = new BitmapFactory.Options();
option.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mFile.getPath(),
option);
option.inSampleSize = calculateInSampleSize(option , mSize , mSize);
option.inJustDecodeBounds = false;
option.inPurgeable = true ;
option.inDither = false ;
option.inScaled = false ;
option.inPreferredConfig = Bitmap.Config.ARGB_8888;
<强>3。制作缓存以保存解码后的位图
LruCache lruCache = new LruCache<String , Bitmap>();
在 baseadapter getView();
我将 lruCache.get(key) 获取解码后的位图
<强>4。通过处理程序解码位图时延迟加载
Handler handler = new Handler();
handler.post(new Runnable(){
public void run(){
imageView.setimageBitmap(bitmap);
}
});
现在我遇到了一个大问题,滚动的时候还是有点卡顿 我在谷歌上搜索了一些可以使更好地滚动的东西,不知道是否真的可以改进,或者问题出在我检查每个 getView() 的地方只花了我大约 2 ~6ms 并且它会调用我的代码从工作线程异步加载图像,所以我真的不知道为什么有些应用程序可以非常顺利地加载?我的情况是滚动时屏幕看起来不是很流畅是否可以应用一些建议?
编辑:当我滚动时,在缓存中找到位图并显示在屏幕上,如果我快速滚动,它看起来好像没有足够的滚动来显示图像,这会使我的滚动不那么流畅,即使我有在缓存中缓存位图
这是适配器代码:
if (convertView == null) {
convertView = layoutInflater.inflate(
R.layout.row_display_image_grid, null);
viewHolder = new DisplayImageGridViewHolder();
viewHolder.background = (RelativeLayout) convertView
.findViewById(R.id.background);
viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
viewHolder.text = (TextView) convertView.findViewById(R.id.text);
viewHolder.position = position;
viewHolder.text.setEllipsize(TruncateAt.END);
viewHolder.text.setTypeface(typeFace);
viewHolder.image.setOnClickListener(this);
viewHolder.image.setOnLongClickListener(this);
convertView.setTag(viewHolder);
} else {
viewHolder = (DisplayImageGridViewHolder) convertView.getTag();
}
viewHolder.position = position;
imageLoader.loadImage(imageLinkList.get(position), viewHolder.image);
return convertView;
强>强>强>强>最佳答案
下面是我使用的 LazyLoader 的一个例子。请注意,我正在为位图使用 SoftReferences,现在使用 LruCache 可以更好地实现这一点。
这将从 web/sdcard/内存异步加载图像,并从占位符图像创建淡入淡出效果。
public class ImageLoader {
private static MemoryCacheNew memoryCache=new MemoryCacheNew();
private static FileCache fileCache;
private static BitmapFactory.Options bitmapOptions;
private static int mInSampleSize;
public ImageLoader(Context context, int inSampleSize){
fileCache=new FileCache(context);
context = null;
bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = mInSampleSize = inSampleSize;
bitmapOptions.inPreferredConfig = Config.RGB_565;
bitmapOptions.inInputShareable = true;
bitmapOptions.inDither = false;
}
final static int PLACEHOLDER_IMAGE = R.drawable.store_placeholder;
public void DisplayImage(String url, ImageView imageView, boolean checkTags){
try{
new AsyncPhotoTask(imageView, url, checkTags).execute();
}catch(Exception e){
e.printStackTrace();
}
}
public void DisplayImage(String url, ImageView imageView)
{
DisplayImage(url, imageView, true);
}
private static Bitmap getBitmap(String url)
{
File f=fileCache.getFile(url);
if(f!= null){
//from SD cache
Bitmap b = decodeFile(f);
if(b!=null)
return b;
}
//from web
try {
Bitmap bitmap=null;
URL imageUrl;
imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
InputStream is=conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
is.close();
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex){
ex.printStackTrace();
return null;
}
}
//decodes image and scales it to reduce memory consumption
private static Bitmap decodeFile(File f){
try {
return BitmapFactory.decodeStream(new FileInputStream(f), null, bitmapOptions);
} catch (FileNotFoundException e) {
} catch (OutOfMemoryError err){
System.gc();
}
return null;
}
private static class AsyncPhotoLoad extends AsyncTask<Void, Void, TransitionDrawable>{
private Bitmap bmp;
private ImageView imageView;
private String url;
private boolean checkTags;
public AsyncPhotoLoad(ImageView imageView, String url, boolean checkTags
){
this.imageView = imageView;
this.url = url;
this.checkTags = checkTags;
}
@Override
protected TransitionDrawable doInBackground(Void... arg0) {
//check that this is the correct imageview
TransitionDrawable transition = null;
try{
if(checkTags){
String tag = (String)imageView.getTag();
if(!tag.equals(url))
return null;
}
bmp = getBitmap(url);
if(bmp != null){
memoryCache.put(url, bmp, mInSampleSize);
Drawable oldDrawable = imageView.getDrawable();
if(!(oldDrawable instanceof TransitionDrawable)){
Drawable layers[] = new Drawable[2];
layers[0] = imageView.getDrawable();
layers[1] = new BitmapDrawable(bmp);
transition = new TransitionDrawable(layers);
}
}
}catch(Exception e){
e.printStackTrace();
}
return transition;
}
@Override
protected void onPostExecute(TransitionDrawable result) {
if(result != null){
try{
if(checkTags){
String tag = (String)imageView.getTag();
if(!tag.equals(url)){
return;
}
}
imageView.setImageDrawable(result);
result.startTransition(300);
} catch(Exception e){
e.printStackTrace();
}
} else {
if(checkTags){
try{
String tag = (String)imageView.getTag();
if(!tag.equals(url))
return;
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}
private static class AsyncPhotoTask extends AsyncTask<Void, Void, Bitmap>{
private ImageView imageView;
private String url;
private boolean checkTags;
public AsyncPhotoTask(ImageView imageView, String url, boolean checkTags){
this.imageView = imageView;
this.url = url;
this.checkTags = checkTags;
}
@Override
protected Bitmap doInBackground(Void... params) {
try{
if(checkTags)
imageView.setTag(url);
}catch(Exception e){
e.printStackTrace();
}
return memoryCache.get(url, mInSampleSize);
}
@Override
protected void onPostExecute(Bitmap result) {
try{
if(result!=null && !result.isRecycled()){
imageView.setImageBitmap(result);
}
else
{
imageView.setImageResource(PLACEHOLDER_IMAGE);
new AsyncPhotoLoad(imageView, url, checkTags).execute();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void clearCache() {
memoryCache.clear();
fileCache.clear();
}
public static void clearMemory(){
memoryCache.clear();
}
public static class MemoryCacheNew {
private HashMap<String, CachedBitmap> cache=new HashMap<String, CachedBitmap>();
public Bitmap get(String id, int sampleSize){
if(!cache.containsKey(id))
return null;
if(cache.get(id) == null)
return null;
if(cache.get(id).sampleSize != sampleSize)
return null;
SoftReference<Bitmap> ref = cache.get(id).softBitmap;
return ref.get();
}
public void put(String id, Bitmap bitmap, int sampleSize){
cache.put(id, new CachedBitmap(bitmap, sampleSize));
}
public void clear() {
cache.clear();
}
private static class CachedBitmap {
public SoftReference<Bitmap> softBitmap;
public int sampleSize;
public CachedBitmap(Bitmap bitmap, int sampleSize){
this.softBitmap = new SoftReference<Bitmap>(bitmap);
this.sampleSize = sampleSize;
}
}
}
}
public class FileCache {
private File cacheDir;
public FileCache(Context context){
//Find the dir to save cached images
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cacheDir=new File(context.getExternalCacheDir(),Consts.STORE_CACHE);
else
cacheDir=context.getCacheDir();
if(!cacheDir.exists())
cacheDir.mkdirs();
}
public File getFile(String url){
//I identify images by hashcode. Not a perfect solution, good for the demo.
String filename=String.valueOf(url.hashCode());
File f = new File(cacheDir, filename);
return f;
}
public void clear(){
File[] files=cacheDir.listFiles();
for(File f:files)
f.delete();
}
}
你可以这样调用它:
imageLoader.DisplayImage(url, holder.image);
关于android - 在 GridView 中显示图像的最佳方式是平滑滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10312167/
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
question的一些答案关于redirect_to让我想到了其他一些问题。基本上,我正在使用Rails2.1编写博客应用程序。我一直在尝试自己完成大部分工作(因为我对Rails有所了解),但在需要时会引用Internet上的教程和引用资料。我设法让一个简单的博客正常运行,然后我尝试添加评论。靠我自己,我设法让它进入了可以从script/console添加评论的阶段,但我无法让表单正常工作。我遵循的其中一个教程建议在帖子Controller中创建一个“评论”操作,以添加评论。我的问题是:这是“标准”方式吗?我的另一个问题的答案之一似乎暗示应该有一个CommentsController参
目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi
在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList()Obt