2012-04-02 2 views
1

갤러리보기와 이미지보기가 포함 된 간단한 응용 프로그램을 개발하고 이미지보기를 가볍게 두드리면 이미지가 배경 화면으로 설정됩니다.배경 무늬 선택 자습서?

여러 자습서를 시도했지만 사용하려는 이미지가 매우 커서 항상 항상 OOM 예외가 발생합니다. 내가 그들을 설정하기 전에 그들을 bitmapfactory 스케일링 시도했지만 제대로 작동하도록 얻을 수 없습니다. 내가 지금까지 가지고있는 것은 아래에있다. 이 일이 그렇게 힘들지 않다고 생각했습니다. 다음에해야 할 일을 누군가가 결정할 수있게 도와 줄 수 있습니까?

감사합니다.

package org.androidpeople.gallery; 

import java.io.IOException; 
import java.io.InputStream; 


import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.res.TypedArray; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.ImageView; 
import android.widget.AdapterView.OnItemClickListener; 

public class GalleryExample extends Activity { 

private Gallery gallery; 
private ImageView imgView; 
int position; 
private Integer[] Imgid = { R.drawable.hm001, R.drawable.hm002, R.drawable.hm003, 
     R.drawable.hm004, R.drawable.hm005, R.drawable.hm006,    R.drawable.hm007 }; 

/* (non-Javadoc) 
* @see android.app.Activity#onCreate(android.os.Bundle) 
*/ 
@Override 
public void onCreate(Bundle home) { 
    super.onCreate(home); 
    setContentView(R.layout.main); 
    position = 0; 
    imgView = (ImageView) findViewById(R.id.ImageView01); 
    imgView.setImageResource(Imgid[0]); 
    gallery = (Gallery) findViewById(R.id.examplegallery); 
    gallery.setAdapter(new AddImgAdp(this)); 
    Bitmap bm; 
bm = Bitmap.createScaledBitmap(bm, 213, 189, true); 





    gallery.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView parent, View v, int position, 
       long id) { 

      imgView.setImageResource(Imgid[position]); 
      GalleryExample.this.position = position; 
     } 
    }); 


    imgView.setOnLongClickListener(new View.OnLongClickListener() { 
     public boolean onLongClick(View v) { 

      AlertDialog alertDialog = new AlertDialog.Builder(
        GalleryExample.this).create(); 
      alertDialog.setTitle("Confirmation"); 
      alertDialog 
        .setMessage("Do you want to set this image as wallaper?"); 
      alertDialog.setButton("Yes", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, 
           int which) { 

          Bitmap bitmap = BitmapFactory.decodeResource(
            getResources(), Imgid[position]); 
          try { 
           GalleryExample.this.setWallpaper(bitmap); 
          } catch (IOException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 
          Log.d("Gallery Example", "Image setted."); 

         } 
        }); 

      alertDialog.show(); 
      return true; 
     } 
    }); 

} 

public class AddImgAdp extends BaseAdapter { 
    int GalItemBg; 
    private Context cont; 

    public AddImgAdp(Context c) { 
     cont = c; 
     TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme); 
     GalItemBg = typArray.getResourceId(
       R.styleable.GalleryTheme_android_galleryItemBackground, 0); 
     typArray.recycle(); 
    } 

    public int getCount() { 
     return Imgid.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 imgView = new ImageView(cont); 

     imgView.setImageResource(Imgid[position]); 
     imgView.setLayoutParams(new Gallery.LayoutParams(80, 70)); 
     imgView.setScaleType(ImageView.ScaleType.FIT_XY); 
     imgView.setBackgroundResource(GalItemBg); 

     return imgView; 
    } 
    Bitmap ShrinkBitmap(Resource, int width, int height){ 

     BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 
      bmpFactoryOptions.inJustDecodeBounds = true; 
      Bitmap bitmap = BitmapFactory.decodeResource(null, String, bmpFactoryOptions); 

      int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height); 
      int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width); 

      if (heightRatio > 1 || widthRatio > 1) 
      { 
      if (heightRatio > widthRatio) 
      { 
       bmpFactoryOptions.inSampleSize = heightRatio; 
      } else { 
       bmpFactoryOptions.inSampleSize = widthRatio; 
      } 
      } 

      bmpFactoryOptions.inJustDecodeBounds = false; 
      bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions); 
     return bitmap; 
     } 
    } 
} 

}

답변

0

그냥 내가 마지막보기는해야 의미가

public void onClick(DialogInterface dialog, int which) { 
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), Imgid[position]); 

아래 bm = Bitmap.createScaledBitmap(bm, 213, 189, true);을 넣어;

public void onClick(DialogInterface dialog, int which) { 
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), Imgid[position]); 
    bitmap = Bitmap.createScaledBitmap(bm, 213, 189, true); 
관련 문제