Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

Commit

Permalink
fix(Android): image stretched instead of cropped
Browse files Browse the repository at this point in the history
Used the logic from RCTCameraView, but that only worked correctly in Landscape mode.
Added an orientation check and more logic for Portrait mode.
  • Loading branch information
waitopiggu committed Apr 14, 2018
1 parent 89ba732 commit 73eb5fd
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions android/src/main/java/org/reactnative/camera/RNCameraView.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,26 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
float width = right - left;
float height = bottom - top;
float ratio = getAspectRatio().toFloat();
int orientation = getResources().getConfiguration().orientation;
int correctHeight;
int correctWidth;
this.setBackgroundColor(Color.BLACK);
if (height / width > ratio) {
correctHeight = (int) (width * ratio);
correctWidth = (int) width;
if (orientation == android.content.res.Configuration.ORIENTATION_LANDSCAPE) {
if (ratio * height < width) {
correctHeight = (int) (width / ratio);
correctWidth = (int) width;
} else {
correctWidth = (int) (height * ratio);
correctHeight = (int) height;
}
} else {
correctHeight = (int) height;
correctWidth = (int) (height * ratio);
if (ratio * width > height) {
correctHeight = (int) (width * ratio);
correctWidth = (int) width;
} else {
correctWidth = (int) (height / ratio);
correctHeight = (int) height;
}
}
int paddingX = (int) ((width - correctWidth) / 2);
int paddingY = (int) ((height - correctHeight) / 2);
Expand Down

0 comments on commit 73eb5fd

Please sign in to comment.