2012-04-26 13 views
0

ギャレービューを最適化する方法は、ギャラリービューを最適化する方法です。私は遅れを防ぐのを助けるために何を使うべきかを知らないのですか?とにかく私はそれに関連するすべてのコードを投稿します。 (私の絵はそれぞれ約200kbで、.jpg形式です)どんな助けでも大歓迎です。ギャラリーの最適化(Android)

ギャラリーの活動:あなたのアダプタのgetView()方法改善する

package kevin.erica.box; 
import kevin.erica.box.R.string; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.content.res.TypedArray; 
import android.os.Bundle; 
import android.util.DisplayMetrics; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup.MarginLayoutParams; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.Gallery; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.TextView; 
public class App2Activity extends Activity { 
/** Called when the activity is first created. */ 
Context context = this; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.gallery); 

    final Button ibgb = (Button) findViewById(R.id.backgallery); 
    Gallery g = (Gallery) findViewById(R.id.gallery1); 
    DisplayMetrics metrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(metrics); 
    MarginLayoutParams mlp = (MarginLayoutParams) g.getLayoutParams(); 
    mlp.setMargins((int) -(metrics.widthPixels/2), 
        mlp.topMargin, 
        mlp.rightMargin, 
        mlp.bottomMargin); 
    g.setAdapter(new ImageAdapter(this)); 
    ibgb.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(); 
      setResult(RESULT_OK, intent); 
      finish(); 
     } 
     }); 




    g.setOnItemClickListener(new OnItemClickListener() { 



     public void onItemClick(@SuppressWarnings("rawtypes") AdapterView parent, View v, int position, long id) { 
      ImageView imageview = (ImageView) findViewById(R.id.ImageView01); 
      imageview.setImageResource(mImageIds[position]); 




     } 

    }); 
} 



public Integer[] mImageIds = { 
     R.drawable.alexbox, 
     R.drawable.arm, 
     R.drawable.mirror, 
     R.drawable.penstache, 
     R.drawable.penstache1, 
     R.drawable.glasses, 
     R.drawable.hatface, 
     R.drawable.rubber, 
     R.drawable.scarfballs, 
     R.drawable.drawing, 
     R.drawable.eastercardalex, 
     R.drawable.extralifenote, 
     R.drawable.mug1 


}; 



public class ImageAdapter extends BaseAdapter { 
int mGalleryItemBackground; 
private Context mContext; 

public ImageAdapter(Context c) { 
    mContext = c; 
    TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery); 
    mGalleryItemBackground = a.getResourceId(
      R.styleable.HelloGallery_android_galleryItemBackground, 0); 
    a.recycle(); 
} 
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) { 
    ImageView i = new ImageView(mContext); 
    i.setImageResource(mImageIds[position]); 
    i.setLayoutParams(new Gallery.LayoutParams(170, 150)); 
    i.setScaleType(ImageView.ScaleType.FIT_XY); 
    i.setBackgroundResource(mGalleryItemBackground); 

    return i; 

} 
} 
} 

The.XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 



<Gallery 
    android:id="@+id/gallery1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" android:gravity="left"/> 





<ImageView 
    android:id="@+id/ImageView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/backgallery" 
    android:layout_below="@+id/gallery1" 
    android:layout_centerHorizontal="true" /> 

<Button 
    android:id="@+id/backgallery" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:text="Back" 
    android:clickable= "true" 
    android:onClick="gallerybackbutton"/> 

答えて

3

試してみてください。

public View getView(int position, View convertView, ViewGroup parent) { 
    ImageView v; 
    if (convertView == null) { 
     v = new ImageView(mContext); 
    } else { 
     v = (ImageView) convertView; 
    } 
    v.setImageResource(mImageIds[position]); 
    v.setLayoutParams(new Gallery.LayoutParams(170, 150)); 
    v.setScaleType(ImageView.ScaleType.FIT_XY); 
    v.setBackgroundResource(mGalleryItemBackground); 
    return v; 
} 
+0

を感謝:)他に何かます思うことができるの? – CarbonAssassin

+0

@CarbonAssassin可能であれば、画像サイズを縮小することもできます(この 'WILL'の助け)。それ以外はこれほどあなたができることはあまりありません。あなたの現在のセットアップは、ギャラリーがスムーズにスクロールできるようにするべきです、私はあなたの現在の活動 'App2Activity'が1.5MBのメモリをより多く使うとは思わない。 – Luksprog