0
我正在实现使用按钮打开的图像的网格视图。我面临的问题是关于打开gridview的滞后/时间延迟。下面是一些代码片段加载图像gridview时的Android性能下降
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class Gallery extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
ActionBar ab=getActionBar();
Resources r=getResources();
Drawable d=r.getDrawable(R.color.quotescolor);
ab.setBackgroundDrawable(d);
GridView gv=(GridView)findViewById(R.id.gridview);
gv.setAdapter(new ImageAdapter2(this));
final FragmentManager fm=getFragmentManager();
gv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
if(position==0)
{
ImageDial1 id1=new ImageDial1();
id1.show(fm,"image_title");
//Intent i=new Intent(Gallery.this,ImageFrag1.class);
//startActivity(i);
}
和图像我使用的适配器这样
package com.example.sachinapp;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter2 extends BaseAdapter {
private Context ctx;
public ImageAdapter2(Context c)
{
ctx=c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return pics.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView iv;
if (convertView == null) { // if it's not recycled, initialize some attributes
iv = new ImageView(ctx);
iv.setLayoutParams(new GridView.LayoutParams(150,150));
iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
iv.setPadding(8, 8, 8, 8);
} else {
iv = (ImageView) convertView;
}
iv.setImageResource(pics[position]);
return iv;
}
private Integer[] pics={
R.drawable.s1,R.drawable.s2,
R.drawable.s3,R.drawable.s4,
R.drawable.s6,R.drawable.s7,
R.drawable.s8,R.drawable.s9,
R.drawable.s10,R.drawable.s11,
R.drawable.s2,R.drawable.s13,
R.drawable.s4,R.drawable.s15,
R.drawable.s6,R.drawable.s7,
R.drawable.s18,R.drawable.s19,
};
}
,我也面临着通过GridView滚动时滞后。我如何提高性能并使gridview加载速度更快。我已经读过,我们可以使用异步任务,但在这种情况下我不太熟悉如何使用它。还有其他解决方案吗? 谢谢
drawables有多大?如果它们太大,你的imageview必须将它们缩小到150dp,那可能会损害性能。 – Patrick
它们都是800像素(宽)和1100像素(高)。这是否必须缩减很多才能达到150dp?如果是的话,什么规模会提高性能? –
这似乎不是一个微不足道的缩小。虽然android会缓存资源,但我不确定它缓存了重新缩放版本,但更有可能是原始版本。 现在想象一下,每当您滚动时,一旦有新元素滚动到屏幕中,就必须重新调整其中的多个元素。 如果可以,请在应用程序中以实际需要的大小提供资源。 (在这里,150dp)请参阅:http://developer.android.com/training/multiscreen/screendensities.html – Patrick