Show Camera image preview after click picture - android

I am making a custom camera like whats app. When i open the custom camera it show me screen same as wats app.
When i click the image, I need to show the preview like whats app in given below image.i does not know what mechanism WHATS App use to show the preview
*Note - Does whats app use a image view to set the bit map for showing picture preview or some other sophisticated way.

I am just giving you a working idea what WhatsApp doing in background in capture.
Result of answer, Change layout design as preferred, my design is basic.
camera_preview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/imageView"
android:layout_weight="1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_gravity="center_horizontal"
android:hint="Add a caption...."
android:textColorHint="#999999"
android:background="#333333"
android:padding="10dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/cancel"
android:layout_weight="1"
android:textColor="#ffffff" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Okay"
android:id="#+id/okay"
android:layout_weight="1"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>
and class Preview.java as
public class Preview extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_preview);
ImageView image=(ImageView)findViewById(R.id.imageView);
Intent data=getIntent();
File imgFile = new File(data.getStringExtra("path"));
//Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenWidth = size.x;
int screenHeight = size.y;
// Get target image size
Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
int bitmapHeight = bitmap.getHeight();
int bitmapWidth = bitmap.getWidth();
// Scale the image down to fit perfectly into the screen
// The value must be adjusted for phone/tables displays
while (bitmapHeight > (screenHeight ) && bitmapWidth > (screenWidth)) {
bitmapHeight = bitmapHeight / 2;
bitmapWidth = bitmapWidth / 2;
}
// Create resized bitmap image
BitmapDrawable resizedBitmap = new BitmapDrawable(getResources(),
Bitmap.createScaledBitmap(bitmap, bitmapWidth, bitmapHeight,
false));
image.setImageDrawable(resizedBitmap);
}
public void onCancelClick(View v){
}
public void onOkayClick(View v){
}
From your main activity startCamera by
public void startCamera(){
File file = new File(path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 2);
}
declare String path as global veriable
Inside onCreate method
path = Environment.getExternalStorageDirectory() + "/example.jpg";
I am starting activity for result Instead of startring activity you
can just shows a dialog,
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2 && resultCode == RESULT_OK) {
Intent startPreview = new Intent(this, Preview.class);
startPreview.putExtra("path", path);
startActivity(startPreview);
}
super.onActivityResult(requestCode, resultCode, data);
}

Related

Taking 3 photos with StartActivityForResults

I would appreciate some help, I want to take three photos under the same Intent with startActivityForResults() within a loop, and getting back onActivityResult () which prints those three photos in three imageviews, with MarshMallow, and its specific permissions (I think this is not the issue and it should be solved in my code),
The issue is that the application takes those three pictures, and makes the ArrayList or URIs, but it does not arrive to actually printing those three photos in the three imageviews,
Here is my code, thanks in advance,
-----------------
MainActivity.java
-----------------
public class MainActivity extends AppCompatActivity {
//private int numFotos = 1;
//private Bitmap bitmap;
private ArrayList <Uri> uriFiles;
private ImageView imageView1, imageView2,imageView3;
private Button button_takePics;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_takePics = (Button) findViewById(R.id.Butt_tiraFotos);
imageView1 = (ImageView) findViewById(R.id.image_1);
imageView2 = (ImageView) findViewById(R.id.image_2);
imageView3 = (ImageView) findViewById(R.id.image_3);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
button_takePics.setEnabled(false);
ActivityCompat.requestPermissions(this, new String[]
{ Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == 0) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
button_takePics.setEnabled(true);
}
}
}
public void takePictures(View view){
uriFiles = new ArrayList<>();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
for (int i=1; i<4; i++){
uriFiles.add(Uri.fromFile(getOutputMediaFile()));
intent.putParcelableArrayListExtra(MediaStore.EXTRA_OUTPUT,uriFiles);
startActivityForResult(intent, 100);
}
}
private static File getOutputMediaFile() {
File mediaStorageDir=null;
File formattedFile = null;
mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "CameraDemo");
if (!mediaStorageDir.exists()){
if (!mediaStorageDir.mkdirs()){
Log.d("CameraDemo", "failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
formattedFile= new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
return formattedFile;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100) {
if (resultCode == RESULT_OK) {
uriFiles = data.getParcelableArrayListExtra(MediaStore.EXTRA_OUTPUT);
// AQUI EMPIEZA EL PROBLEMA ////////
imageView1.setImageURI(uriFiles.get(0));
imageView2.setImageURI(uriFiles.get(1));
imageView3.setImageURI(uriFiles.get(2));
}
}
}
}
-------------------
activity_main.xml
-------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.faustocheca.photochooseshare.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="18dp"
android:layout_marginTop="18dp"
android:textSize="18sp"
android:text="Tira, escoge y comparte Fotos" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="Tira 3 Fotos"
android:id="#+id/Butt_tiraFotos"
android:onClick="takePictures"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="48dp"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/image_1"
android:layout_weight="1"
android:layout_width="80dp"
android:layout_height="120dp"
android:layout_margin="1dp"
android:background="#feafea"/>
<ImageView
android:id="#+id/image_2"
android:layout_weight="1"
android:layout_width="80dp"
android:layout_height="120dp"
android:layout_margin="1dp"
android:background="#feafea"/>
<ImageView
android:id="#+id/image_3"
android:layout_weight="1"
android:layout_width="80dp"
android:layout_height="120dp"
android:layout_margin="1dp"
android:background="#feafea"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="Comparte"
android:id="#+id/Butt_comparte"
/>
</LinearLayout>
This will not work; a single startActivityForResult will result in a single call to onActivityResult.
There's no contract in the ACTION_IMAGE_CAPTURE documentation or elsewhere in the system that says that you can issue it multiple times and get one result with 3 URIs.
You'll have to loop it so that you fire one intent, wait for the result, fire the second intent, wait for the result, etc.
You may be able to fire the 3 intents in a loop like this, but I'm not sure I'd trust that the responding camera application would actually be invoked 3 times. But even so, you'll receive 3 separate onActivityResult calls, if that works.

