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