7

わかりました、変更について本当に質問があると思います。Android 開発者ページhttp://developer.android.com/resources/tutorials/views/hello-gridview.htmlの指示に従って、Android に gridView を実装しました 。 、ビットマップが返されます。これはすべて、Galxy Note や Galaxy S2 などの高度な携帯電話でも、Galaxy ace などの高度でない携帯電話でも、2 年前の安っぽい htc でもうまく機能します。しかし、何らかの理由で、OutOfMemory の問題により、ICS を使用する Galaxy 3 でクラッシュします。グリッドビューの画像の約半分を使用すると、(少しゆっくりではありますが) 動作します。これは誰にとっても意味がありますか?

これは ImageAdapter の私の実装です:

public class ImageAdapter extends BaseAdapter {
private Context mContext;
private int mWidth;
private int mHeight;

public ImageAdapter(Context c, int width, int height) {
    mContext = c;
    mWidth = width;
    mHeight = height;
}

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

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

public long getItemId(int position) {
    return mThumbIds[position];
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(mWidth, mHeight));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.third_world , R.drawable.annoyinggirl,
        R.drawable.asianfather, R.drawable.awkawespeng,
        R.drawable.awkpeng, R.drawable.blankdad,
        R.drawable.collegesenior, R.drawable.courage,
        R.drawable.frog, R.drawable.frynotsure,
        R.drawable.goodguygreg, R.drawable.scumbag,
        R.drawable.raptor,R.drawable.keano,
        R.drawable.successkid, R.drawable.wonka,
        R.drawable.braceyourselves, R.drawable.onedoesnotsimply,
        R.drawable.firstworldproblem, R.drawable.amitheonlyone,
        R.drawable.badluckbrian, R.drawable.interestingman,
        R.drawable.toodamnhigh, R.drawable.def    
};

}

これは私のメインからの呼び出し関数です:

  private void changeToTemplateView(){
    setContentView(R.layout.templates);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(context, (int)(dstWidth * 1.4), (int)(dstHeight * 1.4)));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            Options opts = new Options();
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), (int) parent.getItemIdAtPosition(position), opts);

            //do stuff with bitmap          
        }
    });
}

これは、グリッドビューの xml です。

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gridview"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"

/>

ここで私が間違っているかもしれないことを誰かが知っているなら、私は永遠に感謝します

編集:クラッシュする前に、ログに「FimgApiStretch:stretch failed」というエラーが表示されます。また、グリッドビューでのスクロールは機能しません。これは、スクロールを引き起こすのに十分なサムネイルがない場合でも、アプリがクラッシュするという事実に関係ありません。

4

3 に答える 3

2

私の情報によると、属性値fill_parentはAndroid 2.2以降では廃止されているため、fill_parentをmatch_parentに置き換えてみてください。これが役立つことを願っています

このリンクを見ることができますhttp://developer.android.com/training/displaying-bitmaps/load-bitmap.html これがお役に立てば幸いです

于 2012-06-12T15:07:44.263 に答える
2

「なぜ劣悪な電話ではなく、Galaxy 3 でクラッシュするのか」という質問については、答えは「わかりません」です。しかし、グリッドビューを悪用していたことが判明しました。サムネイルと画像全体に同じ画像を使用することはお勧めできません。私がしたこと (これは回避策に過ぎないかもしれませんが、うまくいきます) は、小さい画像とフルサイズの画像の 2 セットを使用することです。これはこのように行われます

サムネイルを設定するには:

private Integer[] mThumbIds = {
        R.drawable.third_world__small , R.drawable.annoyinggirl__small,
        R.drawable.asianfather__small, R.drawable.awkawespeng__small,
        R.drawable.awkpeng__small, R.drawable.blankdad__small,
        R.drawable.collegesenior__small, R.drawable.courage__small,
        R.drawable.frog__small, R.drawable.frynotsure__small,
        R.drawable.goodguygreg__small, R.drawable.scumbag__small,
        R.drawable.raptor__small,R.drawable.keano__small,
        R.drawable.successkid__small, R.drawable.wonka__small,
        R.drawable.braceyourselves__small, R.drawable.onedoesnotsimply__small,
        R.drawable.firstworldproblem__small, R.drawable.amitheonlyone__small,
        R.drawable.badluckbrian__small, R.drawable.interestingman__small,
        R.drawable.toodamnhigh__small, R.drawable.def__small    
};

