Friday, 7 March 2014

TounchImageView for zoom Image

1) Create class TouchImageView.java
=========================
/*
 * TouchImageView.java
 * By: Michael Ortiz
 * Updated By: Patrick Lackemacher
 * Updated By: Babay88
 * Updated By: @ipsilondev
 * -------------------
 * Extends Android ImageView to include pinch zooming, panning, fling and double tap zoom.
 */

package com.example.touch;

import static com.example.touch.TouchImageView.State.ANIMATE_ZOOM;
import static com.example.touch.TouchImageView.State.DRAG;
import static com.example.touch.TouchImageView.State.FLING;
import static com.example.touch.TouchImageView.State.NONE;
import static com.example.touch.TouchImageView.State.ZOOM;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;
import android.widget.Scroller;

public class TouchImageView extends ImageView {

private static final String DEBUG = "DEBUG";

//
// SuperMin and SuperMax multipliers. Determine how much the image can be
// zoomed below or above the zoom boundaries, before animating back to the
// min/max zoom boundary.
//
private static final float SUPER_MIN_MULTIPLIER = .75f;
private static final float SUPER_MAX_MULTIPLIER = 1.25f;

    //
    // Scale of image ranges from minScale to maxScale, where minScale == 1
    // when the image is stretched to fit view.
    //
    private float normalizedScale;
   
    //
    // Matrix applied to image. MSCALE_X and MSCALE_Y should always be equal.
    // MTRANS_X and MTRANS_Y are the other values used. prevMatrix is the matrix
    // saved prior to the screen rotating.
    //
private Matrix matrix, prevMatrix;

    public static enum State { NONE, DRAG, ZOOM, FLING, ANIMATE_ZOOM };
    private State state;

    private float minScale;
    private float maxScale;
    private float superMinScale;
    private float superMaxScale;
    private float[] m;
   
    private Context context;
    private Fling fling;

    //
    // Size of view and previous view size (ie before rotation)
    //
    private int viewWidth, viewHeight, prevViewWidth, prevViewHeight;
   
    //
    // Size of image when it is stretched to fit view. Before and After rotation.
    //
    private float matchViewWidth, matchViewHeight, prevMatchViewWidth, prevMatchViewHeight;
   
    //
    // After setting image, a value of true means the new image should maintain
    // the zoom of the previous image. False means it should be resized within the view.
    //
    private boolean maintainZoomAfterSetImage;
   
    //
    // True when maintainZoomAfterSetImage has been set to true and setImage has been called.
    //
    private boolean setImageCalledRecenterImage;
   
    private ScaleGestureDetector mScaleDetector;
    private GestureDetector mGestureDetector;

    public TouchImageView(Context context) {
        super(context);
        sharedConstructing(context);
    }

