AndroidカスタムView横ページ


原理見:ページをめくる効果原理実現のページをめくる試み
package com.stone.turnpage.view;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * author : stone
 * email  : [email protected]
 * time   : 16/7/8 10 35
 */
public class HorizonTurnPageView extends View {

    private List mBitmaps;
    private float mClipX; //       
    private float mCurPointX;//         X    
    private float mAutoAreaLeft, mAutoAreaRight; //              
    private boolean mIsLastPage; //      
    private boolean mIsNextPage; //     
    private int mPageIndex;

    private Runnable mMsgCallback;
    private Handler mHandler = new Handler();

    public HorizonTurnPageView(Context context) {
        super(context);

//        ViewConfiguration.get(context).getScaledTouchSlop()
    }

    public void setBitmaps(List bitmaps) {
        if (null == bitmaps || bitmaps.size() == 0) return;
        this.mBitmaps = bitmaps;
        System.out.println("setBitmaps");
        invalidate();
    }

    /**
     *     :         (    )        
     */
    private void initBitmaps() {
        if (mBitmaps == null) {
            return;
        }
        List temp = new ArrayList();
        for (int i = mBitmaps.size() - 1; i >= 0; i--) {
            Bitmap bitmap = Bitmap.createScaledBitmap(mBitmaps.get(i), getWidth(), getHeight(), true);
            temp.add(bitmap);
        }
        mBitmaps = temp;
    }

    private void drawBitmaps(Canvas canvas) {
/*        for (int i = 0; i < mBitmaps.size(); i++) {
//            canvas.save();

            if (i == mBitmaps.size() - 1) {//         (        )      
                canvas.clipRect(0, 0, mClipX, getHeight());
            }
            canvas.drawBitmap(mBitmaps.get(i), 0, 0, null);

//            canvas.restore();
        }*/

        /*
              ,                ,           
              
         */
        mIsLastPage = false;
        mPageIndex = mPageIndex < 0 ? 0 : mPageIndex;
        mPageIndex = mPageIndex > mBitmaps.size() ? mBitmaps.size() : mPageIndex;
        //         
        int start = mBitmaps.size() - 2 - mPageIndex;//mBitmaps.size() - 2        
        int end = mBitmaps.size() - mPageIndex;   // end - start = 2
        /*
         *           0               
         */
        if (start < 0) {
            mIsLastPage = true; //   mPageIndex = size - 1

            showToast("       ");
            //         
            start = 0;
            end = 1;
        }
        for (int i = start; i < end; i++) {//end - start = 2           
            if (!mIsLastPage && i == end - 1) {
                canvas.clipRect(0, 0, mClipX, getHeight()); //             
            }
            canvas.drawBitmap(mBitmaps.get(i), 0, 0, null);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mIsNextPage = true;
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                mCurPointX = event.getX();
                if (mCurPointX < mAutoAreaLeft) {
                    mIsNextPage = false; //   
                    mPageIndex--;
                    mClipX = mCurPointX;
                    invalidate();
                }
                break;

            case MotionEvent.ACTION_MOVE:
                mClipX = event.getX();
                invalidate();
                break;

            case MotionEvent.ACTION_UP:
                judgeSlideAuto();
                /*
                 *            
                 *          
                 *        clip , judgeSlideAuto      
                 */
                if (!mIsLastPage && mIsNextPage && mClipX <= 0) {
                    mPageIndex++;
                    mClipX = getWidth();
                    invalidate();
                }
                break;
        }
        return true;
    }

    /**
     *          :           
     */
    private void judgeSlideAuto() {
        if (mClipX < mAutoAreaLeft) {//    1/5 w     
            while (mClipX > 0) {
                mClipX--;
                invalidate();
            }

        } else if (mClipX > mAutoAreaRight) {//    4/5 w    
            while (mClipX < getWidth()) {
                mClipX++;
                invalidate();
            }
        }
    }

    /*
    onMeasure    ,    ViewGroup ,  onLayout              
     */
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        System.out.println("onSizeChanged");
        initBitmaps();

        mClipX = getWidth();
        mAutoAreaLeft = getWidth() / 5f;
        mAutoAreaRight = getWidth() / 5f * 4;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (null == mBitmaps || mBitmaps.size() == 0) {
            return;
        }
        drawBitmaps(canvas);

    }

    private void showToast(final Object msg) {
        mHandler.removeCallbacksAndMessages(null);
        mMsgCallback = new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getContext(), msg.toString(), Toast.LENGTH_SHORT).show();
            }
        };
        mHandler.postDelayed(mMsgCallback, 200);

    }

}

自分のカスタムViewプロジェクトアドレス:https://github.com/aa86799/MyCustomView(start&forkへようこそ)
本住所:https://github.com/aa86799/MyCustomView/tree/master/turnpage