Skip to content

Commit 61cba03

Browse files
committed
Added support for starting scale and position
1 parent ac06efb commit 61cba03

File tree

6 files changed

+98
-19
lines changed

6 files changed

+98
-19
lines changed

example/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121

2222

2323
<activity android:name="StandardImageProgrammaticWithOnClick"></activity>
24+
<activity android:name="StandardImageProgrammaticWithStartSettings"></activity>
2425
<activity android:name="StandardImageProgrammatic"></activity>
2526
<activity android:name="StandardImageXML"></activity>
27+
<activity android:name="StandardImageXMLWithStartSettings"></activity>
2628
<activity android:name="DoubleImage"></activity>
2729
<activity android:name="ScaleTypeCenter"></activity>
2830
<activity android:name="ScaleTypeCenterCrop"></activity>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:gesture-image="http://schemas.polites.com/android"
5+
android:id="@+id/layout"
6+
android:layout_width="fill_parent"
7+
android:layout_height="fill_parent"
8+
android:orientation="vertical"
9+
android:background="#000000">
10+
<com.polites.android.GestureImageView
11+
android:id="@+id/image"
12+
android:layout_width="fill_parent"
13+
android:layout_height="fill_parent"
14+
gesture-image:start-scale="2.0"
15+
gesture-image:start-x="1000"
16+
gesture-image:start-y="0"
17+
gesture-image:min-scale="0.75"
18+
gesture-image:max-scale="10.0"
19+
android:src="@drawable/image"/>
20+
</LinearLayout>

