'android ImageView resize'에 해당되는 글 1건

  1. 2013.07.05 [Android] - 안드로이드 ImageView 리사이징 함수

[Android] - 안드로이드 ImageView 리사이징 함수

Android Java 2013. 7. 5. 11:23
반응형

public static Context context;

public static ImageView bg_mic;

public static final int ratioWidth  = 768;   // 이미지 제작시 작업한 가로 사이즈 (px)

public static final int ratioHeight = 1024; // 이미지 제작시 작업한 세로 사이즈 (px)


@Override

protected void onCreate(Bundle savedInstanceState) 

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main); 


context = this;


bg_mic = (ImageView) findViewById(R.id.bg_mic);

}


@Override

public void onWindowFocusChanged(boolean hasFocus) 

{

super.onWindowFocusChanged(hasFocus);

if (hasFocus == true) 

{

resizeImg(btn_mic, R.drawable.btn_mic_off_press, 0 , 1112);

}

}


public static void resizeImg(ImageView imgView, int id, int X, int Y)

{

BitmapFactory.Options options = new BitmapFactory.Options();

options.inJustDecodeBounds = true;

BitmapFactory.decodeResource(context.getResources(), id, options);

double imgWidth  = options.outWidth;

double imgHeight = options.outHeight;

double widthRat = (double) ratioWidth / imgWidth;

double HeightRat = (double) ratioWidth / imgHeight;

double XRate = (double) ratioWidth / X;

double YRate = (double) ratioWidth / Y;

int newX = (int) Math.ceil(DeviceWidth / XRate);

int newY = (int) Math.ceil(DeviceWidth / YRate);

int newImgWidth  = (int) Math.ceil(DeviceWidth / widthRat);

int newImgHeight = (int) Math.ceil(DeviceWidth / HeightRat);

if( ( newImgHeight + newY ) > DeviceHeight )

{

newY = DeviceHeight - newImgHeight;

}

if( ( newImgWidth + newX ) > DeviceWidth )

{

newX = DeviceWidth - newImgWidth;

}

Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), id);

Bitmap bm  = Bitmap.createScaledBitmap(bmp, newImgWidth, newImgHeight, true);


Math.ceil(DeviceWidth / YRate));

imgView.setImageBitmap(bm);

if(X > 0)

imgView.setX( newX );

if(Y > 0)

imgView.setY( newY );

}


* 이미지 제작시 기준잡았던 사이즈를 기준으로 X, Y 픽셀을 입력하시면됩니다.

직접 만든거라서 오류가 발생할수도 있습니다. ( 참고로 안드로이드 한지 한달째 입니다... )


반응형
: