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.widget.ImageView; |
18 |
import android.widget.TextView; |
19 |
|
20 |
public class PictureView extends Activity { |
21 |
final static int CONTEXT_VIEWIMG = 1000; |
22 |
|
23 |
final static String TAG = "Side9Pigen"; |
24 |
|
25 |
TextView mDescription; |
26 |
TextView mCaption; |
27 |
ImageView mImageView; |
28 |
ArrayList<ImageEntry> mImagePaths; |
29 |
int mIndex; |
30 |
|
31 |
|
32 |
Bitmap mBitmap; |
33 |
|
34 |
@SuppressWarnings("unchecked") |
35 |
@Override |
36 |
public void onCreate(Bundle savedInstanceState) { |
37 |
super.onCreate(savedInstanceState); |
38 |
setContentView(R.layout.pictureview); |
39 |
|
40 |
mImagePaths = (ArrayList<ImageEntry>) getIntent().getSerializableExtra("images"); |
41 |
mIndex = getIntent().getIntExtra("index", 0); |
42 |
|
43 |
|
44 |
mImageView = (ImageView) findViewById(R.id.imageview); |
45 |
mImageView.setOnTouchListener( new Touch() ); |
46 |
|
47 |
mCaption = (TextView) findViewById(R.id.caption); |
48 |
mDescription = (TextView) findViewById(R.id.description); |
49 |
|
50 |
|
51 |
loadImage(); |
52 |
|
53 |
} |
54 |
|
55 |
|
56 |
|
57 |
private void loadImage(int newIndex) { |
58 |
Log.e(TAG, "NewIndex " + newIndex); |
59 |
if (newIndex != mIndex) { |
60 |
mIndex = newIndex; |
61 |
loadImage(); |
62 |
} |
63 |
} |
64 |
|
65 |
private void loadImage() { |
66 |
ImageEntry currentImage = mImagePaths.get(mIndex); |
67 |
mBitmap = BitmapFactory.decodeFile( currentImage.path ); |
68 |
mImageView.setImageBitmap(mBitmap); |
69 |
|
70 |
|
71 |
if (currentImage.caption.length() > 0) { |
72 |
mCaption.setVisibility(View.VISIBLE); |
73 |
mCaption.setText(currentImage.caption); |
74 |
} else { |
75 |
mCaption.setVisibility(View.INVISIBLE); |
76 |
} |
77 |
|
78 |
String pathParts[] = currentImage.path.split("/"); |
79 |
String fileName = pathParts[ pathParts.length -1]; |
80 |
String desc = "" + (mIndex +1) + "/" + mImagePaths.size() + " - " + fileName; |
81 |
mDescription.setText(desc); |
82 |
} |
83 |
|
84 |
|
85 |
|
86 |
@Override |
87 |
public boolean onCreateOptionsMenu(Menu menu) { |
88 |
MenuItem item = menu.add(Menu.NONE, CONTEXT_VIEWIMG, Menu.NONE, "Built-in viewer"); |
89 |
item.setIcon( android.R.drawable.ic_menu_gallery); |
90 |
return true; |
91 |
} |
92 |
|
93 |
|
94 |
|
95 |
@Override |
96 |
public boolean onOptionsItemSelected(MenuItem item) { |
97 |
boolean res; |
98 |
|
99 |
switch(item.getItemId()) { |
100 |
case CONTEXT_VIEWIMG: |
101 |
String uri = "file://" + mImagePaths.get(mIndex); |
102 |
|
103 |
Intent i = new Intent(Intent.ACTION_VIEW); |
104 |
i.setDataAndType(Uri.parse(uri), "image/jpeg"); |
105 |
startActivity(i); |
106 |
res = true; |
107 |
break; |
108 |
default: |
109 |
res = super.onOptionsItemSelected(item); |
110 |
} |
111 |
|
112 |
return res; |
113 |
} |
114 |
|
115 |
class Touch implements View.OnTouchListener { |
116 |
|
117 |
Float firstX = null; |
118 |
|
119 |
|
120 |
@Override |
121 |
public boolean onTouch(View v, MotionEvent event) { |
122 |
if (event.getAction() == MotionEvent.ACTION_DOWN) { |
123 |
firstX = event.getX(); |
124 |
} else { |
125 |
if (firstX != null) { |
126 |
float x = event.getX(); |
127 |
float diff = firstX - x; |
128 |
|
129 |
if (diff > 150) { |
130 |
int newIndex = Math.min(mIndex+1, mImagePaths.size() -1); |
131 |
loadImage(newIndex); |
132 |
firstX = null; |
133 |
return false; |
134 |
} |
135 |
|
136 |
if (diff < -150) { |
137 |
int newIndex = Math.max(mIndex-1, 0); |
138 |
loadImage(newIndex); |
139 |
firstX = null; |
140 |
return false; |
141 |
} |
142 |
} |
143 |
} |
144 |
|
145 |
return true; |
146 |
} |
147 |
|
148 |
} |
149 |
} |