Camera opens on clicking anywhere rather than on button

Image for button 500x500.
I have created an ImageButton and when ImageButton is clicked the system camera should launch and the image brought from the system's camera app will be shown in the ImageView. But, the problem is: the button doesn't work here. Other than button if I click anywhere in the app the system camera launches but in case of button click the camera doesn't launch. What kind of error is causing this unexpected behaviour?
Java File:
public class Home extends AppCompatActivity {
static final int REQUEST_IMAGE_CAPTURE = 1;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// CAMERA BUTTON IMPLEMENTATION
ImageButton cameraButton = (ImageButton)findViewById(R.id.imageButton);
imageView = (ImageView)findViewById(R.id.imageView);
/* Disable the button if the user doesn't have camera */
if(!hasCamera())
cameraButton.setEnabled(false);
// CAMERA BUTTON IMPLEMENTATION
}
// Check if the user has a camera
private boolean hasCamera(){
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}
// Launching the camera
public void launchCamera(View view){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Take a picture and pass results along to onActivityResult
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
// If you want to return the image taken
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
//Get the photo
Bundle extras = data.getExtras();
Bitmap photo = (Bitmap) extras.get("data");
imageView.setImageBitmap(photo);
}
}
}
XML FILE:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.muchmore.www.chasquido.Home"
android:background="#drawable/home_background"
android:id="#+id/home_activity"
android:onClick="launchCamera">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome User!!!"
android:id="#+id/welcome_tag"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#000"
/>
<ImageButton
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/imageButton"
android:background="#drawable/camera_button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:minHeight="300dp"
android:minWidth="300dp"
android:layout_above="#+id/imageButton"
android:layout_centerHorizontal="true" />
</RelativeLayout>
You have assigned OnClickListener android:onClick="launchCamera" to your RelativeLayout (your root view) instead to your ImageButton.

Crop image to square - Android

