1 |
package dk.thoerup.side9; |
2 |
|
3 |
|
4 |
import java.util.ArrayList; |
5 |
|
6 |
import android.app.Activity; |
7 |
import android.content.Intent; |
8 |
import android.graphics.Bitmap; |
9 |
import android.graphics.BitmapFactory; |
10 |
import android.net.Uri; |
11 |
import android.os.Bundle; |
12 |
import android.util.Log; |
13 |
import android.view.Menu; |
14 |
import android.view.MenuItem; |
15 |
import android.view.MotionEvent; |
16 |
import android.view.View; |
17 |
import android.view.animation.AccelerateInterpolator; |
18 |
import android.widget.ImageView; |
19 |
import android.widget.TextView; |
20 |
import dk.thoerup.side9.anim.DisplayNextView; |
21 |
import dk.thoerup.side9.anim.Flip3dAnimation; |
22 |
|
23 |
public class PictureView extends Activity { |
24 |
final static int CONTEXT_VIEWIMG = 1000; |
25 |
|
26 |
final static String TAG = "Side9Pigen"; |
27 |
|
28 |
TextView mDescription; |
29 |
TextView mCaption; |
30 |
ImageView mImageView1; |
31 |
ImageView mImageView2; |
32 |
|
33 |
boolean isFirstImage = true; |
34 |
|
35 |
ArrayList<ImageEntry> mImages; |
36 |
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 |
mImages = (ArrayList<ImageEntry>) getIntent().getSerializableExtra("images"); |
48 |
mIndex = getIntent().getIntExtra("index", 0); |
49 |
|
50 |
|
51 |
mImageView1 = (ImageView) findViewById(R.id.imageview1); |
52 |
mImageView1.setOnTouchListener( new Touch() ); |
53 |
|
54 |
mImageView2 = (ImageView) findViewById(R.id.imageview2); |
55 |
|
56 |
mCaption = (TextView) findViewById(R.id.caption); |
57 |
mDescription = (TextView) findViewById(R.id.description); |
58 |
|
59 |
|
60 |
loadImage(true, true); |
61 |
|
62 |
} |
63 |
|
64 |
|
65 |
|
66 |
private void loadImage(int newIndex) { |
67 |
Log.e(TAG, "NewIndex " + newIndex); |
68 |
if (newIndex != mIndex) { |
69 |
|
70 |
boolean isForward = (newIndex > mIndex); |
71 |
|
72 |
mIndex = newIndex; |
73 |
loadImage(false,isForward); |
74 |
} |
75 |
} |
76 |
|
77 |
private void loadImage(boolean firstLoad, boolean isForward) { |
78 |
ImageEntry currentImage = mImages.get(mIndex); |
79 |
mBitmap = BitmapFactory.decodeFile( currentImage.path ); |
80 |
|
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 |
|
101 |
|
102 |
if (currentImage.caption.length() > 0) { |
103 |
mCaption.setVisibility(View.VISIBLE); |
104 |
mCaption.setText(currentImage.caption); |
105 |
} else { |
106 |
mCaption.setVisibility(View.INVISIBLE); |
107 |
} |
108 |
|
109 |
String pathParts[] = currentImage.path.split("/"); |
110 |
String fileName = pathParts[ pathParts.length -1]; |
111 |
String desc = "" + (mIndex +1) + "/" + mImages.size() + " - " + fileName; |
112 |
mDescription.setText(desc); |
113 |
} |
114 |
|
115 |
|
116 |
|
117 |
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 |
|
122 |
// 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 |
|
142 |
@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 |
String uri = "file://" + mImages.get(mIndex).path; |
158 |
|
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 |
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 |
int newIndex = Math.min(mIndex+1, mImages.size() -1); |
187 |
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 |
} |