    public TouchImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        sharedConstructing(context);
    }
   
    public TouchImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    sharedConstructing(context);
    }
   
    private void sharedConstructing(Context context) {
        super.setClickable(true);
        this.context = context;
        mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
        mGestureDetector = new GestureDetector(context, new GestureListener());
        matrix = new Matrix();
        prevMatrix = new Matrix();
        m = new float[9];
        normalizedScale = 1;
        minScale = 1;
        maxScale = 3;
        superMinScale = SUPER_MIN_MULTIPLIER * minScale;
        superMaxScale = SUPER_MAX_MULTIPLIER * maxScale;
        maintainZoomAfterSetImage = true;
        setImageMatrix(matrix);
        setScaleType(ScaleType.MATRIX);
        setState(NONE);
        setOnTouchListener(new TouchImageViewListener());
    }
   
    @Override
    public void setImageResource(int resId) {
    super.setImageResource(resId);
    setImageCalled();
    savePreviousImageValues();
    fitImageToView();
    }
   
    @Override
    public void setImageBitmap(Bitmap bm) {
    super.setImageBitmap(bm);
    setImageCalled();
    savePreviousImageValues();
    fitImageToView();
    }
   
    @Override
    public void setImageDrawable(Drawable drawable) {
    super.setImageDrawable(drawable);
    setImageCalled();
    savePreviousImageValues();
    fitImageToView();
    }
   
    @Override
    public void setImageURI(Uri uri) {
    super.setImageURI(uri);
    setImageCalled();
    savePreviousImageValues();
    fitImageToView();
    }
   
    private void setImageCalled() {
    if (!maintainZoomAfterSetImage) {
    setImageCalledRecenterImage = true;
    }
    }
   
    /**
     * Save the current matrix and view dimensions
     * in the prevMatrix and prevView variables.
     */
    private void savePreviousImageValues() {
    if (matrix != null) {
    matrix.getValues(m);
    prevMatrix.setValues(m);
    prevMatchViewHeight = matchViewHeight;
       prevMatchViewWidth = matchViewWidth;
       prevViewHeight = viewHeight;
       prevViewWidth = viewWidth;
    }
    }
   
    @Override
    public Parcelable onSaveInstanceState() {
      Bundle bundle = new Bundle();
      bundle.putParcelable("instanceState", super.onSaveInstanceState());
      bundle.putFloat("saveScale", normalizedScale);
      bundle.putFloat("matchViewHeight", matchViewHeight);
      bundle.putFloat("matchViewWidth", matchViewWidth);
      bundle.putInt("viewWidth", viewWidth);
      bundle.putInt("viewHeight", viewHeight);
      matrix.getValues(m);
      bundle.putFloatArray("matrix", m);
      return bundle;
    }

    @Override
    public void onRestoreInstanceState(Parcelable state) {
      if (state instanceof Bundle) {
       Bundle bundle = (Bundle) state;
       normalizedScale = bundle.getFloat("saveScale");
       m = bundle.getFloatArray("matrix");
       prevMatrix.setValues(m);
       prevMatchViewHeight = bundle.getFloat("matchViewHeight");
       prevMatchViewWidth = bundle.getFloat("matchViewWidth");
       prevViewHeight = bundle.getInt("viewHeight");
       prevViewWidth = bundle.getInt("viewWidth");
       super.onRestoreInstanceState(bundle.getParcelable("instanceState"));
       return;
      }

      super.onRestoreInstanceState(state);
    }
   
    /**
     * Get the max zoom multiplier.
     * @return max zoom multiplier.
     */
    public float getMaxZoom() {
    return maxScale;
    }

    /**
     * Set the max zoom multiplier. Default value: 3.
     * @param max max zoom multiplier.
     */
    public void setMaxZoom(float max) {
        maxScale = max;
        superMaxScale = SUPER_MAX_MULTIPLIER * maxScale;
    }
   
    /**
     * Get the min zoom multiplier.
     * @return min zoom multiplier.
     */
    public float getMinZoom() {
    return minScale;
    }
   
    /**
     * After setting image, a value of true means the new image should maintain
     * the zoom of the previous image. False means the image should be resized within
     * the view. Defaults value is true.
     * @param maintainZoom
     */
    public void maintainZoomAfterSetImage(boolean maintainZoom) {
    maintainZoomAfterSetImage = maintainZoom;
    }
   
    /**
     * Get the current zoom. This is the zoom relative to the initial
     * scale, not the original resource.
     * @return current zoom multiplier.
     */
    public float getCurrentZoom() {
    return normalizedScale;
    }
   
    /**
     * Set the min zoom multiplier. Default value: 1.
     * @param min min zoom multiplier.
     */
    public void setMinZoom(float min) {
    minScale = min;
    superMinScale = SUPER_MIN_MULTIPLIER * minScale;
    }
   
    /**
     * For a given point on the view (ie, a touch event), returns the
     * point relative to the original drawable's coordinate system.
     * @param x
     * @param y
     * @return PointF relative to original drawable's coordinate system.
     */
    public PointF getDrawablePointFromTouchPoint(float x, float y) {
    return transformCoordTouchToBitmap(x, y, true);
    }
   
    /**
     * For a given point on the view (ie, a touch event), returns the
     * point relative to the original drawable's coordinate system.
     * @param p
     * @return PointF relative to original drawable's coordinate system.
     */
    public PointF getDrawablePointFromTouchPoint(PointF p) {
    return transformCoordTouchToBitmap(p.x, p.y, true);
    }
   
    /**
     * Performs boundary checking and fixes the image matrix if it
     * is out of bounds.
     */
    private void fixTrans() {
        matrix.getValues(m);
        float transX = m[Matrix.MTRANS_X];
        float transY = m[Matrix.MTRANS_Y];
       
        float fixTransX = getFixTrans(transX, viewWidth, getImageWidth());
        float fixTransY = getFixTrans(transY, viewHeight, getImageHeight());
       
        if (fixTransX != 0 || fixTransY != 0) {
            matrix.postTranslate(fixTransX, fixTransY);
        }
    }
   
    /**
     * When transitioning from zooming from focus to zoom from center (or vice versa)
     * the image can become unaligned within the view. This is apparent when zooming
     * quickly. When the content size is less than the view size, the content will often
     * be centered incorrectly within the view. fixScaleTrans first calls fixTrans() and
     * then makes sure the image is centered correctly within the view.
     */
    private void fixScaleTrans() {
    fixTrans();
    matrix.getValues(m);
    if (getImageWidth() < viewWidth) {
    m[Matrix.MTRANS_X] = (viewWidth - getImageWidth()) / 2;
    }
   
    if (getImageHeight() < viewHeight) {
    m[Matrix.MTRANS_Y] = (viewHeight - getImageHeight()) / 2;
    }
    matrix.setValues(m);
    }

    private float getFixTrans(float trans, float viewSize, float contentSize) {
        float minTrans, maxTrans;

        if (contentSize <= viewSize) {
            minTrans = 0;
            maxTrans = viewSize - contentSize;
           
        } else {
            minTrans = viewSize - contentSize;
            maxTrans = 0;
        }

        if (trans < minTrans)
            return -trans + minTrans;
        if (trans > maxTrans)
            return -trans + maxTrans;
        return 0;
    }
   
    private float getFixDragTrans(float delta, float viewSize, float contentSize) {
        if (contentSize <= viewSize) {
            return 0;
        }
        return delta;
    }
   
    private float getImageWidth() {
    return matchViewWidth * normalizedScale;
    }
   
    private float getImageHeight() {
    return matchViewHeight * normalizedScale;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Drawable drawable = getDrawable();
        if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0) {
        setMeasuredDimension(0, 0);
        return;
        }
       
        int drawableWidth = drawable.getIntrinsicWidth();
        int drawableHeight = drawable.getIntrinsicHeight();
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        viewWidth = setViewSize(widthMode, widthSize, drawableWidth);
        viewHeight = setViewSize(heightMode, heightSize, drawableHeight);
       
        //
        // Set view dimensions
        //
        setMeasuredDimension(viewWidth, viewHeight);
       
        //
        // Fit content within view
        //
        fitImageToView();
    }
   
    /**
     * If the normalizedScale is equal to 1, then the image is made to fit the screen. Otherwise,
     * it is made to fit the screen according to the dimensions of the previous image matrix. This
     * allows the image to maintain its zoom after rotation.
     */
    private void fitImageToView() {
    Drawable drawable = getDrawable();
        if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0) {
        return;
        }
        if (matrix == null || prevMatrix == null) {
        return;
        }
       
        int drawableWidth = drawable.getIntrinsicWidth();
        int drawableHeight = drawable.getIntrinsicHeight();
   
    //
    // Scale image for view
    //
        float scaleX = (float) viewWidth / drawableWidth;
        float scaleY = (float) viewHeight / drawableHeight;
        float scale = Math.min(scaleX, scaleY);

        //
        // Center the image
        //
        float redundantYSpace = viewHeight - (scale * drawableHeight);
        float redundantXSpace = viewWidth - (scale * drawableWidth);
        matchViewWidth = viewWidth - redundantXSpace;
        matchViewHeight = viewHeight - redundantYSpace;
        if (normalizedScale == 1 || setImageCalledRecenterImage) {
        //
        // Stretch and center image to fit view
        //
        matrix.setScale(scale, scale);
        matrix.postTranslate(redundantXSpace / 2, redundantYSpace / 2);
        normalizedScale = 1;
        setImageCalledRecenterImage = false;
       
        } else {
        prevMatrix.getValues(m);
       
        //
        // Rescale Matrix after rotation
        //
        m[Matrix.MSCALE_X] = matchViewWidth / drawableWidth * normalizedScale;
        m[Matrix.MSCALE_Y] = matchViewHeight / drawableHeight * normalizedScale;
       
        //
        // TransX and TransY from previous matrix
        //
            float transX = m[Matrix.MTRANS_X];
            float transY = m[Matrix.MTRANS_Y];
           
            //
            // Width
            //
            float prevActualWidth = prevMatchViewWidth * normalizedScale;
            float actualWidth = getImageWidth();
            translateMatrixAfterRotate(Matrix.MTRANS_X, transX, prevActualWidth, actualWidth, prevViewWidth, viewWidth, drawableWidth);
           
            //
            // Height
            //
            float prevActualHeight = prevMatchViewHeight * normalizedScale;
            float actualHeight = getImageHeight();
            translateMatrixAfterRotate(Matrix.MTRANS_Y, transY, prevActualHeight, actualHeight, prevViewHeight, viewHeight, drawableHeight);
           
            //
            // Set the matrix to the adjusted scale and translate values.
            //
            matrix.setValues(m);
        }
        setImageMatrix(matrix);
    }
   
    /**
     * Set view dimensions based on layout params
     *
     * @param mode
     * @param size
     * @param drawableWidth
     * @return
     */
    private int setViewSize(int mode, int size, int drawableWidth) {
    int viewSize;
    switch (mode) {
case MeasureSpec.EXACTLY:
viewSize = size;
break;

case MeasureSpec.AT_MOST:
viewSize = Math.min(drawableWidth, size);
break;

case MeasureSpec.UNSPECIFIED:
viewSize = drawableWidth;
break;

default:
viewSize = size;
break;
}
    return viewSize;
    }
   
    /**
     * After rotating, the matrix needs to be translated. This function finds the area of image
     * which was previously centered and adjusts translations so that is again the center, post-rotation.
     *
     * @param axis Matrix.MTRANS_X or Matrix.MTRANS_Y
     * @param trans the value of trans in that axis before the rotation
     * @param prevImageSize the width/height of the image before the rotation
     * @param imageSize width/height of the image after rotation
     * @param prevViewSize width/height of view before rotation
     * @param viewSize width/height of view after rotation
     * @param drawableSize width/height of drawable
     */
    private void translateMatrixAfterRotate(int axis, float trans, float prevImageSize, float imageSize, int prevViewSize, int viewSize, int drawableSize) {
    if (imageSize < viewSize) {
        //
        // The width/height of image is less than the view's width/height. Center it.
        //
        m[axis] = (viewSize - (drawableSize * m[Matrix.MSCALE_X])) * 0.5f;
       
        } else if (trans > 0) {
        //
        // The image is larger than the view, but was not before rotation. Center it.
        //
        m[axis] = -((imageSize - viewSize) * 0.5f);
       
        } else {
        //
        // Find the area of the image which was previously centered in the view. Determine its distance
        // from the left/top side of the view as a fraction of the entire image's width/height. Use that percentage
        // to calculate the trans in the new view width/height.
        //
        float percentage = (Math.abs(trans) + (0.5f * prevViewSize)) / prevImageSize;
        m[axis] = -((percentage * imageSize) - (viewSize * 0.5f));
        }
    }
   
    private void setState(State state) {
    this.state = state;
    }
   
    /**
     * Gesture Listener detects a single click or long click and passes that on
     * to the view's listener.
     * @author Ortiz
     *
     */
    private class GestureListener extends GestureDetector.SimpleOnGestureListener {
   
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e)
        {
        return performClick();
        }
       
        @Override
        public void onLongPress(MotionEvent e)
        {
        performLongClick();
        }
       
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
        {
        if (fling != null) {
        //
        // If a previous fling is still active, it should be cancelled so that two flings
        // are not run simultaenously.
        //
        fling.cancelFling();
        }
        fling = new Fling((int) velocityX, (int) velocityY);
        compatPostOnAnimation(fling);
        return super.onFling(e1, e2, velocityX, velocityY);
        }
       
        @Override
        public boolean onDoubleTap(MotionEvent e) {
        boolean consumed = false;
        if (state == NONE) {
        float targetZoom = (normalizedScale == minScale) ? maxScale : minScale;
        DoubleTapZoom doubleTap = new DoubleTapZoom(targetZoom, e.getX(), e.getY(), false);
        compatPostOnAnimation(doubleTap);
        consumed = true;
        }
        return consumed;
        }
    }
   
    /**
     * Responsible for all touch events. Handles the heavy lifting of drag and also sends
     * touch events to Scale Detector and Gesture Detector.
     * @author Ortiz
     *
     */
    private class TouchImageViewListener implements OnTouchListener {
   
    //
        // Remember last point position for dragging
        //
        private PointF last = new PointF();
   
    @Override
        public boolean onTouch(View v, MotionEvent event) {
            mScaleDetector.onTouchEvent(event);
            mGestureDetector.onTouchEvent(event);
            PointF curr = new PointF(event.getX(), event.getY());
           
            if (state == NONE || state == DRAG || state == FLING) {
           switch (event.getAction()) {
               case MotionEvent.ACTION_DOWN:
                last.set(curr);
                   if (fling != null)
                    fling.cancelFling();
                   setState(DRAG);
                   break;
                 
               case MotionEvent.ACTION_MOVE:
                   if (state == DRAG) {
                       float deltaX = curr.x - last.x;
                       float deltaY = curr.y - last.y;
                       float fixTransX = getFixDragTrans(deltaX, viewWidth, getImageWidth());
                       float fixTransY = getFixDragTrans(deltaY, viewHeight, getImageHeight());
                       matrix.postTranslate(fixTransX, fixTransY);
                       fixTrans();
                       last.set(curr.x, curr.y);
                   }
                   break;

               case MotionEvent.ACTION_UP:
               case MotionEvent.ACTION_POINTER_UP:
                   setState(NONE);
                   break;
           }
            }
           
            setImageMatrix(matrix);
            //
            // indicate event was handled
            //
            return true;
        }
    }

    /**
     * ScaleListener detects user two finger scaling and scales image.
     * @author Ortiz
     *
     */
    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            setState(ZOOM);
            return true;
        }

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
        scaleImage(detector.getScaleFactor(), detector.getFocusX(), detector.getFocusY(), true);
            return true;
        }
       
        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
        super.onScaleEnd(detector);
        setState(NONE);
        boolean animateToZoomBoundary = false;
        float targetZoom = normalizedScale;
        if (normalizedScale > maxScale) {
        targetZoom = maxScale;
        animateToZoomBoundary = true;
       
        } else if (normalizedScale < minScale) {
        targetZoom = minScale;
        animateToZoomBoundary = true;
        }
       
        if (animateToZoomBoundary) {
        DoubleTapZoom doubleTap = new DoubleTapZoom(targetZoom, viewWidth / 2, viewHeight / 2, true);
        compatPostOnAnimation(doubleTap);
        }
        }
    }
   
    private void scaleImage(float deltaScale, float focusX, float focusY, boolean stretchImageToSuper) {
   
    float lowerScale, upperScale;
    if (stretchImageToSuper) {
    lowerScale = superMinScale;
    upperScale = superMaxScale;
   
    } else {
    lowerScale = minScale;
    upperScale = maxScale;
    }
   
    float origScale = normalizedScale;
        normalizedScale *= deltaScale;
        if (normalizedScale > upperScale) {
            normalizedScale = upperScale;
            deltaScale = upperScale / origScale;
        } else if (normalizedScale < lowerScale) {
            normalizedScale = lowerScale;
            deltaScale = lowerScale / origScale;
        }
       
        matrix.postScale(deltaScale, deltaScale, focusX, focusY);
        fixScaleTrans();
    }
   
    /**
     * DoubleTapZoom calls a series of runnables which apply
     * an animated zoom in/out graphic to the image.
     * @author Ortiz
     *
     */
    private class DoubleTapZoom implements Runnable {
   
    private long startTime;
    private static final float ZOOM_TIME = 500;
    private float startZoom, targetZoom;
    private float bitmapX, bitmapY;
    private boolean stretchImageToSuper;
    private AccelerateDecelerateInterpolator interpolator = new AccelerateDecelerateInterpolator();
    private PointF startTouch;
    private PointF endTouch;

    DoubleTapZoom(float targetZoom, float focusX, float focusY, boolean stretchImageToSuper) {
    setState(ANIMATE_ZOOM);
    startTime = System.currentTimeMillis();
    this.startZoom = normalizedScale;
    this.targetZoom = targetZoom;
    this.stretchImageToSuper = stretchImageToSuper;
    PointF bitmapPoint = transformCoordTouchToBitmap(focusX, focusY, false);
    this.bitmapX = bitmapPoint.x;
    this.bitmapY = bitmapPoint.y;
   
    //
    // Used for translating image during scaling
    //
    startTouch = transformCoordBitmapToTouch(bitmapX, bitmapY);
    endTouch = new PointF(viewWidth / 2, viewHeight / 2);
    }

@Override
public void run() {
float t = interpolate();
float deltaScale = calculateDeltaScale(t);
scaleImage(deltaScale, bitmapX, bitmapY, stretchImageToSuper);
translateImageToCenterTouchPosition(t);
fixScaleTrans();
setImageMatrix(matrix);

if (t < 1f) {
//
// We haven't finished zooming
//
compatPostOnAnimation(this);

} else {
//
// Finished zooming
//
setState(NONE);
}
}

/**
* Interpolate between where the image should start and end in order to translate
* the image so that the point that is touched is what ends up centered at the end
* of the zoom.
* @param t
*/
private void translateImageToCenterTouchPosition(float t) {
float targetX = startTouch.x + t * (endTouch.x - startTouch.x);
float targetY = startTouch.y + t * (endTouch.y - startTouch.y);
PointF curr = transformCoordBitmapToTouch(bitmapX, bitmapY);
matrix.postTranslate(targetX - curr.x, targetY - curr.y);
}

/**
* Use interpolator to get t
* @return
*/
private float interpolate() {
long currTime = System.currentTimeMillis();
float elapsed = (currTime - startTime) / ZOOM_TIME;
elapsed = Math.min(1f, elapsed);
return interpolator.getInterpolation(elapsed);
}

/**
* Interpolate the current targeted zoom and get the delta
* from the current zoom.
* @param t
* @return
*/
private float calculateDeltaScale(float t) {
float zoom = startZoom + t * (targetZoom - startZoom);
return zoom / normalizedScale;
}
    }
   
    /**
     * This function will transform the coordinates in the touch event to the coordinate
     * system of the drawable that the imageview contain
     * @param x x-coordinate of touch event
     * @param y y-coordinate of touch event
     * @param clipToBitmap Touch event may occur within view, but outside image content. True, to clip return value
     * to the bounds of the bitmap size.
     * @return Coordinates of the point touched, in the coordinate system of the original drawable.
     */
    private PointF transformCoordTouchToBitmap(float x, float y, boolean clipToBitmap) {
         matrix.getValues(m);
         float origW = getDrawable().getIntrinsicWidth();
         float origH = getDrawable().getIntrinsicHeight();
         float transX = m[Matrix.MTRANS_X];
         float transY = m[Matrix.MTRANS_Y];
         float finalX = ((x - transX) * origW) / getImageWidth();
         float finalY = ((y - transY) * origH) / getImageHeight();
       
         if (clipToBitmap) {
        finalX = Math.min(Math.max(x, 0), origW);
        finalY = Math.min(Math.max(y, 0), origH);
         }
       
         return new PointF(finalX , finalY);
    }
   
    /**
     * Inverse of transformCoordTouchToBitmap. This function will transform the coordinates in the
     * drawable's coordinate system to the view's coordinate system.
     * @param bx x-coordinate in original bitmap coordinate system
     * @param by y-coordinate in original bitmap coordinate system
     * @return Coordinates of the point in the view's coordinate system.
     */
    private PointF transformCoordBitmapToTouch(float bx, float by) {
        matrix.getValues(m);      
        float origW = getDrawable().getIntrinsicWidth();
        float origH = getDrawable().getIntrinsicHeight();
        float px = bx / origW;
        float py = by / origH;
        float finalX = m[Matrix.MTRANS_X] + getImageWidth() * px;
        float finalY = m[Matrix.MTRANS_Y] + getImageHeight() * py;
        return new PointF(finalX , finalY);
    }
   
    /**
     * Fling launches sequential runnables which apply
     * the fling graphic to the image. The values for the translation
     * are interpolated by the Scroller.
     * @author Ortiz
     *
     */
    private class Fling implements Runnable {
   
        Scroller scroller;
    int currX, currY;
   
    Fling(int velocityX, int velocityY) {
    setState(FLING);
    scroller = new Scroller(context);
    matrix.getValues(m);
   
    int startX = (int) m[Matrix.MTRANS_X];
    int startY = (int) m[Matrix.MTRANS_Y];
    int minX, maxX, minY, maxY;
   
    if (getImageWidth() > viewWidth) {
    minX = viewWidth - (int) getImageWidth();
    maxX = 0;
   
    } else {
    minX = maxX = startX;
    }
   
    if (getImageHeight() > viewHeight) {
    minY = viewHeight - (int) getImageHeight();
    maxY = 0;
   
    } else {
    minY = maxY = startY;
    }
   
    scroller.fling(startX, startY, (int) velocityX, (int) velocityY, minX,
                    maxX, minY, maxY);
    currX = startX;
    currY = startY;
    }
   
    public void cancelFling() {
    if (scroller != null) {
    setState(NONE);
    scroller.forceFinished(true);
    }
    }
   
@Override
public void run() {
if (scroller.isFinished()) {
        scroller = null;
        return;
        }

if (scroller.computeScrollOffset()) {
        int newX = scroller.getCurrX();
           int newY = scroller.getCurrY();
           int transX = newX - currX;
           int transY = newY - currY;
           currX = newX;
           currY = newY;
           matrix.postTranslate(transX, transY);
           fixTrans();
           setImageMatrix(matrix);
           compatPostOnAnimation(this);
        }
}
    }
   
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void compatPostOnAnimation(Runnable runnable) {
    if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
            postOnAnimation(runnable);
           
        } else {
            postDelayed(runnable, 1000/60);
        }
    }
   
    private void printMatrixInfo() {
    matrix.getValues(m);
    Log.d(DEBUG, "Scale: " + m[Matrix.MSCALE_X] + " TransX: " + m[Matrix.MTRANS_X] + " TransY: " + m[Matrix.MTRANS_Y]);
    }
}

