I am trying scale an image of 1280x800 image to 1024x600 to fit it in Galaxy 7 inch tab. I am using function draw() from this tutorial.
in my XML file i declare an imageView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
in my java file :
public class MainActivity extends Activity {
TextView text;
Button mybutton;
Bitmap bitmap2;
ImageView iview;
int maxWidth ;
int maxHeight ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
Display display = getWindowManager().getDefaultDisplay();
Point size= new Point();
(display).getSize(size);
maxWidth = size.x;
maxHeight = size.y;
Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.drawable.main);
mybutton=(Button) findViewById(R.id.Send);
iview=(ImageView)findViewById(R.id.imageView1);
resizeImage1(bitmap);// set the image
}
public void resizeImage1(final Bitmap image)
{
float scaleFactor;
Bitmap resizedImage = null;
float screenAspect = (float)maxWidth / (float)maxHeight;
if (screenAspect <= 2)
{
scaleFactor = (float)(maxWidth )/ (float)(image.getWidth());
Toast.makeText(this, "i am here", Toast.LENGTH_LONG).show();
}
else
{
final int PLAYFIELD_HEIGHT = 10000;
scaleFactor = (float)maxHeight / (float)PLAYFIELD_HEIGHT;
}
int newWidth = (int)(image.getWidth() * scaleFactor);
int newHeight = (int)(image.getHeight() * scaleFactor);
int destX = (image.getWidth() - newWidth) / 2;
int destY = (image.getHeight() - newHeight) / 2;
resizedImage = Bitmap.createScaledBitmap( image, newWidth, newHeight, true);
iview.setMaxHeight(newHeight);
iview.setMaxWidth(newWidth);
iview.setImageBitmap(resizedImage);
iview.setVisibility(1);
}
}
Now before resizing when i try to get the image height and width it gives me 640x400 but in actual the image is of size 1280x800. After resizing it gives the blur image because the image is re size from 640x400 to 1024x600.i can't understand why it is happening.
Any help will be appreciated
After updating with new code:
public class MainActivity extends Activity {
ImageView iview;
int maxWidth ;
int maxHeight ;
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
Display display = getWindowManager().getDefaultDisplay();
Point size= new Point();
(display).getSize(size);
maxWidth = size.x;
maxHeight = size.y;
File photos= new File("C:/Users/drive3/workspace/images");
Bitmap bitmap = decodeFile(photos);
bitmap = Bitmap.createScaledBitmap(bitmap,150, 150, true);
}
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=40;
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;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
}
return super.onOptionsItemSelected(item);
}
}