How can I cut rectangular image (600 x 300) from left and right to fit in square ImageView ? I don't want to resize image, I just want to crop it, to be 300 x 300.
[SOLUTION]
As #blackbelt said
Bitmap cropImg = Bitmap.createBitmap(src, startX, startY, dstWidth, dstHeight);
is great for cropping images. So how can you automatically crop images with different sizes. I create this simple code for that:
// From drawable
Bitmap src= BitmapFactory.decodeResource(context.getResources(), R.drawable.image);
// From URL
Bitmap src = null;
try {
String URL = "http://www.example.com/image.jpg";
InputStream in = new java.net.URL(URL).openStream();
src = BitmapFactory.decodeStream(in);
} catch (Exception e) {
e.printStackTrace();
}
int width = src.getWidth();
int height = src.getHeight();
int crop = (width - height) / 2;
Bitmap cropImg = Bitmap.createBitmap(src, crop, 0, height, height);
ImageView.setImageBitmap(cropImg);
Expanding a little on the answer above
Since in some situations you can end up with an exception or not the expected result, just by using the Bitmap.createBitmap(), like the fallowing:
java.lang.IllegalArgumentException: x + width must be <=
bitmap.width()
Heres is a small function that does the crop and handle some of the commons cases.
Edit: updated accordantly to droidster's claim.
public static Bitmap cropToSquare(Bitmap bitmap){
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int newWidth = (height > width) ? width : height;
int newHeight = (height > width)? height - ( height - width) : height;
int cropW = (width - height) / 2;
cropW = (cropW < 0)? 0: cropW;
int cropH = (height - width) / 2;
cropH = (cropH < 0)? 0: cropH;
Bitmap cropImg = Bitmap.createBitmap(bitmap, cropW, cropH, newWidth, newHeight);
return cropImg;
}
I did several testing with some images of different resolutions and sizes and it work as expected.
It can also be used in other situations, for example when you are trying to make a "perfectly" round image and need to pass a squarish bitmap, etc.
set fixed image view height, width, and set two properties to image view
android:adjustViewBounds="true"
android:scaleType="centerCrop"
done
You can use
Bitmap dst = Bitmap.createBitmap(src, startX, startY, dstWidth, dstHeight);
from the documentation:
Returns an immutable bitmap from the specified subset of the source
bitmap. The new bitmap may be the same object as source, or a copy may
have been made. It is initialized with the same density as the
original bitmap.
Here you can find the documentation
Now xml is having properties like
custom:cropAspectRatioX="2"
custom:cropAspectRatioY="1"
Make both 1 if u want square cropping. now it is of rectangle
Add activity CropActivity
package agropost.post.agro.com.agropost.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.widget.Button;
import com.theartofdev.edmodo.cropper.CropImageView;
import agropost.post.agro.com.agropost.R;
import agropost.post.agro.com.agropost.Utility.Constants;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class CropActivity extends AppCompatActivity {
public static boolean isCrop = false;
#BindView(R.id.img_crop)
CropImageView imgCrop;
#BindView(R.id.btn_done)
Button btnDone;
#BindView(R.id.btn_cancel)
Button btnCancel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop);
ButterKnife.bind(this);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
width = width - 80;
imgCrop.getLayoutParams().height = width;
imgCrop.getLayoutParams().width = width;
imgCrop.setBackground(null);
imgCrop.setScaleType(CropImageView.ScaleType.FIT_CENTER);
imgCrop.setImageBitmap(Constants.mDashboardActivity.thumbnail_r);
}
#OnClick(R.id.btn_done)
public void onViewClicked() {
isCrop = true;
Intent returnIntent = new Intent();
Constants.mDashboardActivity.thumbnail_r = imgCrop.getCroppedImage();
setResult(3, returnIntent);
finish();
}
#OnClick(R.id.btn_cancel)
public void onViewClickedCancel() {
byte[] byteArray = getIntent().getByteArrayExtra("default");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
Constants.mDashboardActivity.thumbnail_r = bmp;
isCrop = true;
Intent returnIntent = new Intent();
setResult(3, returnIntent);
finish();
}
#Override
public void onBackPressed() {
// super.onBackPressed();
}
}
xml of activity..............
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparent"
android:gravity="center"
android:orientation="vertical"
tools:context=".Activity.CropActivity">
<com.theartofdev.edmodo.cropper.CropImageView xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="#+id/img_crop"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/drawer_bg"
android:scaleType="centerInside"
custom:cropAspectRatioX="2"
custom:cropAspectRatioY="1"
custom:cropFixAspectRatio="true"
custom:cropShape="rectangle" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/btn_done"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="#dimen/margin_40"
android:layout_marginRight="#dimen/margin_20"
android:layout_marginTop="#dimen/margin_20"
android:layout_weight="1"
android:background="#drawable/btn_bg_green_rounded"
android:text="Done"
android:textColor="#color/colorWhite"
android:textSize="#dimen/fontsize_normal" />
<Button
android:id="#+id/btn_cancel"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="#dimen/margin_20"
android:layout_marginRight="#dimen/margin_40"
android:layout_marginTop="#dimen/margin_20"
android:layout_weight="1"
android:background="#drawable/btn_bg_green_rounded"
android:text="Cancel"
android:textColor="#color/colorWhite"
android:textSize="#dimen/fontsize_normal"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
add dependecy
implementation 'com.theartofdev.edmodo:android-image-cropper:2.4.+'

Android Progress bar on ImageView

