Question

How would i optimize the galley view as at the moment as it uses lots of memory which causes it to lag. I dont know what to use to help prevent lag? Anyway i'll post all my code related to it. (my picture are about 200kb each and are .jpg format) Any help much appreciated.

Gallery activity:

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"/>

Was it helpful?

Solution

Try to improve the getView() method of your adapter:

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top