Reputation: 1966
I want to know how to scale bitmap to screen height and width?
Can anyone pls tell me how to do this.
Thanks Monali
Upvotes: 24
Views: 90587
Reputation: 11107
Try this to Decode the Bitmap :
Where imagefilepath is the path name of image,it will be in String covert that to File by using
File photos= new File(imageFilePath);
Where photo is the File name of the Image,Now you set your height and width according t your requirements.
public void main(){
Bitmap bitmap = decodeFile(photo);
bitmap = Bitmap.createScaledBitmap(bitmap,150, 150, true);
imageView.setImageBitmap(bitmap);
}
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=70;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale++;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
Upvotes: 29
Reputation: 18356
You can create a scaledBitmap based on this code.
private Bitmap getScaledBitMapBaseOnScreenSize(Bitmap bitmapOriginal){
Bitmap scaledBitmap=null;
try {
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = bitmapOriginal.getWidth();
int height = bitmapOriginal.getHeight();
float scaleWidth = metrics.scaledDensity;
float scaleHeight = metrics.scaledDensity;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
scaledBitmap = Bitmap.createBitmap(bitmapOriginal, 0, 0, width, height, matrix, true);
}
catch (Exception e){
e.printStackTrace();
}
return scaledBitmap;
}
Method calling & set the scaled image to imageview
Bitmap scaleBitmap=getScaledBitMapBaseOnScreenSize(originalBitmapImage);
if(scaleBitmap!=null) {
imageView.setImageBitmap(scaleBitmap);
}
My xml code for imageview,
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="center"
/>
Upvotes: 0
Reputation: 10512
You can use Bitmap.createScaledBitmap()
, but make sure to use the correct conversion formula when you use it:
final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) (dps * scale + 0.5f);
In Bitmap.createScaledBitmap()
the units for the width and height are "pixels", while you should always set your size in density-independent pixels (dp units) as described here.
So, if you want to show your images maintaining density independence - i.e. your images will occupy the same portion of screen on any device - you need to use a code similar to the following:
Bitmap b = decodeScaledFile(f);
final float scale = mContext.getResources().getDisplayMetrics().density;
int p = (int) (SIZE_DP * scale + 0.5f);
Bitmap b2 = Bitmap.createScaledBitmap(b, p, p, true);
Upvotes: 9
Reputation: 27431
I had a similar challenge where I wanted to stretch the width to 100% of screen, but keep the width/height ratio intact. So I did this..
Create a ScalableImageView class that extends ImageView:
public class ScalableImageView extends ImageView {
public boolean isMeasured = true;
public ScalableImageView(Context context) {
super(context);
}
public ScalableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScalableImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try
{
Drawable drawable = getDrawable();
if (drawable == null)
{
setMeasuredDimension(0, 0);
}
else
{
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = width * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
setMeasuredDimension(width, height);
}
}
catch (Exception e)
{
isMeasured = false;
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
In the layout XML file, I have a placeholder for the image defined like this:
<ScalableImageView android:id="@+id/image1"
android:layout_centerHorizontal="true"
android:src="@drawable/cam_image_placeholder"
android:scaleType="fitCenter"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:adjustViewBounds="true"
android:visibility="invisible"
android:layout_marginBottom="6px">
</ScalableImageView>
And then load/set it like this:
ScalableImageView imgView = null;
imgView = (ScalableImageView)findViewById(imgViewResource);
imgView.setImageDrawable(myDrawable);
imgView.setVisibility(ScalableImageView.VISIBLE);
Upvotes: 15
Reputation:
first get the screen height and width:
Android: How to get screen dimensions
1: Get screen dimensions in pixels then
have a look at this:
Resizing a Bitmap
Upvotes: 2