Skip to content

Commit

Permalink
Fixed multiple images and layout
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonpolites committed Feb 20, 2012
1 parent 24c4d31 commit 1af2566
Showing 1 changed file with 39 additions and 19 deletions.
58 changes: 39 additions & 19 deletions main/src/com/polites/android/GestureImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class GestureImageView extends View {

private float rotation = 0.0f;

private int lastOrientation = -1;

private float centerX;
private float centerY;

Expand Down Expand Up @@ -72,25 +74,56 @@ public GestureImageView(Context context) {
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
setupCanvas(w, h, getResources().getConfiguration().orientation);
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int orientation = getResources().getConfiguration().orientation;

if(orientation == Configuration.ORIENTATION_LANDSCAPE) {
displayHeight = MeasureSpec.getSize(heightMeasureSpec);
float ratio = (float) this.bitmap.getWidth() / (float) this.bitmap.getHeight();
displayWidth = Math.round( (float) displayHeight * ratio) ;
}
else {
displayWidth = MeasureSpec.getSize(widthMeasureSpec);
float ratio = (float) this.bitmap.getHeight() / (float) this.bitmap.getWidth();
displayHeight = Math.round( (float) displayWidth * ratio) ;
}

setMeasuredDimension(displayWidth, displayHeight);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if(changed) {
setupCanvas(displayWidth, displayHeight, getResources().getConfiguration().orientation);
}
}

protected void setupCanvas(int measuredWidth, int measuredHeight, int orientation) {

if(lastOrientation != orientation) {
layout = false;
lastOrientation = orientation;
}

if(bitmap != null && !layout) {

int imageWidth = this.bitmap.getWidth();
int imageHeight = this.bitmap.getHeight();

hWidth = ((float)imageWidth / 2.0f);
hHeight = ((float)imageHeight / 2.0f);

if(orientation == Configuration.ORIENTATION_LANDSCAPE) {
displayHeight = measuredHeight;

// Calc height based on width
float ratio = (float) this.bitmap.getWidth() / (float) imageHeight;

displayWidth = Math.round( (float) displayHeight * ratio) ;
displayWidth = Math.round( (float) displayHeight * ratio);

startingScale = (float) displayHeight / (float) imageHeight;
}
else {
displayWidth = measuredWidth;
Expand All @@ -99,24 +132,11 @@ protected void setupCanvas(int measuredWidth, int measuredHeight, int orientatio
float ratio = (float) this.bitmap.getHeight() / (float) imageWidth;

displayHeight = Math.round( (float) displayWidth * ratio) ;
}

hWidth = ((float)imageWidth / 2.0f);
hHeight = ((float)imageHeight / 2.0f);

float widthRatio = (float) imageWidth / (float) displayWidth;
float heightRatio = (float) imageHeight / (float) displayHeight;

if(widthRatio > heightRatio) {

startingScale = (float) displayWidth / (float) imageWidth;
}
else {
startingScale = (float) displayHeight / (float) imageHeight;
}

if(!layout) {
scaleAdjust = startingScale;
}
scaleAdjust = startingScale;

this.centerX = (float)measuredWidth / 2.0f;
this.centerY = (float)measuredHeight / 2.0f;
Expand Down

0 comments on commit 1af2566

Please sign in to comment.