package dk.thoerup.side9; import java.util.ArrayList; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.animation.AccelerateInterpolator; import android.widget.ImageView; import android.widget.TextView; import dk.thoerup.side9.anim.DisplayNextView; import dk.thoerup.side9.anim.Flip3dAnimation; public class PictureView extends Activity { final static int CONTEXT_VIEWIMG = 1000; final static String TAG = "Side9Pigen"; TextView mDescription; TextView mCaption; ImageView mImageView1; ImageView mImageView2; boolean isFirstImage = true; ArrayList mImages; int mIndex; Bitmap mBitmap; @SuppressWarnings("unchecked") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pictureview); mImages = (ArrayList) getIntent().getSerializableExtra("images"); mIndex = getIntent().getIntExtra("index", 0); mImageView1 = (ImageView) findViewById(R.id.imageview1); mImageView1.setOnTouchListener( new Touch() ); mImageView2 = (ImageView) findViewById(R.id.imageview2); mCaption = (TextView) findViewById(R.id.caption); mDescription = (TextView) findViewById(R.id.description); loadImage(true, true); } private void loadImage(int newIndex) { Log.e(TAG, "NewIndex " + newIndex); if (newIndex != mIndex) { boolean isForward = (newIndex > mIndex); mIndex = newIndex; loadImage(false,isForward); } } private void loadImage(boolean firstLoad, boolean isForward) { ImageEntry currentImage = mImages.get(mIndex); mBitmap = BitmapFactory.decodeFile( currentImage.path ); if (firstLoad) { mImageView1.setImageBitmap(mBitmap); isFirstImage = true; } else { int endAngle = isForward ? -90 : 90; if (isFirstImage) { mImageView2.setImageBitmap(mBitmap); applyRotation(0, endAngle, isForward); } else { mImageView1.setImageBitmap(mBitmap); applyRotation(0, endAngle, isForward); } isFirstImage = !isFirstImage; } if (currentImage.caption.length() > 0) { mCaption.setVisibility(View.VISIBLE); mCaption.setText(currentImage.caption); } else { mCaption.setVisibility(View.INVISIBLE); } String pathParts[] = currentImage.path.split("/"); String fileName = pathParts[ pathParts.length -1]; String desc = "" + (mIndex +1) + "/" + mImages.size() + " - " + fileName; mDescription.setText(desc); } private void applyRotation(float start, float end, boolean isForward) { // Find the center of image final float centerX = mImageView1.getWidth() / 2.0f; final float centerY = mImageView1.getHeight() / 2.0f; // Create a new 3D rotation with the supplied parameter // The animation listener is used to trigger the next animation final Flip3dAnimation rotation = new Flip3dAnimation(start, end, centerX, centerY); rotation.setDuration(250); rotation.setFillAfter(true); rotation.setInterpolator(new AccelerateInterpolator()); rotation.setAnimationListener(new DisplayNextView(isFirstImage, isForward, mImageView1, mImageView2)); if (isFirstImage) { mImageView1.startAnimation(rotation); } else { mImageView2.startAnimation(rotation); } } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuItem item = menu.add(Menu.NONE, CONTEXT_VIEWIMG, Menu.NONE, "Built-in viewer"); item.setIcon( android.R.drawable.ic_menu_gallery); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { boolean res; switch(item.getItemId()) { case CONTEXT_VIEWIMG: String uri = "file://" + mImages.get(mIndex).path; Intent i = new Intent(Intent.ACTION_VIEW); i.setDataAndType(Uri.parse(uri), "image/jpeg"); startActivity(i); res = true; break; default: res = super.onOptionsItemSelected(item); } return res; } class Touch implements View.OnTouchListener { Float firstX = null; @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { firstX = event.getX(); } else { if (firstX != null) { float x = event.getX(); float diff = firstX - x; if (diff > 150) { int newIndex = Math.min(mIndex+1, mImages.size() -1); loadImage(newIndex); firstX = null; return false; } if (diff < -150) { int newIndex = Math.max(mIndex-1, 0); loadImage(newIndex); firstX = null; return false; } } } return true; } } }