Android Open Source - sana Scaling Image Adapter






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;
//ww  w  .j a v a  2s . com
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import org.moca.db.ImageProvider;
import org.moca.db.MocaDB;
import org.moca.db.MocaDB.ImageSQLFormat;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.GridView;
import android.widget.ImageView;

/**
 * Scaling image adapter is used by the image grid for displaying multiple
 * images acquired on the phone on a picture element in a given procedure. The
 * ScalingImageAdapter also maintains the state for the set of selected images.
 * 
 * Images are stored in a database. This adapter exposes these images to the
 * GridView.
 */
public class ScalingImageAdapter extends CursorAdapter {
    private static final String TAG = ScalingImageAdapter.class.toString();
    private static final int IMAGE_WIDTH = 90;
    private static final int IMAGE_HEIGHT = 90;
    
    private int scaleFactor;
    private HashMap<Long, Boolean> selectedImages;

    public ScalingImageAdapter(Context ctx, Cursor cur, int scaleFactor) {
        super(ctx,cur);
        this.scaleFactor = scaleFactor;
        this.selectedImages = new HashMap<Long,Boolean>();
    }
    
    public boolean isSelected(long id) {
      if(selectedImages.containsKey(id)) {
        return selectedImages.get(id);
      }
      return false;
    }
    
    public void setSelected(long id, boolean status) {
      Log.i(TAG, "Setting " + id + " selected as " + status);
      selectedImages.put(id, status);
    }
    
    public void toggleSelection(long selection) { 
      setSelected(selection, !isSelected(selection));
    }
    
    private Bitmap bitmapForImageUri(Context context, Uri imageUri) throws IOException {
      BitmapFactory.Options bmo = new BitmapFactory.Options();
      bmo.inSampleSize = scaleFactor;
      InputStream is = context.getContentResolver().openInputStream(imageUri);
      Bitmap bitmap = BitmapFactory.decodeStream(is, null, bmo); 
      is.close();
      return bitmap;
    }
    
    /**
     * Takes the cursor and returns the URI for the cursor's current row.
     */
    private Uri getImageThumbnailUriFromCursorRow(Cursor cursor) {
        int id = cursor.getInt(cursor.getColumnIndexOrThrow(ImageSQLFormat._ID));
        Uri uri = ContentUris.withAppendedId(MocaDB.ImageSQLFormat.CONTENT_URI, id);
        return ImageProvider.getThumbUri(uri);
    }
    
    /**
     * Makes a new, empty view. (do not bind an image to it or set its id)
     */
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
      Log.i(TAG, "newView");
        ImageView imageView = new SelectableImageView(context, this);
        imageView.setLayoutParams(new GridView.LayoutParams(IMAGE_WIDTH, IMAGE_HEIGHT));
        imageView.setAdjustViewBounds(false);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        //imageView.setPadding(6, 6, 6, 6);
        imageView.setPadding(6, 12, 6, 12);
        return imageView;
    }

    /**
     * Takes a SelectableImageView, binds an image to it, and sets it image id.
     */
    @Override
    public void bindView(View _view, Context context, Cursor cursor) {
        SelectableImageView view = (SelectableImageView)_view;

        long imageId = cursor.getLong(cursor.getColumnIndex(ImageSQLFormat._ID));
        view.setImageId(imageId);
        
        // Make new images selected by default
        if(!selectedImages.containsKey(imageId)) {
          selectedImages.put(imageId, true);
        }
        
        Uri thumbUri = getImageThumbnailUriFromCursorRow(cursor);
        view.setImageURI(thumbUri);
        
        Log.i(TAG, "bindView: " + thumbUri);
        /*new Thread() { 
          private Context mContext;
          private Uri imageUri;
          private SelectableImageView view;
          
          void initAndStart(Uri imageUri, SelectableImageView view, Context context) {
            this.imageUri = imageUri;
            this.view = view;
            this.mContext = context;
            start();
          }
          
          public void run() {
            try {
              view.setImageBitmap(bitmapForImageUri(mContext, imageUri));
            } catch(IOException e) {
            }
          }
        }.initAndStart(imageUri, view, context);*/
    }
}




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