example/src/com/polites/android/example/Main.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public class Main extends ListActivity {
3838
"Single Image XML Layout",
3939
"Single Image Programmatic",
4040
"Single Image Programmatic with onClick event",
41+
"Single Image Programmatic with start scale & position",
42+
"Single Image XML Layout with start scale & position",
4143
"Double Image (With delayed load)",
4244
"ScaleType CENTER Large",
4345
"ScaleType CENTER_CROP Large",
@@ -57,6 +59,8 @@ public class Main extends ListActivity {
5759
StandardImageXML.class,
5860
StandardImageProgrammatic.class,
5961
StandardImageProgrammaticWithOnClick.class,
62+
StandardImageProgrammaticWithStartSettings.class,
63+
StandardImageXMLWithStartSettings.class,
6064
DoubleImage.class,
6165
ScaleTypeCenter.class,
6266
ScaleTypeCenterCrop.class,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.polites.android.example;
2+
3+
import android.os.Bundle;
4+
5+
public class StandardImageProgrammaticWithStartSettings extends StandardImageProgrammatic {
6+
@Override
7+
public void onCreate(Bundle savedInstanceState) {
8+
super.onCreate(savedInstanceState);
9+
view.setStartingScale(2.0f);
10+
view.setStartingPosition((float)view.getImageWidth() / 2.0f, (float)view.getImageHeight() / 2.0f);
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.polites.android.example;
2+
3+
import android.app.Activity;
4+
import android.os.Bundle;
5+
import android.view.Window;
6+
7+
public class StandardImageXMLWithStartSettings extends Activity {
8+
@Override
9+
public void onCreate(Bundle savedInstanceState) {
10+
super.onCreate(savedInstanceState);
11+
requestWindowFeature(Window.FEATURE_NO_TITLE);
12+
setContentView(R.layout.standard_image_with_settings);
13+
}
14+
}

main/src/com/polites/android/GestureImageView.java

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class GestureImageView extends ImageView {
5252
private boolean layout = false;
5353

5454
private float scaleAdjust = 1.0f;
55-
private float startingScale = 1.0f;
55+
private float startingScale = -1.0f;
5656

5757
private float scale = 1.0f;
5858
private float maxScale = 5.0f;
@@ -63,6 +63,8 @@ public class GestureImageView extends ImageView {
6363

6464
private float centerX;
6565
private float centerY;
66+
67+
private Float startX, startY;
6668

6769
private int hWidth;
6870
private int hHeight;
@@ -99,18 +101,30 @@ public GestureImageView(Context context, AttributeSet attrs) {
99101
setScaleType(ScaleType.CENTER_INSIDE);
100102
}
101103

104+
String strStartX = attrs.getAttributeValue(LOCAL_NS, "start-x");
105+
String strStartY = attrs.getAttributeValue(LOCAL_NS, "start-y");
106+
107+
if(strStartX != null && strStartX.trim().length() > 0) {
108+
startX = Float.parseFloat(strStartX);
109+
}
110+
111+
if(strStartY != null && strStartY.trim().length() > 0) {
112+
startY = Float.parseFloat(strStartY);
113+
}
114+
115+
setStartingScale(attrs.getAttributeFloatValue(LOCAL_NS, "start-scale", startingScale));
102116
setMinScale(attrs.getAttributeFloatValue(LOCAL_NS, "min-scale", minScale));
103117
setMaxScale(attrs.getAttributeFloatValue(LOCAL_NS, "max-scale", maxScale));
104118
setStrict(attrs.getAttributeBooleanValue(LOCAL_NS, "strict", strict));
105119
setRecycle(attrs.getAttributeBooleanValue(LOCAL_NS, "recycle", recycle));
106120

107-
initImage(false);
121+
initImage();
108122
}
109123

110124
public GestureImageView(Context context) {
111125
super(context);
112126
setScaleType(ScaleType.CENTER_INSIDE);
113-
initImage(false);
127+
initImage();
114128
}
115129

116130
@Override
@@ -174,15 +188,29 @@ protected void setupCanvas(int measuredWidth, int measuredHeight, int orientatio
174188
measuredHeight -= (getPaddingTop() + getPaddingBottom());
175189

176190
computeCropScale(imageWidth, imageHeight, measuredWidth, measuredHeight);
177-
computeStartingScale(imageWidth, imageHeight, measuredWidth, measuredHeight);
191+
192+
if(startingScale <= 0.0f) {
193+
computeStartingScale(imageWidth, imageHeight, measuredWidth, measuredHeight);
194+
}
178195

179196
scaleAdjust = startingScale;
180197

181198
this.centerX = (float) measuredWidth / 2.0f;
182199
this.centerY = (float) measuredHeight / 2.0f;
200+
201+
if(startX == null) {
202+
x = centerX;
203+
}
204+
else {
205+
x = startX;
206+
}
183207

184-
x = centerX;
185-
y = centerY;
208+
if(startY == null) {
209+
y = centerY;
210+
}
211+
else {
212+
y = startY;
213+
}
186214

187215
gestureImageViewTouchListener = new GestureImageViewTouchListener(this, measuredWidth, measuredHeight);
188216

@@ -338,7 +366,7 @@ protected void onDetachedFromWindow() {
338366
super.onDetachedFromWindow();
339367
}
340368

341-
protected void initImage(boolean original) {
369+
protected void initImage() {
342370
if(this.drawable != null) {
343371
this.drawable.setAlpha(alpha);
344372
this.drawable.setFilterBitmap(true);
@@ -347,32 +375,24 @@ protected void initImage(boolean original) {
347375
}
348376
}
349377

350-
if(!original) {
351-
layout = false;
378+
if(!layout) {
352379
requestLayout();
353380
redraw();
354381
}
355382
}
356383

357384
public void setImageBitmap(Bitmap image) {
358-
setImageBitmap(image, false);
359-
}
360-
361-
protected void setImageBitmap(Bitmap image, boolean original) {
362385
this.drawable = new BitmapDrawable(getResources(), image);
363-
initImage(original);
386+
initImage();
364387
}
365388

366389
@Override
367390
public void setImageDrawable(Drawable drawable) {
368391
this.drawable = drawable;
392+
initImage();
369393
}
370394

371395
public void setImageResource(int id) {
372-
setImageResource(id, false);
373-
}
374-
375-
protected void setImageResource(int id, boolean original) {
376396
if(this.drawable != null) {
377397
this.recycle();
378398
}
@@ -653,9 +673,16 @@ public boolean isLandscape() {
653673
public boolean isPortrait() {
654674
return getImageWidth() <= getImageHeight();
655675
}
656-
657676

677+
public void setStartingScale(float startingScale) {
678+
this.startingScale = startingScale;
679+
}
658680

681+
public void setStartingPosition(float x, float y) {
682+
this.startX = x;
683+
this.startY = y;
684+
}
685+
659686
@Override
660687
public void setOnClickListener(OnClickListener l) {
661688
this.onClickListener = l;

0 commit comments

Comments
 (0)