2)TouchImageViewActivity
package com.example.touch;

import android.app.Activity;
import android.os.Bundle;


public class TouchImageViewActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TouchImageView img = (TouchImageView) findViewById(R.id.img);
        img.setMaxZoom(4);
    }
}

3)main.xml
========
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
   
<com.example.touch.TouchImageView
   android:id="@+id/img"
   android:src="@drawable/grand_canyon"
   android:layout_width="match_parent"
   android:layout_height="match_parent" />
   
</LinearLayout>

Thursday, 6 March 2014

Horizontal Scrollview with dynamically adding images

1) in xml
======
<LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="0.55"
                    android:orientation="vertical" >

                    <HorizontalScrollView
                        android:id="@+id/horizontalScrollView2"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginTop="5dp"
                        android:fillViewport="true" >

                        <LinearLayout
                            android:id="@+id/llGallery"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="bottom"
                            android:gravity="bottom"
                            android:orientation="horizontal"
                            android:scrollbars="vertical" >
                        </LinearLayout>
                    </HorizontalScrollView>
                </LinearLayout>
2) In Activity

private LinearLayout llGallery;
private ImageView t1ourstoreContentImg;
private ImageView t1ourstoreContentImg;

public class ABC extends activity{

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);

                 llGallery = (LinearLayout) v.findViewById(R.id.llGallery);


