Java tutorial
//package com.java2s; /* * Copyright [2013] [Nazmul Idris] * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.content.*; import android.graphics.*; import android.graphics.drawable.*; public class Main { /** simple resizing of the given image to the desired width/height */ public static Bitmap getBitmapFromResource(Context ctx, int id, int x1, int y1) { BitmapDrawable bd = (BitmapDrawable) ctx.getResources().getDrawable(id); Bitmap b = bd.getBitmap(); int x = b.getWidth(); int y = b.getHeight(); float scaleX = (float) x1 / x; float scaleY = (float) y1 / y; float scale = 1f; boolean scaleXInBounds = (scaleX * x) <= x1 && (scaleX * y) <= y1; boolean scaleYInBounds = (scaleY * x) <= x1 && (scaleY * y) <= y1; if (scaleXInBounds && scaleYInBounds) { scale = (scaleX > scaleY) ? scaleX : scaleY; } else if (scaleXInBounds) { scale = scaleX; } else if (scaleYInBounds) { scale = scaleY; } return Bitmap.createScaledBitmap(b, (int) (scale * x), (int) (scale * y), true); } }