I am developing an app in which user will take Picture from camera and display it on preview screen having image view.
**CameraCapture.java**
class ButtonClickHandler implements View.OnClickListener
{
public void onClick( View view ){
myVib.vibrate(50);
startCameraActivity();
}
}
protected void startCameraActivity()
{
File filenew = new File( _path );
Uri outputFileUri = Uri.fromFile( filenew );
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivityForResult( intent, 0 );
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch( resultCode )
{
case 0:
break;
case -1:
//pictaken++;
onPhotoTaken();
break;
}
}
protected void onPhotoTaken()
{
//pictaken=true;
Intent i = new Intent(CameraCapture.this,Preview.class);
startActivity(i);
}
In my Preview class the captured picture is displayed on ImageView
**Preview.java**
File imgFile = new File("/sdcard/DCIM/Camera/test.jpg");
if(imgFile.exists()){
// Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.image);
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
Matrix mat = new Matrix();
String degree="90";
mat.postRotate(Integer.parseInt(degree));
Bitmap bMapRotate = Bitmap.createBitmap(myBitmap, 0, 0,myBitmap.getWidth(),myBitmap.getHeight(), mat, true);
myImage.setImageBitmap(bMapRotate);
//myImage.setImageBitmap(myBitmap);
}
_image = ( ImageView ) findViewById( R.id.image );
Is there is any way to show progress bar on the ImageView till it load the Captured image from SD card.
Thnx in advance :)
Put your ImageView and Progressbar in a RelativeLayout. For your ProgressBar, use:
android:layout_centerInParent="true"
Which will center it in the RelativeLayout, meaning over the ImageView. You might also want to hide the ProgressBar when the image has loaded:
progressBar.setVisibility(View.Gone)
when the image has been loaded.
Sample code:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"/>
<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible"/>
</RelativeLayout>
This is my solution. It is a little bit different. You can use it to create intro view with an image and progress bar. Here, progress bar is at 150dp to the bottom of center. FrameLayout can also be used.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/intro_description"
android:scaleType="fitXY"
android:src="#drawable/intro" />
<ProgressBar
android:id="#+id/progress"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="150dp"
android:visibility="visible" />
</FrameLayout>

How to fill the image within ImageView by maintaining aspect ratio?

I saw many questions on this same topic and tried by using
android:adjustViewBounds="true"
android:scaleType="fitCenter"
still my image is displaying as it is.If I change the
android:scaleType="fitCenter"
to
fitXY working fine.But as per document this one not maintains aspect ratio.So How can I change the code to work as expected?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/card"
android:layout_marginTop="25dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/card" >
<ImageView
android:id="#+id/idImage"
android:layout_width="fill_parent"
android:layout_height="110dp"
android:layout_margin="10dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/783454" />
</LinearLayout>
</LinearLayout>
![enter image description here][1]
![enter image description here][2]
Where iam missing the concept?
Please look at this code, this is how I was scaling bitmaps to fit screen properly. Maybe it will be helpful and give you ideas regarding to your task.
private void loadImage() {
ImageView imageView = (ImageView)findViewById(R.id.imageView);
Bitmap imageBitmap = ... load original image bitmap;
Bitmap scaledBitmap = imageBitmap;
// Scaling
int imgSrcHeight = imageBitmap.getHeight();
int imgSrcWidth = imageBitmap.getWidth();
int scaledHeight = 0;
int scaledWidth = 0;
int ctnrHeight = imageView.getMeasuredHeight();
int ctnrWidth = imageView.getMeasuredWidth();
int mHeight = imgSrcHeight - ctnrHeight;
int mWidth = imgSrcWidth - ctnrWidth;
if(mHeight > 0 && mWidth > 0)
{
if(mHeight > mWidth)
{
// scale to fit height
if(mHeight > 0)
{
scaledHeight = ctnrHeight;
// if height < 0 it means it's already inside of content
int coefOverhight = (ctnrHeight * 100)/imgSrcHeight;
scaledWidth = (int)(imgSrcWidth * ((coefOverhight)/100.0));
}
}
else
{
// scale to fit width
if(mWidth > 0)
{
scaledWidth = ctnrWidth;
int coefOverwidth = (ctnrWidth * 100)/imgSrcWidth;
scaledHeight = (int)(imgSrcHeight * ((coefOverwidth)/100.0));
}
}
}
else
{
scaledHeight = imgSrcHeight;
scaledWidth = imgSrcWidth;
}
scaledBitmap = Bitmap.createScaledBitmap(imageBitmap, scaledWidth, scaledHeight, true);
imageView.setImageBitmap(scaledBitmap);
}

Categories

Resources