JSONArray product_images = mDetails.getJSONArray("product_images");
int imagesLength = product_images.length();
System.out.println("imagesLength=" + imagesLength);
for (int i = 0; i < imagesLength; i++) {
String url = product_images.getJSONObject(i).getString("url");

ImageView imagView = new ImageView(getActivity());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
100, 100);
lp.setMargins(3, 3, 3, 3);
imagView.setLayoutParams(lp);
imagView.setTag(url);

mImageFetcher.loadImage(product_images.getJSONObject(i)
.getString("url_thumb"), imagView);
imagView.setAdjustViewBounds(true);
llGallery.addView(imagView);

imagView.setOnClickListener(new onMyClick(url,t1ourstoreContentImg));
                                mImageFetcher.loadImage(product_images.getJSONObject(0).getString("url"),
t1ourstoreContentImg);
}
}
            public class onMyClick implements OnClickListener {

private final String mString;
public ImageView mImageView;
public onMyClick(String mString,ImageView mImageView) {
this.mString = mString;
this.mImageView = mImageView;
}

@Override
public void onClick(final View v) {
mImageFetcher.loadImage(mString, t1ourstoreContentImg);
}

}

}

Gallery Example with left align

public class GalleryAdapter extends BaseAdapter {
// private LayoutInflater mLayoutInflater;
private ArrayList<String> alstImage;
private Context mContext;

public GalleryAdapter(Context mContext, ArrayList<String> alstImage) {
// this.mLayoutInflater = LayoutInflater.from(mContext);
this.mContext = mContext;
this.alstImage = alstImage;
}

public int getCount() {
return alstProductImageThumb.size();
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

@SuppressWarnings("deprecation")
public View getView(int position, View convertView, ViewGroup parent) {
// convertView = mLayoutInflater.inflate(R.layout.gallery_adapter,
// null);
// ImageView mGalleryImage =
// (ImageView)convertView.findViewById(R.id.mGalleryImage);
// System.out.println("alstProductImageThumb="+alstImage.get(position).toString());
// mImageFetcher.loadImage(alstProductImageThumb.get(position),
// mGalleryImage);
// return convertView;
ImageView imagView = new ImageView(mContext);
System.out.println("alstProductImageThumb="
+ alstProductImageThumb.get(position).toString());
mImageFetcher.loadImage(alstProductImageThumb.get(position),
imagView);
// i.setImageResource(alstProductImageURL.get(position));
imagView.setAdjustViewBounds(true);
imagView.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// i.setBackgroundResource(R.drawable.picture_frame);
return imagView;

}
}


//In Gallery class
gallery = (Gallery) v.findViewById(R.id.gallery);
DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
MarginLayoutParams mlp = (MarginLayoutParams) gallery.getLayoutParams();
        mlp.setMargins((int) -(metrics.widthPixels/3), mlp.topMargin, mlp.rightMargin,mlp.bottomMargin);


mGalleryAdapter = new GalleryAdapter(getActivity(),
alstProductImageThumb);
gallery.setAdapter(mGalleryAdapter);
gallery.setSelection(1);
gallery.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
mImageFetcher.loadImage(alstProductImageURL.get(arg2)
.toString(), t1ContentImg);
}
});

