1 |
package dk.thoerup.side9; |
package dk.thoerup.side9; |
2 |
|
|
3 |
import java.io.File; |
|
|
import java.io.IOException; |
|
|
import java.io.RandomAccessFile; |
|
4 |
import java.util.ArrayList; |
import java.util.ArrayList; |
5 |
|
|
6 |
import android.app.Activity; |
import android.app.Activity; |
14 |
import android.view.MenuItem; |
import android.view.MenuItem; |
15 |
import android.view.MotionEvent; |
import android.view.MotionEvent; |
16 |
import android.view.View; |
import android.view.View; |
17 |
|
import android.view.animation.AccelerateInterpolator; |
18 |
import android.widget.ImageView; |
import android.widget.ImageView; |
19 |
import android.widget.TextView; |
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 { |
public class PictureView extends Activity { |
24 |
final static int CONTEXT_VIEWIMG = 1000; |
final static int CONTEXT_VIEWIMG = 1000; |
27 |
|
|
28 |
TextView mDescription; |
TextView mDescription; |
29 |
TextView mCaption; |
TextView mCaption; |
30 |
ImageView mImageView; |
ImageView mImageView1; |
31 |
ArrayList<String> mImagePaths; |
ImageView mImageView2; |
32 |
|
|
33 |
|
boolean isFirstImage = true; |
34 |
|
|
35 |
|
ArrayList<ImageEntry> mImages; |
36 |
int mIndex; |
int mIndex; |
37 |
|
|
38 |
|
|
44 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
45 |
setContentView(R.layout.pictureview); |
setContentView(R.layout.pictureview); |
46 |
|
|
47 |
mImagePaths = (ArrayList<String>) getIntent().getSerializableExtra("files"); |
mImages = (ArrayList<ImageEntry>) getIntent().getSerializableExtra("images"); |
48 |
mIndex = getIntent().getIntExtra("index", 0); |
mIndex = getIntent().getIntExtra("index", 0); |
49 |
|
|
50 |
|
|
51 |
mImageView = (ImageView) findViewById(R.id.imageview); |
mImageView1 = (ImageView) findViewById(R.id.imageview1); |
52 |
mImageView.setOnTouchListener( new Touch() ); |
mImageView1.setOnTouchListener( new Touch() ); |
53 |
|
|
54 |
|
mImageView2 = (ImageView) findViewById(R.id.imageview2); |
55 |
|
|
56 |
mCaption = (TextView) findViewById(R.id.caption); |
mCaption = (TextView) findViewById(R.id.caption); |
57 |
mDescription = (TextView) findViewById(R.id.description); |
mDescription = (TextView) findViewById(R.id.description); |
58 |
|
|
59 |
|
|
60 |
loadImage(); |
loadImage(true, true); |
61 |
|
|
62 |
} |
} |
63 |
|
|
66 |
private void loadImage(int newIndex) { |
private void loadImage(int newIndex) { |
67 |
Log.e(TAG, "NewIndex " + newIndex); |
Log.e(TAG, "NewIndex " + newIndex); |
68 |
if (newIndex != mIndex) { |
if (newIndex != mIndex) { |
69 |
|
|
70 |
|
boolean isForward = (newIndex > mIndex); |
71 |
|
|
72 |
mIndex = newIndex; |
mIndex = newIndex; |
73 |
loadImage(); |
loadImage(false,isForward); |
74 |
} |
} |
75 |
} |
} |
76 |
|
|
77 |
private void loadImage() { |
private void loadImage(boolean firstLoad, boolean isForward) { |
78 |
String currentFile = mImagePaths.get(mIndex); |
ImageEntry currentImage = mImages.get(mIndex); |
79 |
mBitmap = BitmapFactory.decodeFile( currentFile ); |
mBitmap = BitmapFactory.decodeFile( currentImage.path ); |
80 |
mImageView.setImageBitmap(mBitmap); |
|
81 |
|
if (firstLoad) { |
82 |
String pathParts[] = currentFile.split("/"); |
mImageView1.setImageBitmap(mBitmap); |
83 |
String fileName = pathParts[ pathParts.length -1]; |
isFirstImage = true; |
84 |
String desc = "" + (mIndex +1) + "/" + mImagePaths.size() + " - " + fileName; |
} else { |
85 |
mDescription.setText(desc); |
|
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 |
String infoFileName = currentFile.replace(".jpg", ".txt"); |
if (currentImage.caption.length() > 0) { |
|
|
|
|
File infoFile = new File(infoFileName); |
|
|
if (infoFile.exists() ) { |
|
|
String caption = getFileContent(infoFile); |
|
103 |
mCaption.setVisibility(View.VISIBLE); |
mCaption.setVisibility(View.VISIBLE); |
104 |
mCaption.setText(caption); |
mCaption.setText(currentImage.caption); |
|
|
|
105 |
} else { |
} else { |
106 |
mCaption.setVisibility(View.INVISIBLE); |
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 |
String getFileContent(File filename) { |
private void applyRotation(float start, float end, boolean isForward) { |
118 |
try { |
// Find the center of image |
119 |
RandomAccessFile raf = new RandomAccessFile(filename, "r"); |
final float centerX = mImageView1.getWidth() / 2.0f; |
120 |
int size = (int)raf.length(); |
final float centerY = mImageView1.getHeight() / 2.0f; |
121 |
byte buf[] = new byte[size]; |
|
122 |
|
// Create a new 3D rotation with the supplied parameter |
123 |
raf.readFully(buf); |
// The animation listener is used to trigger the next animation |
124 |
return new String(buf); |
final Flip3dAnimation rotation = new Flip3dAnimation(start, end, centerX, centerY); |
125 |
} catch (IOException e) { |
rotation.setDuration(250); |
126 |
Log.e(TAG, "Error", e); |
rotation.setFillAfter(true); |
127 |
return ""; |
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 |
@Override |
143 |
public boolean onCreateOptionsMenu(Menu menu) { |
public boolean onCreateOptionsMenu(Menu menu) { |
154 |
|
|
155 |
switch(item.getItemId()) { |
switch(item.getItemId()) { |
156 |
case CONTEXT_VIEWIMG: |
case CONTEXT_VIEWIMG: |
157 |
String uri = "file://" + mImagePaths.get(mIndex); |
String uri = "file://" + mImages.get(mIndex).path; |
158 |
|
|
159 |
Intent i = new Intent(Intent.ACTION_VIEW); |
Intent i = new Intent(Intent.ACTION_VIEW); |
160 |
i.setDataAndType(Uri.parse(uri), "image/jpeg"); |
i.setDataAndType(Uri.parse(uri), "image/jpeg"); |
183 |
float diff = firstX - x; |
float diff = firstX - x; |
184 |
|
|
185 |
if (diff > 150) { |
if (diff > 150) { |
186 |
int newIndex = Math.min(mIndex+1, mImagePaths.size() -1); |
int newIndex = Math.min(mIndex+1, mImages.size() -1); |
187 |
loadImage(newIndex); |
loadImage(newIndex); |
188 |
firstX = null; |
firstX = null; |
189 |
return false; |
return false; |