android. FIG loading Long

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Scroller;

import java.io.IOException;
import java.io.InputStream;


public class BigView extends View implements GestureDetector.OnGestureListener, View.OnTouchListener {
private static final String TAG = "BigView";
private Scroller mScroller;
private GestureDetector mGestureDetector;
private BitmapFactory.Options mOptions;
private Rect mRect;
private int mImageWidth;
private int mImageHeight;
private BitmapRegionDecoder mDecoder;
private int mViewWidth;
private int mViewHeight;
private float mScale;
private Bitmap bitmap;

public BigView(Context context) {
this(context, null, 0);
}

public BigView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}

public BigView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
Super (context, attrs, defStyleAttr);
// specified rectangular area to be loaded
mRect = new new Rect ();
// decoded picture configuration
mOptions new new BitmapFactory.Options = ();
// gesture
mGestureDetector = new GestureDetector (context, this );
setOnTouchListener (the this);
// slide help
mScroller = Scroller new new (context);
}

/ **
* image input by a user inputs a stream
*
* @param IS
* /
public void the setImage (the InputStream IS) {
// first read the original image width, height
mOptions.inJustDecodeBounds = to true;
BitmapFactory.decodeStream (iS, null, mOptions);
mImageWidth = mOptions.outWidth;
mImageHeight = mOptions.outHeight;
// multiplexing the multiplexed memory
mOptions.inMutable = true ;
// set the pixel format of RGB565
mOptions.inPreferredConfig = Bitmap.Config.RGB_565;
mOptions.inJustDecodeBounds = to false;
// Create area decoder for decoding the image area
the try {
mDecoder = BitmapRegionDecoder.newInstance (IS, to false);
} the catch (IOException E) {
e.printStackTrace ();
}
the requestLayout ();
}

/ **
* measurement view size
*
* @param widthMeasureSpec
* @param heightMeasureSpec
* /
@Override
protected void onMeasure (widthMeasureSpec int, int heightMeasureSpec) {
super.onMeasure (widthMeasureSpec, heightMeasureSpec);
// get a view of the size measurement
mViewWidth getMeasuredWidth = ();
mViewHeight getMeasuredHeight = ();
// null if the decoder is not provided showing had to be a realistic picture
IF (mDecoder == null) {
return;
}
// determine the image area to be loaded
mRect.left = 0;
mRect.top = 0;
mRect.right mImageWidth =;
// Log.e (the TAG, "scaling factor =" + (1.0f mViewWidth * / * mImageWidth 1.0f));
// Log.e (the TAG, "scaling factor =" + (mViewHeight * 1.0f / * 1.0f mImageHeight));
// get the scaling factor
Mscale mViewWidth = / (a float) mImageWidth;

// needs to be loaded high zoom factor = * high view view
// = X * Mscale mViewHeight
mRect.bottom = (int) ( mViewHeight / Mscale);
Log.e (the TAG, "= L" + mRect.left);
Log.e (the TAG, "T =" + mRect.top);
Log.e (the TAG, "R & lt =" + mRect. right);
Log.e (the TAG, "B =" + mRect.bottom);
}

/ **
* the picture painted
*
@Param Canvas *
* /
@Override
protected void the onDraw (the Canvas Canvas) {
super.onDraw (Canvas);
// null if the decoder is not provided showing had to be a realistic picture
IF (mDecoder == null) {
return;
}
/ / multiplexing spend a Bitmap
Log.e (the TAG, "a complex spend = Bitmap" Bitmap +);
mOptions.inBitmap = Bitmap;
// specified region decoding
Bitmap = mDecoder.decodeRegion (mRect, mOptions);
// matrix image to zoom using
the matrix the matrix the matrix = new new ();
matrix.setScale (Mscale, Mscale);
// draw out

Canvas.drawBitmap (Bitmap, the matrix, null);
}


/ **
* finger pressed screen callback
* E @param
* @return
* /
@Override
public Boolean onDown (the MotionEvent E) {
// If not stopped sliding forced stop
IF (mScroller.isFinished ()!) {
MScroller.forceFinished (to true);
}
// continue to receive subsequent events
return to true;
}

@Override
public void onShowPress (the MotionEvent E) {

}

@Override
boolean onSingleTapUp public (MotionEvent E) {
return false;
}


@Override
public void onLongPress (MotionEvent E) {

}

/ **
* do not leave the screen and drag your finger
* @param e1 event fingers press down - get the coordinates of the beginning of the
* @ param e2 current gesture event - to obtain the current coordinates
* @param distanceX x-axis direction distance
* @param distanceY y direction distance
* @return
* /
@Override
boolean onScroll public (MotionEvent E1, MotionEvent e2, distanceX float, float distanceY) {
// your finger from the bottom up pictures up distanceY also negative, top and bottom at minus
// finger down from the top down but also pictures distanceY is a positive number, plus the top and bottom
// load change picture region
mRect.offset (0, (int) distanceY );
high picture // bottom is greater than, or less than zero the top
if (mRect.bottom> mImageHeight) {
= mImageHeight mRect.bottom;
mRect.top mImageHeight- = (int) (mViewHeight / Mscale);
}
IF (mRect.top <0) {
mRect.top = 0;
mRect.bottom = (int) (mViewHeight / Mscale);
}
// redraw
the invalidate ();
return to false;
}

/ **
* inertial sliding the finger from the screen
* @param E1
* @param E2
* @Param velocityX per second x-direction movement of the pixel
* @param velocityY Y
* @return
* /
@Override
public Boolean onFling (the MotionEvent E1, E2 the MotionEvent, velocityX a float, a float velocityY) {
/ **
* startX: start of sliding x-coordinate
* startY: slide starting y-coordinate
* two speed
* minX: minimum value of the x-direction
* max maximum
* y
* /
// calculator
mScroller.fling (0, mRect.top,
0, (int) -velocityY ,
0,0,0,
mImageHeight - (int) (mViewHeight / Mscale));
return to false;
}

// Get results and redraw
@Override
public void computeScroll () {
// calculation has ended return
IF (mScroller.isFinished ()) {
return;
}
// true indicates that the current is not completed animation
IF (mScroller.computeScrollOffset ()) {
//
mRect.top mScroller.getCurrY = ();
mRect.bottom mRect.top + = (int) (mViewHeight / Mscale);
the invalidate ();
}
}

@Override
public Boolean OnTouch (View V, the MotionEvent Event) {
// referred gesture processing
return mGestureDetector.onTouchEvent (Event);
}

————————————————

Guess you like

Origin www.cnblogs.com/hyhy904/p/11426870.html