Android Open Source - sana Image Preview Dialog






From Project

Back to project page sana.

License

The source code is released under:

Copyright (c) 2010, Moca All rights reserved. The source code for Moca is licensed under the BSD license as follows: Redistribution and use in source and binary forms, with or without modification, ...

If you think the Android project sana listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.moca;
/*from   w  w  w . jav a 2 s .  co m*/
import java.io.IOException;
import java.io.InputStream;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;

/**
 * ImagePreviewDialog for reviewing captured images. 
 * 
 * Quick user hints: Zoom in, Zoom out, center image, and close dialog (back)
 * buttons available.
 * 
 * Also, a user can flick the image or pan it by dragging the image. If the user
 * holds down a finger to the image, it will reset (center and resize). If a user
 * taps on the image, it will zoom in.
 */
public class ImagePreviewDialog extends Dialog implements OnClickListener,
    OnTouchListener, OnGestureListener {

  private static int BITMAP_SAMPLE_SIZE = 1;

  private Button zoomInButton, zoomOutButton, centerButton, endButton, rotateButton, fitButton;
  private ImageView imageView;
  private GestureDetector gestureDetector;
  private Context context;
  private float scaledWidth, scaledHeight, transX, transY;
  private float imageWidth, imageHeight;
  private float originalWidth, originalHeight;
  private final float zoomConst = 0.4f;
  private final float initScaledWidth = 0.55f, initScaledHeight = 0.55f;
  
  public ImagePreviewDialog(Context c) {
    super(c);
    LinearLayout fv = new LinearLayout(c);
    context = c;
    imageView = new ImageView(c);
    zoomInButton = new Button(c);
    zoomInButton.setText("ZoomIn");
    zoomInButton.setOnClickListener(this);
    zoomOutButton = new Button(c);
    zoomOutButton.setText("ZoomOut");
    zoomOutButton.setOnClickListener(this);
    centerButton = new Button(c);
    centerButton.setText("Center");
    centerButton.setOnClickListener(this);
    endButton = new Button(c);
    endButton.setText("Back");
    endButton.setOnClickListener(this);
    LinearLayout buttonContainer = new LinearLayout(c);
    buttonContainer.setOrientation(LinearLayout.HORIZONTAL);
    buttonContainer.addView(zoomInButton);
    buttonContainer.addView(zoomOutButton);
    buttonContainer.addView(centerButton);
    buttonContainer.addView(endButton);
    imageView.setOnClickListener(this);
    imageView.setOnTouchListener(this);
    gestureDetector = new GestureDetector(this);

//    height is a hack set for the G1 phone
//    imageView.setLayoutParams(new LayoutParams(-1, -1));
        
    fv.addView(buttonContainer);
    fv.addView(imageView);
    fv.setOrientation(LinearLayout.VERTICAL);
    fv.setGravity(Gravity.CENTER_HORIZONTAL);
    fv.setLayoutParams(new LayoutParams(150, -1));
    setTitle("View Photos");
    setContentView(fv);
  }

  public void onClick(View v) {
    if (v == zoomInButton) {
      zoomIn();
    } else if (v == zoomOutButton) {
      zoomOut();
    } else if (v == centerButton) {
      center();
    } else if (v == endButton) {
      this.dismiss();
    }
  }

  /**
   * Loads a bitmap image into the image preview dialog window.
   * @param is inputstream of the bitmap image
   */
  public void showImage(Uri imageUri) throws IOException {
    BitmapFactory.Options bmo = new BitmapFactory.Options();

      // Get the image size.
      bmo.inJustDecodeBounds = true;
      InputStream imageInputStream = getContext().getContentResolver().openInputStream(imageUri);
      BitmapFactory.decodeStream(imageInputStream, null, bmo);
      int width = bmo.outWidth;
      int height = bmo.outHeight;
      bmo.inJustDecodeBounds = false;
      imageInputStream.close();
      
      
      // Load the bitmap and downsample it to 1000 pixels along its largest dimension.
      int maxDimension = (width > height) ? width : height;
      bmo.inSampleSize = maxDimension / 1000;
      imageInputStream = getContext().getContentResolver().openInputStream(imageUri);
    Bitmap loadedBitmap = BitmapFactory.decodeStream(imageInputStream, null, bmo);
    
    originalWidth = (float) (loadedBitmap.getWidth());
    originalHeight = (float) (loadedBitmap.getHeight());  
    scaledWidth = initScaledWidth;
    scaledHeight = initScaledHeight;
    imageView.setImageBitmap(loadedBitmap);    
    transX = 0f;
    transY = 0f;
    updateSize();
    setImageMatrix();
  }
  

  void updateSize() {
    imageWidth = originalWidth * scaledWidth;
    imageHeight = originalHeight * scaledHeight;
  }

  void setImageMatrix() {
    Matrix mtrx = new Matrix();
    mtrx.postScale(scaledWidth, scaledHeight);
    mtrx.postTranslate(transX, transY);
    imageView.setImageMatrix(mtrx);
    imageView.setScaleType(ScaleType.MATRIX);
    imageView.invalidate();
  }

  /**
   * Pan an image.
   * @param dx left/right movement amount
   * @param dy up/down movement amount
   */
  void translate(float dx, float dy) {
    transX -= dx;
    transY -= dy;
    setImageMatrix();
  }

  /**
   * Zooms in the image in by a constant factor (zoomConst).
   */
  void zoomIn() {
    float origImageWidth = imageWidth;
    float origImageHeight = imageHeight;
    scaledWidth += zoomConst;
    scaledHeight += zoomConst;
    updateSize();
    float widthIncrease = imageWidth - origImageWidth;
    float heightIncrease = imageHeight - origImageHeight;
    translate(0.5f * widthIncrease, 0.5f * heightIncrease);  
//    setImageMatrix();
  }

   /**
   * Zooms out the image in by a constant factor (zoomConst).
   */
  void zoomOut() {    
    float origImageWidth = imageWidth;
    float origImageHeight = imageHeight;
    if (scaledWidth > zoomConst)
      scaledWidth -= zoomConst;
    if (scaledHeight > zoomConst)
      scaledHeight -= zoomConst;
    updateSize();
    float widthIncrease = imageWidth - origImageWidth;
    float heightIncrease = imageHeight - origImageHeight;
    translate(0.5f * widthIncrease, 0.5f * heightIncrease);
//    setImageMatrix();
  }

  /**
   * Centers the image in the image preview dialog.
   */
  void center() {
    transX = (imageView.getWidth() / 2.0f) - (imageWidth / 2.0f);
    transY = (imageView.getHeight() / 2.0f) - (imageHeight / 2.0f);
    updateSize();
    translate(0, 0); 
  }

  public boolean onTouch(View v, MotionEvent me) {
    if (v == imageView) {
      gestureDetector.onTouchEvent(me);
    }
    return false;
  }

  public boolean onDown(MotionEvent arg0) {
    return false;
  }

  public boolean onFling(MotionEvent arg0, MotionEvent arg1, float velocityX,
      float velocityY) {
//    Log.i("imagepreview", "fling x:" + velocityX + " y:" + velocityY);
    translate((float) (-velocityX / 8.0), (float) (-velocityY / 8.0));
    return false; 
  }

  /**
   * Reset image position and size when a finger is held to the screen.
   */
  public void onLongPress(MotionEvent arg0) {
//    Log.d("imagepreview", "long press");
    scaledWidth = initScaledWidth;
    scaledHeight = initScaledHeight;
    updateSize();
    center();
  }

  public boolean onScroll(MotionEvent arg0, MotionEvent arg1,
      float distanceX, float distanceY) {
//    Log.i("imagepreview", "scroll x:" + distanceX + " y:" + distanceY);
    translate((float) (distanceX / 1.0), (float) (distanceY / 1.0));
    return false;
  }

  public void onShowPress(MotionEvent arg0) {
//    Log.d("imagepreview", "show press");
  }

  /**
   * Zoom in when screen is tapped.
   */
  public boolean onSingleTapUp(MotionEvent arg0) {
//    Log.d("imagepreview", "single tap");
    zoomIn();
    return false;
  }
}




Java Source Code List

.Moca.java
org.moca.Constants.java
org.moca.ImagePreviewDialog.java
org.moca.ScalingImageAdapter.java
org.moca.SelectableImageView.java
org.moca.activity.NotificationList.java
org.moca.activity.NotificationViewer.java
org.moca.activity.PatientInfoDialog.java
org.moca.activity.ProcedureRunner.java
org.moca.activity.ProceduresList.java
org.moca.activity.SavedProcedureList.java
org.moca.activity.Settings.java
org.moca.db.EncounterDAO.java
org.moca.db.EventDAO.java
org.moca.db.EventProvider.java
org.moca.db.Event.java
org.moca.db.ImageProvider.java
org.moca.db.MocaDB.java
org.moca.db.NotificationMessage.java
org.moca.db.NotificationProvider.java
org.moca.db.PatientInfo.java
org.moca.db.PatientProvider.java
org.moca.db.PatientValidator.java
org.moca.db.ProcedureDAO.java
org.moca.db.ProcedureProvider.java
org.moca.db.SavedProcedureProvider.java
org.moca.db.SoundProvider.java
org.moca.media.AudioPlayer.java
org.moca.net.MDSCode.java
org.moca.net.MDSInterface.java
org.moca.net.MDSNotification.java
org.moca.net.MDSResult.java
org.moca.net.SMSReceive.java
org.moca.procedure.BinaryUploadElement.java
org.moca.procedure.DateElement.java
org.moca.procedure.GpsElement.java
org.moca.procedure.MultiSelectElement.java
org.moca.procedure.PatientIdElement.java
org.moca.procedure.PictureElement.java
org.moca.procedure.ProcedureElement.java
org.moca.procedure.ProcedurePage.java
org.moca.procedure.ProcedureParseException.java
org.moca.procedure.Procedure.java
org.moca.procedure.RadioElement.java
org.moca.procedure.SelectElement.java
org.moca.procedure.SoundElement.java
org.moca.procedure.TextElement.java
org.moca.procedure.TextEntryElement.java
org.moca.procedure.ValidationError.java
org.moca.procedure.branching.Criteria.java
org.moca.procedure.branching.Criterion.java
org.moca.procedure.branching.LogicAnd.java
org.moca.procedure.branching.LogicBase.java
org.moca.procedure.branching.LogicNot.java
org.moca.procedure.branching.LogicOr.java
org.moca.service.BackgroundUploader.java
org.moca.service.QueueManager.java
org.moca.service.ServiceConnector.java
org.moca.service.ServiceListener.java
org.moca.task.CheckCredentialsTask.java
org.moca.task.ImageProcessingTaskRequest.java
org.moca.task.ImageProcessingTask.java
org.moca.task.MDSSyncTask.java
org.moca.task.PatientLookupListener.java
org.moca.task.PatientLookupTask.java
org.moca.task.ResetDatabaseTask.java
org.moca.task.ValidationListener.java
org.moca.util.MocaUtil.java
org.moca.util.UserDatabase.java