1 |
torben |
925 |
package dk.thoerup.side9; |
2 |
|
|
|
3 |
torben |
1185 |
|
4 |
torben |
925 |
import java.util.ArrayList; |
5 |
|
|
|
6 |
|
|
import android.app.Activity; |
7 |
torben |
926 |
import android.content.Intent; |
8 |
torben |
925 |
import android.graphics.Bitmap; |
9 |
|
|
import android.graphics.BitmapFactory; |
10 |
torben |
926 |
import android.net.Uri; |
11 |
torben |
925 |
import android.os.Bundle; |
12 |
|
|
import android.util.Log; |
13 |
torben |
926 |
import android.view.Menu; |
14 |
|
|
import android.view.MenuItem; |
15 |
torben |
925 |
import android.view.MotionEvent; |
16 |
|
|
import android.view.View; |
17 |
torben |
1217 |
import android.view.animation.AccelerateInterpolator; |
18 |
torben |
925 |
import android.widget.ImageView; |
19 |
|
|
import android.widget.TextView; |
20 |
torben |
1217 |
import dk.thoerup.side9.anim.DisplayNextView; |
21 |
|
|
import dk.thoerup.side9.anim.Flip3dAnimation; |
22 |
torben |
925 |
|
23 |
|
|
public class PictureView extends Activity { |
24 |
torben |
926 |
final static int CONTEXT_VIEWIMG = 1000; |
25 |
torben |
925 |
|
26 |
|
|
final static String TAG = "Side9Pigen"; |
27 |
|
|
|
28 |
|
|
TextView mDescription; |
29 |
|
|
TextView mCaption; |
30 |
torben |
1217 |
ImageView mImageView1; |
31 |
|
|
ImageView mImageView2; |
32 |
|
|
|
33 |
|
|
boolean isFirstImage = true; |
34 |
|
|
|
35 |
torben |
1187 |
ArrayList<ImageEntry> mImages; |
36 |
torben |
925 |
int mIndex; |
37 |
|
|
|
38 |
|
|
|
39 |
|
|
Bitmap mBitmap; |
40 |
|
|
|
41 |
|
|
@SuppressWarnings("unchecked") |
42 |
|
|
@Override |
43 |
|
|
public void onCreate(Bundle savedInstanceState) { |
44 |
|
|
super.onCreate(savedInstanceState); |
45 |
|
|
setContentView(R.layout.pictureview); |
46 |
|
|
|
47 |
torben |
1187 |
mImages = (ArrayList<ImageEntry>) getIntent().getSerializableExtra("images"); |
48 |
torben |
925 |
mIndex = getIntent().getIntExtra("index", 0); |
49 |
|
|
|
50 |
|
|
|
51 |
torben |
1217 |
mImageView1 = (ImageView) findViewById(R.id.imageview1); |
52 |
|
|
mImageView1.setOnTouchListener( new Touch() ); |
53 |
torben |
926 |
|
54 |
torben |
1217 |
mImageView2 = (ImageView) findViewById(R.id.imageview2); |
55 |
|
|
|
56 |
torben |
926 |
mCaption = (TextView) findViewById(R.id.caption); |
57 |
torben |
925 |
mDescription = (TextView) findViewById(R.id.description); |
58 |
|
|
|
59 |
torben |
926 |
|
60 |
torben |
1217 |
loadImage(true, true); |
61 |
torben |
925 |
|
62 |
|
|
} |
63 |
|
|
|
64 |
torben |
926 |
|
65 |
|
|
|
66 |
torben |
925 |
private void loadImage(int newIndex) { |
67 |
|
|
Log.e(TAG, "NewIndex " + newIndex); |
68 |
|
|
if (newIndex != mIndex) { |
69 |
torben |
1217 |
|
70 |
|
|
boolean isForward = (newIndex > mIndex); |
71 |
|
|
|
72 |
torben |
925 |
mIndex = newIndex; |
73 |
torben |
1217 |
loadImage(false,isForward); |
74 |
torben |
925 |
} |
75 |
|
|
} |
76 |
|
|
|
77 |
torben |
1217 |
private void loadImage(boolean firstLoad, boolean isForward) { |
78 |
torben |
1187 |
ImageEntry currentImage = mImages.get(mIndex); |
79 |
torben |
1185 |
mBitmap = BitmapFactory.decodeFile( currentImage.path ); |
80 |
torben |
1217 |
|
81 |
|
|
if (firstLoad) { |
82 |
|
|
mImageView1.setImageBitmap(mBitmap); |
83 |
|
|
isFirstImage = true; |
84 |
|
|
} else { |
85 |
|
|
|
86 |
|
|
int endAngle = isForward ? -90 : 90; |
87 |
|
|
|
88 |
|
|
if (isFirstImage) { |
89 |
|
|
mImageView2.setImageBitmap(mBitmap); |
90 |
|
|
applyRotation(0, endAngle, isForward); |
91 |
|
|
} else { |
92 |
|
|
mImageView1.setImageBitmap(mBitmap); |
93 |
|
|
applyRotation(0, endAngle, isForward); |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
|
97 |
|
|
|
98 |
|
|
isFirstImage = !isFirstImage; |
99 |
|
|
} |
100 |
torben |
1185 |
|
101 |
torben |
925 |
|
102 |
torben |
1185 |
if (currentImage.caption.length() > 0) { |
103 |
torben |
925 |
mCaption.setVisibility(View.VISIBLE); |
104 |
torben |
1185 |
mCaption.setText(currentImage.caption); |
105 |
torben |
925 |
} else { |
106 |
|
|
mCaption.setVisibility(View.INVISIBLE); |
107 |
|
|
} |
108 |
torben |
1185 |
|
109 |
|
|
String pathParts[] = currentImage.path.split("/"); |
110 |
|
|
String fileName = pathParts[ pathParts.length -1]; |
111 |
torben |
1187 |
String desc = "" + (mIndex +1) + "/" + mImages.size() + " - " + fileName; |
112 |
torben |
1185 |
mDescription.setText(desc); |
113 |
torben |
925 |
} |
114 |
torben |
1217 |
|
115 |
|
|
|
116 |
torben |
925 |
|
117 |
torben |
1217 |
private void applyRotation(float start, float end, boolean isForward) { |
118 |
|
|
// Find the center of image |
119 |
|
|
final float centerX = mImageView1.getWidth() / 2.0f; |
120 |
|
|
final float centerY = mImageView1.getHeight() / 2.0f; |
121 |
torben |
926 |
|
122 |
torben |
1217 |
// Create a new 3D rotation with the supplied parameter |
123 |
|
|
// The animation listener is used to trigger the next animation |
124 |
|
|
final Flip3dAnimation rotation = new Flip3dAnimation(start, end, centerX, centerY); |
125 |
|
|
rotation.setDuration(250); |
126 |
|
|
rotation.setFillAfter(true); |
127 |
|
|
rotation.setInterpolator(new AccelerateInterpolator()); |
128 |
|
|
rotation.setAnimationListener(new DisplayNextView(isFirstImage, isForward, mImageView1, mImageView2)); |
129 |
|
|
|
130 |
|
|
if (isFirstImage) |
131 |
|
|
{ |
132 |
|
|
mImageView1.startAnimation(rotation); |
133 |
|
|
} else { |
134 |
|
|
mImageView2.startAnimation(rotation); |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
|
140 |
|
|
|
141 |
torben |
925 |
|
142 |
torben |
926 |
@Override |
143 |
|
|
public boolean onCreateOptionsMenu(Menu menu) { |
144 |
|
|
MenuItem item = menu.add(Menu.NONE, CONTEXT_VIEWIMG, Menu.NONE, "Built-in viewer"); |
145 |
|
|
item.setIcon( android.R.drawable.ic_menu_gallery); |
146 |
|
|
return true; |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
|
|
@Override |
152 |
|
|
public boolean onOptionsItemSelected(MenuItem item) { |
153 |
|
|
boolean res; |
154 |
|
|
|
155 |
|
|
switch(item.getItemId()) { |
156 |
|
|
case CONTEXT_VIEWIMG: |
157 |
torben |
1187 |
String uri = "file://" + mImages.get(mIndex).path; |
158 |
torben |
926 |
|
159 |
|
|
Intent i = new Intent(Intent.ACTION_VIEW); |
160 |
|
|
i.setDataAndType(Uri.parse(uri), "image/jpeg"); |
161 |
|
|
startActivity(i); |
162 |
|
|
res = true; |
163 |
|
|
break; |
164 |
|
|
default: |
165 |
|
|
res = super.onOptionsItemSelected(item); |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
return res; |
169 |
|
|
} |
170 |
|
|
|
171 |
torben |
925 |
class Touch implements View.OnTouchListener { |
172 |
|
|
|
173 |
|
|
Float firstX = null; |
174 |
|
|
|
175 |
|
|
|
176 |
|
|
@Override |
177 |
|
|
public boolean onTouch(View v, MotionEvent event) { |
178 |
|
|
if (event.getAction() == MotionEvent.ACTION_DOWN) { |
179 |
|
|
firstX = event.getX(); |
180 |
|
|
} else { |
181 |
|
|
if (firstX != null) { |
182 |
|
|
float x = event.getX(); |
183 |
|
|
float diff = firstX - x; |
184 |
|
|
|
185 |
|
|
if (diff > 150) { |
186 |
torben |
1187 |
int newIndex = Math.min(mIndex+1, mImages.size() -1); |
187 |
torben |
925 |
loadImage(newIndex); |
188 |
|
|
firstX = null; |
189 |
|
|
return false; |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
if (diff < -150) { |
193 |
|
|
int newIndex = Math.max(mIndex-1, 0); |
194 |
|
|
loadImage(newIndex); |
195 |
|
|
firstX = null; |
196 |
|
|
return false; |
197 |
|
|
} |
198 |
|
|
} |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
return true; |
202 |
|
|
} |
203 |
|
|
|
204 |
|
|
} |
205 |
|
|
} |