Androidはどのように大きいピクチャーをロードします


ステップ1:ジェスチャーベースクラスを作成するには
public abstract class BaseGuestureDetector {

    /**        */
    protected boolean mIsGestureMoving;
    /**       */
    protected MotionEvent mPreMotionEvent;
    /**       */
    protected MotionEvent mCurrentMotionEvent;

    public Context mContext;

    public BaseGuestureDetector(Context context){
        mContext = context;
    }

    public boolean onTouchEvent(MotionEvent event){
        if (!mIsGestureMoving) {
            handleStartProgressEvent(event);
        }else {
            handleInProgressEvent(event);
        }
        return true;
    }
    protected abstract void handleInProgressEvent(MotionEvent event);
    protected abstract void handleStartProgressEvent(MotionEvent event);
    protected abstract void updateStateByEvent(MotionEvent event);

    protected void resetState(){
        if (mPreMotionEvent != null) {
            mPreMotionEvent.recycle();
            mPreMotionEvent = null;
        }
        if (mCurrentMotionEvent != null) {
            mCurrentMotionEvent.recycle();
            mCurrentMotionEvent = null;
        }
        mIsGestureMoving = false;
    }
}

ステップ2:ベースクラスに基づいてジェスチャークラスを作成する
public class MoveGestureDetector extends BaseGuestureDetector {
    /**         */
    private PointF mCurrentPointF;
    /**         */
    private PointF mPrePointF;
    /**         ,    */
    private PointF mExtenalPointer = new PointF();

    private OnMoveGestureListener mListener;

    public MoveGestureDetector(Context context, OnMoveGestureListener listener) {
        super(context);
        this.mListener = listener;
    }

    /** *           */
    @Override
    protected void handleInProgressEvent(MotionEvent event) {
        int action = event.getAction() & MotionEvent.ACTION_MASK;
        switch (action) {
        //         
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            mListener.onMoveEnd(this);
            resetState();
            break;
        //     
        case MotionEvent.ACTION_MOVE:
            updateStateByEvent(event);
            boolean move = mListener.onMove(this);
            if (move) {
                mPreMotionEvent.recycle();
                mPreMotionEvent = MotionEvent.obtain(event);
            }
            break;

        default:
            break;
        }

    }

    /** *         */
    @Override
    protected void handleStartProgressEvent(MotionEvent event) {
        int action = event.getAction() & MotionEvent.ACTION_MASK;
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            //        CANCEL or UP ,    
            resetState();
            //    
            mPreMotionEvent = MotionEvent.obtain(event);
            updateStateByEvent(event);
            break;

        case MotionEvent.ACTION_MOVE:
            mIsGestureMoving = mListener.onMoveBegin(this);
            break;
        default:
            break;
        }
    }

    @Override
    protected void updateStateByEvent(MotionEvent event) {
        final MotionEvent prev = mPreMotionEvent;
        //         
        mPrePointF = caculateFocalPointer(prev);
        mCurrentPointF = caculateFocalPointer(event);

        Log.e("TAG", mPrePointF.toString() + " , " + mCurrentPointF);
        //        ,          
        boolean mSkipThisMoveEvent = prev.getPointerCount() != event.getPointerCount();
        //          0
        mExtenalPointer.x = mSkipThisMoveEvent ? 0 : mCurrentPointF.x - mPrePointF.x;
        mExtenalPointer.y = mSkipThisMoveEvent ? 0 : mCurrentPointF.y - mPrePointF.y;
    }

    /** *   event        * * @param event * @return */
    private PointF caculateFocalPointer(MotionEvent event) {
        final int count = event.getPointerCount();
        float x = 0, y = 0;
        for (int i = 0; i < count; i++) {
            x += event.getX(i);
            y += event.getY(i);
        }
        //        
        x = x / count;
        y = y / count;
        return new PointF(x, y);
    }

    /** *          ,         * * @return */
    public float getMoveX() {
        return mExtenalPointer.x;
    }

    public float getMoveY() {
        return mExtenalPointer.y;
    }

    /** *      * * @Project App_View * @Package com.android.view.largeimageview * @author chenlin * @version 1.0 * @Note TODO */
    public interface OnMoveGestureListener {
        /** *        * * @param detector * @return */
        public boolean onMoveBegin(MoveGestureDetector detector);

        /** *       * * @param detector * @return */
        public boolean onMove(MoveGestureDetector detector);

        /** *      * * @param detector */
        public void onMoveEnd(MoveGestureDetector detector);
    }

    public static class SimpleMoveGestureDetector implements OnMoveGestureListener {
        @Override
        public boolean onMoveBegin(MoveGestureDetector detector) {
            return true;
        }

        @Override
        public boolean onMove(MoveGestureDetector detector) {
            return false;
        }

        @Override
        public void onMoveEnd(MoveGestureDetector detector) {
        }
    }

}