// In xml
======
               <Gallery
                            android:id="@+id/gallery"
                            android:layout_width="match_parent"
                            android:layout_height="60dp"
                            android:background="@android:color/transparent"
                            android:spacing="5dp" />

Sunday, 16 February 2014

Zoom image in android

package com.example.testproject;
import com.example.testproject.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;

@SuppressLint("NewApi")
public class PinchZoom extends Activity {
 View mainView = null;

 // Remember some things for zooming
 PointF start = new PointF();
 PointF mid = new PointF();

 float oldDist = 1f;
 PointF oldDistPoint = new PointF();

 public static String TAG = "ZOOM";

 static final int NONE = 0;
 static final int DRAG = 1;
 static final int ZOOM = 2;
 int mode = NONE;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.zoom );
  mainView = (LinearLayout) findViewById(R.id.lytMain);

  Button buttonZoomOut = (Button) findViewById(R.id.btnZoomOut);
  Button buttonNormal = (Button) findViewById(R.id.btnZoom);
  Button buttonZoomIn = (Button) findViewById(R.id.btnZoomIn);

  buttonZoomOut.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    zoom(0.5f, 0.5f, new PointF(0, 0));
   }
  });
  buttonNormal.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    zoom(1f, 1f, new PointF(0, 0));
   }
  });
  buttonZoomIn.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    zoom(2f, 2f, new PointF(0, 0));
   }
  });

  mainView.setOnTouchListener(new OnTouchListener() {
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    Log.d(TAG, "mode=DRAG");
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
     start.set(event.getX(), event.getY());
     Log.d(TAG, "mode=DRAG");
     mode = DRAG;

     break;
    case MotionEvent.ACTION_POINTER_DOWN:
     oldDist = spacing(event);
     oldDistPoint = spacingPoint(event);
     Log.d(TAG, "oldDist=" + oldDist);
     if (oldDist > 10f) {
      midPoint(mid, event);
      mode = ZOOM;
      Log.d(TAG, "mode=ZOOM");
     }
     System.out.println("current time :" + System.currentTimeMillis());
     break;// return !gestureDetector.onTouchEvent(event);
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
     Log.d(TAG, "mode=NONE");
     mode = NONE;
     break;
    case MotionEvent.ACTION_MOVE:
     if (mode == DRAG) {

     } else if (mode == ZOOM) {
      PointF newDist = spacingPoint(event);
      float newD = spacing(event);
      Log.e(TAG, "newDist=" + newDist);
      float[] old = new float[9];
      float[] newm = new float[9];
      Log.e(TAG, "x=" + old[0] + ":&:" + old[2]);
      Log.e(TAG, "y=" + old[4] + ":&:" + old[5]);
      float scale = newD / oldDist;
      float scalex = newDist.x / oldDistPoint.x;
      float scaley = newDist.y / oldDistPoint.y;
      zoom(scale, scale, start);
     }
     break;
    }
    return true;
   }
  });
 }

 /**
  * zooming is done from here
  */
 @SuppressLint("NewApi")
