1 |
package dk.thoerup.side9; |
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
import java.io.RandomAccessFile; |
6 |
import java.util.ArrayList; |
7 |
|
8 |
import android.app.Activity; |
9 |
import android.graphics.Bitmap; |
10 |
import android.graphics.BitmapFactory; |
11 |
import android.os.Bundle; |
12 |
import android.util.Log; |
13 |
import android.view.MotionEvent; |
14 |
import android.view.View; |
15 |
import android.widget.ImageView; |
16 |
import android.widget.TextView; |
17 |
|
18 |
public class PictureView extends Activity { |
19 |
|
20 |
final static String TAG = "Side9Pigen"; |
21 |
|
22 |
TextView mDescription; |
23 |
TextView mCaption; |
24 |
ImageView mImageView; |
25 |
ArrayList<String> mImagePaths; |
26 |
int mIndex; |
27 |
|
28 |
|
29 |
Bitmap mBitmap; |
30 |
|
31 |
@SuppressWarnings("unchecked") |
32 |
@Override |
33 |
public void onCreate(Bundle savedInstanceState) { |
34 |
super.onCreate(savedInstanceState); |
35 |
setContentView(R.layout.pictureview); |
36 |
|
37 |
mImagePaths = (ArrayList<String>) getIntent().getSerializableExtra("files"); |
38 |
mIndex = getIntent().getIntExtra("index", 0); |
39 |
|
40 |
|
41 |
mImageView = (ImageView) findViewById(R.id.imageview); |
42 |
mImageView.setOnTouchListener( new Touch() ); |
43 |
|
44 |
mDescription = (TextView) findViewById(R.id.description); |
45 |
mCaption = (TextView) findViewById(R.id.caption); |
46 |
|
47 |
loadImage(); |
48 |
|
49 |
} |
50 |
|
51 |
private void loadImage(int newIndex) { |
52 |
Log.e(TAG, "NewIndex " + newIndex); |
53 |
if (newIndex != mIndex) { |
54 |
mIndex = newIndex; |
55 |
loadImage(); |
56 |
} |
57 |
} |
58 |
|
59 |
private void loadImage() { |
60 |
String currentFile = mImagePaths.get(mIndex); |
61 |
mBitmap = BitmapFactory.decodeFile( currentFile ); |
62 |
mImageView.setImageBitmap(mBitmap); |
63 |
|
64 |
String pathParts[] = currentFile.split("/"); |
65 |
String fileName = pathParts[ pathParts.length -1]; |
66 |
String desc = "" + (mIndex +1) + "/" + mImagePaths.size() + " - " + fileName; |
67 |
mDescription.setText(desc); |
68 |
|
69 |
String infoFileName = currentFile.replace(".jpg", ".txt"); |
70 |
|
71 |
File infoFile = new File(infoFileName); |
72 |
if (infoFile.exists() ) { |
73 |
String caption = getFileContent(infoFile); |
74 |
mCaption.setVisibility(View.VISIBLE); |
75 |
mCaption.setText(caption); |
76 |
|
77 |
} else { |
78 |
mCaption.setVisibility(View.INVISIBLE); |
79 |
} |
80 |
} |
81 |
|
82 |
String getFileContent(File filename) { |
83 |
try { |
84 |
RandomAccessFile raf = new RandomAccessFile(filename, "r"); |
85 |
int size = (int)raf.length(); |
86 |
byte buf[] = new byte[size]; |
87 |
|
88 |
raf.readFully(buf); |
89 |
return new String(buf); |
90 |
} catch (IOException e) { |
91 |
Log.e(TAG, "Error", e); |
92 |
return ""; |
93 |
} |
94 |
} |
95 |
|
96 |
class Touch implements View.OnTouchListener { |
97 |
|
98 |
Float firstX = null; |
99 |
|
100 |
|
101 |
@Override |
102 |
public boolean onTouch(View v, MotionEvent event) { |
103 |
if (event.getAction() == MotionEvent.ACTION_DOWN) { |
104 |
firstX = event.getX(); |
105 |
} else { |
106 |
if (firstX != null) { |
107 |
float x = event.getX(); |
108 |
float diff = firstX - x; |
109 |
|
110 |
if (diff > 150) { |
111 |
int newIndex = Math.min(mIndex+1, mImagePaths.size() -1); |
112 |
loadImage(newIndex); |
113 |
firstX = null; |
114 |
return false; |
115 |
} |
116 |
|
117 |
if (diff < -150) { |
118 |
int newIndex = Math.max(mIndex-1, 0); |
119 |
loadImage(newIndex); |
120 |
firstX = null; |
121 |
return false; |
122 |
} |
123 |
} |
124 |
} |
125 |
|
126 |
return true; |
127 |
} |
128 |
|
129 |
} |
130 |
} |