ステップ3:LargeImageViewのカスタマイズ
public class LargeImageView extends View {
    /**         */
    private BitmapRegionDecoder mDecoder;
    /**         */
    private int mImageWidth, mImageHeight;
    /**     */
    private volatile Rect mRect = new Rect();
    /**     */
    private MoveGestureDetector mDetector;
    /** * Creates Bitmap objects from various sources, including files, streams, * and byte-arrays. */
    private static final BitmapFactory.Options options = new BitmapFactory.Options();
    static{
        /*             */
        options.inPreferredConfig = Bitmap.Config.RGB_565;
    }

    public LargeImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public LargeImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LargeImageView(Context context) {
        super(context);
        init();
    }

    //     
    private void init() {
        mDetector = new MoveGestureDetector(getContext(), new SimpleMoveGestureDetector(){
            @Override
            public boolean onMove(MoveGestureDetector detector) {
                //    
                int moveX = (int) detector.getMoveX();
                int moveY = (int) detector.getMoveY();

                Logger.i("large", "moveX == " + moveX);
                Logger.i("large", "moveY == " + moveY);

                //       >view   
                if (mImageWidth > getWidth()) {
                    //    
                    mRect.offset(-moveX, 0);
                    checkWidth();
                    //  
                    invalidate();
                }
                if (mImageHeight > getHeight()) {
                    //    
                    mRect.offset(0, -moveY);
                    checkHeight();
                    //  
                    invalidate();
                }
                return true;
            }
        });
    }


// private OnMoveGestureListener listener = new OnMoveGestureListener(){
//
// @Override
// public boolean onMoveBegin(MoveGestureDetector detector) {
// //Toast.makeText(getContext(), "    ", 1).show();
// return false;
// }
//
// @Override
// public boolean onMove(MoveGestureDetector detector) {
// //    
// int moveX = (int) detector.getMoveX();
// int moveY = (int) detector.getMoveY();
// //       >view   
// if (mImageWidth > getWidth()) {
// //    
// mRect.offset(-moveX, 0);
// checkWidth();
// //  
// invalidate();
// }
// if (mImageHeight > getHeight()) {
// //    
// mRect.offset(0, -moveY);
// checkHeight();
// //  
// invalidate();
// }
// return true;
// }
//
// @Override
// public void onMoveEnd(MoveGestureDetector detector) {
// //Toast.makeText(getContext(), "    ", 1).show();
// }
// 
// };

    /** *          */
    protected void checkWidth() {
        Rect rect = mRect;
        int width = mImageWidth;

        //      > view
        if (rect.right > width) {
            rect.right = width;
            //   =       -     
            rect.left = width - getWidth();
        }

        //    
        if (rect.left < 0) {
            rect.left  = 0;
            //   =     
            rect.right = getWidth();
        }

    }

    /** *         */
    protected void checkHeight() {
        Rect rect = mRect;
        int height = mImageHeight;

        if (rect.bottom > height) {
            rect.bottom = height;
            rect.top = height - getHeight();
        }

        if (rect.top < 0) {
            rect.top = 0;
            rect.bottom = getHeight();
        }
    }

    /** *    :     ,          */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //          
        int widthMeasure = getMeasuredWidth();
        int heightMeasure = getMeasuredHeight();

        //       
        int imageWidth = mImageWidth;
        int imageHeight = mImageHeight;

        //             ,        
        mRect.left = (imageWidth - widthMeasure)/2;
        mRect.top = (imageHeight - heightMeasure)/2;

        mRect.right = mRect.left + widthMeasure;
        mRect.bottom = mRect.top + heightMeasure;

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /** *    :              */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //touchevent        
        mDetector.onTouchEvent(event);
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Bitmap bm = mDecoder.decodeRegion(mRect, options);
        //        
        canvas.drawBitmap(bm, 0, 0, null);
        super.onDraw(canvas);
    }

    /** *                 * @param is * @return */
    protected void setInputStream(InputStream is){
        try {
            //            
            mDecoder = BitmapRegionDecoder.newInstance(is, false);
            BitmapFactory.Options tmpOptions = new BitmapFactory.Options();
            tmpOptions.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(is, null, tmpOptions);

            //       
            mImageHeight = tmpOptions.outHeight;
            mImageWidth = tmpOptions.outWidth;

        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

ステップ4:実装
public class MainActivity extends Activity {
    private LargeImageView mImageview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_largeimage);

        mImageview = (LargeImageView) findViewById(R.id.largeImage);

        loadImage();
    }

    /** *  asset         */
    private void loadImage() {
        try {
            InputStream is = getAssets().open("22.jpg");
            mImageview.setInputStream(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

activity_largeimage.xmlファイル
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >

    <com.android.view.largeimageview.LargeImageView  android:id="@+id/largeImage" android:layout_width="match_parent" android:layout_height="match_parent" >
    </com.android.view.largeimageview.LargeImageView>

</RelativeLayout>