public void zoom(Float scaleX, Float scaleY, PointF pivot) {
  mainView.setPivotX(pivot.x);
  mainView.setPivotY(pivot.y);
  mainView.setScaleX(scaleX);
  mainView.setScaleY(scaleY);
 }

 /**
  * space between the first two fingers
  */
 private float spacing(MotionEvent event) {
  // ...
  float x = event.getX(0) - event.getX(1);
  float y = event.getY(0) - event.getY(1);
  return FloatMath.sqrt(x * x + y * y);
 }

 private PointF spacingPoint(MotionEvent event) {
  PointF f = new PointF();
  f.x = event.getX(0) - event.getX(1);
  f.y = event.getY(0) - event.getY(1);
  return f;
 }

 /**
  * the mid point of the first two fingers
  */
 private void midPoint(PointF point, MotionEvent event) {
  // ...
  float x = event.getX(0) + event.getX(1);
  float y = event.getY(0) + event.getY(1);
  point.set(x / 2, y / 2);
 }
}

Other Links
========
http://stackoverflow.com/questions/13398288/image-zoom-issue-with-universal-image-loader-and-view-pager

Monday, 10 February 2014

Custom Dialog Android

1)custom_dialog_calcel.xml
===================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="280dp"
    android:layout_height="140dp"
    android:gravity="center"
    android:layout_gravity="center"
    android:background="@drawable/ractangle_button_background"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtMsg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="15dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Your booking has been canceled successfully"
        android:textColor="@android:color/white"
        android:textSize="14sp" />

    <Button
        android:id="@+id/btnOk"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:layout_marginBottom="15dp"
        android:background="@drawable/ractangle_button"
        android:gravity="center"
        android:padding="10dp"
        android:text="OK"
        android:textColor="@color/blue" />