public View getView(int position, View convertView, ViewGroup parent) {
    //clearAllResources();
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(mWidth, mHeight));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

次に、別の配列を作成します。

private Map<Integer, Integer> mThumbIdToFullSizeId = new HashMap<Integer,Integer>();
public ImageAdapter(Context c, int width, int height) {
    mContext = c;
    mWidth = width;
    mHeight = height;
    mThumbIdToFullSizeId.put(R.drawable.third_world__small, R.drawable.third_world);
    mThumbIdToFullSizeId.put(R.drawable.annoyinggirl__small,R.drawable.annoyinggirl);
    mThumbIdToFullSizeId.put(R.drawable.asianfather__small, R.drawable.asianfather);
    mThumbIdToFullSizeId.put(R.drawable.awkawespeng__small, R.drawable.awkawespeng);
    mThumbIdToFullSizeId.put(R.drawable.awkpeng__small, R.drawable.awkpeng);
    mThumbIdToFullSizeId.put(R.drawable.blankdad__small, R.drawable.blankdad);
    mThumbIdToFullSizeId.put(R.drawable.collegesenior__small, R.drawable.collegesenior);
    mThumbIdToFullSizeId.put(R.drawable.courage__small, R.drawable.courage);
    mThumbIdToFullSizeId.put(R.drawable.frog__small, R.drawable.frog);
    mThumbIdToFullSizeId.put(R.drawable.frynotsure__small, R.drawable.frynotsure);
    mThumbIdToFullSizeId.put(R.drawable.goodguygreg__small, R.drawable.goodguygreg);
    mThumbIdToFullSizeId.put(R.drawable.scumbag__small, R.drawable.scumbag);
    mThumbIdToFullSizeId.put(R.drawable.raptor__small, R.drawable.raptor);
    mThumbIdToFullSizeId.put(R.drawable.keano__small, R.drawable.keano);
    mThumbIdToFullSizeId.put(R.drawable.successkid__small, R.drawable.successkid);
    mThumbIdToFullSizeId.put(R.drawable.wonka__small, R.drawable.wonka);
    mThumbIdToFullSizeId.put(R.drawable.braceyourselves__small, R.drawable.braceyourselves);
    mThumbIdToFullSizeId.put(R.drawable.onedoesnotsimply__small, R.drawable.onedoesnotsimply);
    mThumbIdToFullSizeId.put(R.drawable.firstworldproblem__small, R.drawable.firstworldproblem);
    mThumbIdToFullSizeId.put(R.drawable.amitheonlyone__small, R.drawable.amitheonlyone);
    mThumbIdToFullSizeId.put(R.drawable.badluckbrian__small, R.drawable.badluckbrian);
    mThumbIdToFullSizeId.put(R.drawable.interestingman__small, R.drawable.interestingman);
    mThumbIdToFullSizeId.put(R.drawable.toodamnhigh__small, R.drawable.toodamnhigh);
    mThumbIdToFullSizeId.put(R.drawable.def__small, R.drawable.def);
}

次に、getview 関数の場合:

public long getItemId(int position) {

    return mThumbIdToFullSizeId.get(mThumbIds[position]);
}

そして、すべてがよりメモリフレンドリーになります。Muhannad にチェックマークを付けて、 http: //developer.android.com/training/displaying-bitmaps/load-bitmap.htmlへのリンクを提供してくれました。

于 2012-06-16T16:41:28.603 に答える
1

同様の問題がありました。私のアプリは常に問題なく正常に動作しますが、Android 4 のデバイスではOutOfMemoryエラーでクラッシュし始めました。特に、GalleryView アダプターをロードするときにクラッシュします。公式ドキュメント (http://android.developer.com) の多くのページを読んだ後、メモリが古いバージョンの SO とは異なる方法で使用されていることを知りましたが、マニフェスト ファイルで作業する問題を完全に解決しました。Android 3.0 をプロジェクト ビルド ターゲットとして設定し、android:targetSdkVersion="15" を追加し、android:largeHeap="true" をアプリケーション タグに追加し、最後に supports-screens android:anyDensity="false" を追加しました。

これがあなたにも役立つことを願っています!

于 2012-08-11T22:41:58.870 に答える