0

大量のメモリを使用してラグが発生するため、現時点でギャレービューを最適化するにはどうすればよいですか。ラグを防ぐために何を使用すればよいかわかりませんか?とにかく、それに関連するすべてのコードを投稿します。(私の写真はそれぞれ約200kbで、.jpg形式です)どんな助けでも大歓迎です。

ギャラリーアクティビティ:

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

4

1 に答える 1

3

getView()アダプターの方法を改善してみてください。

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;
}
于 2012-04-26T20:12:49.280 に答える