2011-10-28 55 views
0

我正在製作一個需要使用Gallery的應用程序。我的問題是,Gallery真的很波濤洶涌。我的代碼非常類似於android提供的示例代碼,但不是僅僅讓ImageAdapter返回ImageView,而是讓它返回LinearLayout,因爲我需要圖像下方的文本。有什麼建議麼?Xperia Arc中的歪曲畫廊

這裏是我的代碼:

package org.example.gallery; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

public class GalleryTestActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Gallery gallery = (Gallery) this.findViewById(R.id.gallery); 
    gallery.setAdapter(new ItemAdapter(this)); 

    gallery.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, 
      int position, long id) { 
     Toast.makeText(GalleryTestActivity.this, "" + position, 
      Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    } 

    public class ItemAdapter extends BaseAdapter { 
    private Context mContext; 

    private final Integer[] mImageIds = { R.drawable.sample_1, 
     R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, 
     R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, 
     R.drawable.sample_8 }; 

    private final String[] mStringIds = { "1-pc. Chicken with Rice", 
     "2-pc. Chicken with Rice", "Tower Burger", "Bucket", "Barrel", 
     "Chicken Fillet", "Chicken burger", "Another Chicken Burger" }; 

    public ItemAdapter(Context c) { 
     this.mContext = c; 
    } 

    public int getCount() { 
     return mImageIds.length; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     LinearLayout l = new LinearLayout(this.mContext); 
     l.setOrientation(LinearLayout.VERTICAL); 

     ImageView iv = new ImageView(this.mContext); 
     iv.setImageResource(mImageIds[position]); 
     iv.setScaleType(ImageView.ScaleType.FIT_CENTER); 

     TextView tv = new TextView(this.mContext); 
     tv.setText(mStringIds[position]); 
     tv.setTextSize(20); 
     tv.setTextColor(Color.RED); 
     tv.setGravity(Gravity.CENTER_HORIZONTAL); 

     l.addView(iv); 
     l.addView(tv); 

     return l; 
    } 
    } 
} 
+0

這個圖像有多大(以像素爲單位)? – Selvin

+0

他們是'png'文件大約400x400px – cesar

+0

你可以將它們全部放在內存中嗎?考慮在#onResume()中預加載它們(然後釋放#onPause()中的Drawable:)。 – Jens

回答

1

使用ViewHolder(這樣你們就不會膨脹/在每個getView調用創建視圖(視圖回收))就像這個例子模式:

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

編輯:

 static class ViewHolder { 
      TextView tv; 
      ImageView iv; 
     } 

    public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder = null; 
     if(convertView == null){ 

     LinearLayout l = new LinearLayout(this.mContext); 
     l.setOrientation(LinearLayout.VERTICAL); 

     ImageView iv = new ImageView(this.mContext); 

     iv.setScaleType(ImageView.ScaleType.FIT_CENTER); 

     TextView tv = new TextView(this.mContext); 

     tv.setTextSize(20); 
     tv.setTextColor(Color.RED); 
     tv.setGravity(Gravity.CENTER_HORIZONTAL); 

     l.addView(iv); 
     l.addView(tv); 
     convertView = l; 
     holder = new ViewHolder(); 
     holder.tv = tv; 
     holder.iv = iv; 

     } else { 
     holder = (ViewHolder)convertView.getTag(); 
    } 
    holder.tv.setText(mStringIds[position]); 
    holder.iv.setImageResource(mImageIds[position]); 

     return convertView; 
    } 
+0

我做到了;我也爲它製作了一個xml佈局,但仍然不穩定。我覺得這很煩人,因爲啓動畫面很流暢,但無法處理8張圖像。我會嘗試使用更少的圖像。 – cesar