</LinearLayout>

2)custom_dialog.xml
==============
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/book_now_dialog_width"
    android:layout_height="@dimen/book_now_dialog_height"
    android:layout_gravity="center"
    android:background="@android:color/transparent" >

    <!-- Contents will go here.. -->

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="10dp"
        android:background="@color/blue"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_margin="1dp"
            android:background="@android:color/white"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/txtHeading"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:background="@color/lemon_yellow"
                android:gravity="left"
                android:padding="5dp"
                android:text="Class"
                android:textColor="@color/blue"
                android:textSize="18dp" />

            <ScrollView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_margin="10dp"
                    android:layout_weight="1"
                    android:orientation="vertical" >

                    <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal" >

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/class_name"
                            android:textColor="@color/blue"
                            android:textSize="14sp" />

                        <TextView
                            android:id="@+id/txtClassName"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Message"
                            android:textColor="@color/Black"
                            android:textSize="14sp" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:orientation="horizontal" >

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/teacher"
                            android:textColor="@color/blue"
                            android:textSize="14sp" />

                        <TextView
                            android:id="@+id/txtTeacher"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Message"
                            android:textColor="@color/Black"
                            android:textSize="14sp" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:orientation="horizontal" >

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/date"
                            android:textColor="@color/blue"
                            android:textSize="14sp" />

                        <TextView
                            android:id="@+id/txtDate"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Message"
                            android:textColor="@color/Black"
                            android:textSize="14sp" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:orientation="horizontal" >

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/time"
                            android:textColor="@color/blue"
                            android:textSize="14sp" />

                        <TextView
                            android:id="@+id/txtTime"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Message"
                            android:textColor="@color/Black"
                            android:textSize="14sp" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:orientation="horizontal" >

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/venue"
                            android:textColor="@color/blue"
                            android:textSize="14sp" />

                        <TextView
                            android:id="@+id/txtVenue"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Message"
                            android:textColor="@color/Black"
                            android:textSize="14sp" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:orientation="horizontal" >

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/class_type"
                            android:textColor="@color/blue"
                            android:textSize="14sp" />

                        <TextView
                            android:id="@+id/txtClassType"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Message"
                            android:textColor="@color/Black"
                            android:textSize="14sp" />
                    </LinearLayout>
                </LinearLayout>
            </ScrollView>

            <Button
                android:id="@+id/btnBookNow"
                android:layout_width="150dp"
                android:layout_height="40dp"
                android:layout_gravity="center|bottom"
                android:layout_margin="15dp"
                android:background="@drawable/ractangle_button"
                android:gravity="center"
                android:text="Book Now"
                android:textColor="@color/blue"
                android:textSize="14sp"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>

    <Button
        android:id="@+id/btnClose"
        android:layout_width="@dimen/btn_close_width"
        android:layout_height="@dimen/btn_close_height"
        android:layout_alignParentRight="true"
        android:layout_gravity="right|center"
        android:layout_marginRight="3dp"
        android:background="@drawable/round_button_background"
        android:gravity="center_vertical|center_horizontal"
        android:text="X"
        android:textColor="@color/blue"
        android:textSize="12sp"
        android:textStyle="bold" />

