0
我如何優化當前的廚房視圖,因爲它使用大量內存,導致它滯後。我不知道用什麼來幫助防止滯後?無論如何,我會發布與它相關的所有代碼。 (我的圖片是每個約200kb,是.jpg格式)任何幫助非常感謝。圖庫優化(Android)
畫廊活動:
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"/>
謝謝:)你還有什麼問題可以想的? – CarbonAssassin 2012-04-26 20:28:25
@CarbonAssassin如果可以的話,你也可以嘗試縮小圖像尺寸(這個'WILL'幫助)。除此之外,你可以做的並不多。你目前的設置應該讓畫廊順利滾動,我不認爲你目前的活動App2Activity使用更多的1.5MB的內存。 – Luksprog 2012-04-26 20:37:42