</RelativeLayout>
3) in drawable folder put
A) ractangle_button_background.xml
     =======================
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#93772C"/>    

<!--     <stroke android:width="3dp" -->
<!--             android:color="#ff000000" -->
<!--             /> -->

    <padding android:left="1dp"
             android:top="1dp"
             android:right="1dp"
             android:bottom="1dp"
             /> 
    <corners android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp" 
     android:topLeftRadius="10dp" android:topRightRadius="10dp"/> 
</shape>

B)ractangle_button.xml
===================
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="@color/lemon_yellow"/>    

<!--     <stroke android:width="3dp" -->
<!--             android:color="#ff000000" -->
<!--             /> -->

    <padding android:left="1dp"
             android:top="1dp"
             android:right="1dp"
             android:bottom="1dp"/> 

    <corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" android:topRightRadius="7dp"/> 
</shape> 

C) round_button_background.xml
     =====================
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
    <solid android:color="@color/lemon_yellow" />
    <stroke android:width="2dp" android:color="@color/blue" />
</shape>


4)
public void dialogCancelBooking() {
final Dialog dialog = new Dialog (this,android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.custom_dialog_cancel);

Button btnOk = (Button) dialog.findViewById(R.id.btnOk);
TextView txtMsg = (TextView) dialog
.findViewById(R.id.txtMsg);

btnOk.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});

dialog.show();
}

Wednesday, 5 February 2014

Image blur issue

// http://www.androiddevelopersolution.com/2013/12/android-make-image-sharper-android.html
Image Before:


Image After :


 1. ImageHelper.java : This is our helper class which helps in processing image
     sharper/



package com.exampl.imagerun;

import android.graphics.Bitmap;
import android.graphics.Color;
 
public class ImageHelper
{
    public static final int SIZE = 3;
 
    public double[][] Matrix;
    public double Factor = 1;
    public double Offset = 1;
 
   //Constructor with argument of size
    public ImageHelper(int size) {
        Matrix = new double[size][size];
    }
 
    public void setAll(double value) {
        for (int x = 0; x < SIZE; ++x) {
            for (int y = 0; y < SIZE; ++y) {
                Matrix[x][y] = value;
            }
        }
    }
 
    public void applyConfig(double[][] config) {
        for(int x = 0; x < SIZE; ++x) {
            for(int y = 0; y < SIZE; ++y) {
                Matrix[x][y] = config[x][y];
            }
        }
    }
 
    public static Bitmap computeConvolution3x3(Bitmap src, ImageHelper matrix) {
        int width = src.getWidth();
        int height = src.getHeight();
        Bitmap result = Bitmap.createBitmap(width, height, src.getConfig());
 
        int A, R, G, B;
        int sumR, sumG, sumB;
        int[][] pixels = new int[SIZE][SIZE];
 
        for(int y = 0; y < height - 2; ++y) {
            for(int x = 0; x < width - 2; ++x) {
 
                // get pixel matrix
                for(int i = 0; i < SIZE; ++i) {
                    for(int j = 0; j < SIZE; ++j) {
                        pixels[i][j] = src.getPixel(x + i, y + j);
                    }
                }
 
                // get alpha of center pixel
                A = Color.alpha(pixels[1][1]);
 
                // init color sum
                sumR = sumG = sumB = 0;
 
                // get sum of RGB on matrix
                for(int i = 0; i < SIZE; ++i) {
                    for(int j = 0; j < SIZE; ++j) {
                        sumR += (Color.red(pixels[i][j]) * matrix.Matrix[i][j]);
                        sumG += (Color.green(pixels[i][j]) * matrix.Matrix[i][j]);
                        sumB += (Color.blue(pixels[i][j]) * matrix.Matrix[i][j]);
                    }
                }
 
                // get final Red
                R = (int)(sumR / matrix.Factor + matrix.Offset);
                if(R < 0) { R = 0; }
                else if(R > 255) { R = 255; }
 
                // get final Green
                G = (int)(sumG / matrix.Factor + matrix.Offset);
                if(G < 0) { G = 0; }
                else if(G > 255) { G = 255; }
 
                // get final Blue
                B = (int)(sumB / matrix.Factor + matrix.Offset);
                if(B < 0) { B = 0; }
                else if(B > 255) { B = 255; }
 
                // apply new pixel
                result.setPixel(x + 1, y + 1, Color.argb(A, R, G, B));
            }
        }
 
        // final image
        return result;
    }
}

 Now, In our Activity class we need to add following code:

   
    ImageView imageView=(ImageView)findViewById(R.id.image);


Now set image on image view

   
    image.setImageBitmap(sharpenImage(BitmapFactory.
           decodeResource(getResources(),images[i]),12));


And, here we are using Convolution Matrix Theorem to make image sharper.


       public Bitmap sharpenImage(Bitmap src, double weight) {
    // set sharpness configuration
    double[][] SharpConfig = new double[][] { { 0, -2, 0 },
    { -2, weight, -2 }, { 0, -2, 0 } };
    // create convolution matrix instance
    ImageHelper convMatrix = new ImageHelper(3);
    // apply configuration
    convMatrix.applyConfig(SharpConfig);
    // set weight according to factor
    convMatrix.Factor = weight - 8;
    return ImageHelper.computeConvolution3x3(src, convMatrix);
 }

Monday, 20 January 2014

Load Image from URL

http://www.androidhive.info/2012/07/android-loading-image-from-url-http/