Example usage for android.graphics Canvas drawBitmapMesh

List of usage examples for android.graphics Canvas drawBitmapMesh

Introduction

In this page you can find the example usage for android.graphics Canvas drawBitmapMesh.

Prototype

public void drawBitmapMesh(@NonNull Bitmap bitmap, int meshWidth, int meshHeight, @NonNull float[] verts,
        int vertOffset, @Nullable int[] colors, int colorOffset, @Nullable Paint paint) 

Source Link

Document

Draw the bitmap through the mesh, where mesh vertices are evenly distributed across the bitmap.

Usage

From source file:t3.giftbook.util.FlipViewPager.java

/**
 * ??/*from w  w w.ja  va 2  s.  c  o  m*/
 */
private void drawFlippingPage(Canvas canvas) {
    if (mOffset == 0) {
        //?????????????Flip?????(draw??)
        return;
    }

    int scrollX = this.getScrollX();

    if (currentPageBmp == null || currentPageBmp.isRecycled() || nextPageBmp.isRecycled()) {
        currentPageBmp = prepareBmp(canvas);
        currentPageCnv = new Canvas(currentPageBmp);
        nextPageBmp = prepareBmp(canvas);
        nextPageCnv = new Canvas(nextPageBmp);
    }

    if (currentPageBmp != null) {
        final int currentPage = mPosition;
        final int nextPage = currentPage + 1;

        if (currentPageCnv != null) {
            if (lastDrawedPage != currentPage) {
                try {
                    //mPosition????????bounds????
                    //1???mOffset??????return.
                    //???????????mPosition???????

                    final long time = System.currentTimeMillis();
                    drawChildTo(currentPageCnv, currentPage, time);
                    drawChildTo(nextPageCnv, nextPage, time);

                    //??currentPageBmp, nextPageBmp????????????
                    lastDrawedPage = currentPage;

                } catch (IllegalArgumentException e) {
                    Log.w(TAG, e);
                }
            }
        }

        /*
         * canvas?????????????
         * ???????????
         * ?????????????
         */

        //?
        final Rect screenLeftSide = new Rect(scrollX, 0, scrollX + this.getWidth() / 2, this.getHeight());
        //??
        final Rect screenRightSide = new Rect(scrollX + this.getWidth() / 2, 0, scrollX + this.getWidth() / 1,
                this.getHeight());

        //bmp?
        final Rect pageLeftSide = new Rect((int) (this.getWidth() * 0.0f), 0, (int) (this.getWidth() * 0.5f),
                this.getHeight());

        //bmp??
        final Rect pageRightSide = new Rect((int) (this.getWidth() * 0.5f), 0, (int) (this.getWidth() * 1.0f),
                this.getHeight());

        /*
         * ???Flip??
         */

        //?
        Paint white = new Paint() {
            {
                this.setColor(Color.WHITE);
            }
        };
        canvas.drawRect(screenLeftSide, white);
        canvas.drawRect(screenRightSide, white);

        //???????
        canvas.drawBitmap(currentPageBmp, pageLeftSide, screenLeftSide, new Paint());

        //????????
        canvas.drawBitmap(nextPageBmp, pageRightSide, screenRightSide, new Paint());

        //
        Paint shadow = new Paint() {
            {
                this.setColor(Color.BLACK);
            }
        };

        final int meshCols = 30;
        final float meshColsf = (float) meshCols;
        final int meshRows = 30;
        final float meshRowsf = (float) meshRows;

        //??????????
        if (mOffset <= 0.5) {
            final float offsetRate = (0.5f - mOffset) * 2;
            final float invOffsetRate = 1f - offsetRate;

            //???
            shadow.setAlpha((int) (90 * offsetRate));
            canvas.drawRect(screenRightSide, shadow);

            //???????????
            final float[] rVerts = new float[(meshCols + 1) * (meshRows + 1) * 2];
            for (int meshY = 0; meshY <= meshRows; meshY++) {
                final float meshYf = (float) meshY;
                for (int meshX = 0; meshX <= meshCols; meshX++) {
                    final float meshXf = (float) meshX;

                    final int meshPosition = (meshX + meshY * (meshCols + 1)) * 2;
                    final int xPosition = meshPosition;
                    final int yPosition = xPosition + 1;

                    if (meshX <= meshCols / 2) {
                        rVerts[xPosition] = screenRightSide.left; //??????????
                        rVerts[yPosition] = screenRightSide.top + screenRightSide.height() / meshRowsf * meshYf;
                    } else {
                        rVerts[xPosition] = screenRightSide.left + offsetRate * screenRightSide.width()
                                / meshColsf * (meshXf - meshColsf / 2f) * 2f;
                        rVerts[yPosition] = screenRightSide.top + screenRightSide.height() / 2f
                                - screenRightSide.height() / meshRowsf * (meshRowsf / 2f - meshYf)
                                        * (1f + invOffsetRate / meshColsf * (meshXf - meshColsf / 2f));
                    }
                }
            }
            canvas.drawBitmapMesh(currentPageBmp, meshCols, meshRows, rVerts, 0, null, 0, null);

            //????
            final Paint pageRootPaint = new Paint() {
                {
                    int shadow1 = Color.argb((int) (30 * invOffsetRate), 0, 0, 0);
                    int shadow2 = Color.argb(0, 0, 0, 0);
                    this.setShader(new LinearGradient((float) screenRightSide.left,
                            (float) screenRightSide.top + screenRightSide.height() / 2,
                            (float) screenRightSide.left + screenRightSide.width() * offsetRate,
                            (float) screenRightSide.top + screenRightSide.height() / 2, shadow1, shadow2,
                            Shader.TileMode.CLAMP));
                }
            };
            canvas.drawRect(screenRightSide.left, screenRightSide.top, screenRightSide.right,
                    screenRightSide.bottom, pageRootPaint);

        } else {
            final float offsetRate = (mOffset - 0.5f) * 2;
            final float invOffsetRate = 1f - offsetRate;

            //??
            shadow.setAlpha((int) (90 * offsetRate));
            canvas.drawRect(screenLeftSide, shadow);

            //????????
            final float[] rVerts = new float[(meshCols + 1) * (meshRows + 1) * 2];
            for (int meshY = 0; meshY <= meshRows; meshY++) {
                final float meshYf = (float) meshY;
                for (int meshX = 0; meshX <= meshCols; meshX++) {
                    final float meshXf = (float) meshX;
                    final float meshXfInv = meshColsf / 2f - meshXf;

                    final int meshPosition = (meshX + meshY * (meshCols + 1)) * 2;
                    final int xPosition = meshPosition;
                    final int yPosition = xPosition + 1;

                    if (meshX >= meshCols / 2) {
                        rVerts[xPosition] = screenLeftSide.right; //???????????
                        rVerts[yPosition] = screenLeftSide.top + screenLeftSide.height() / meshRowsf * meshYf;
                    } else {
                        rVerts[xPosition] = screenLeftSide.right - offsetRate * screenLeftSide.width()
                                + offsetRate * screenLeftSide.width() / meshColsf * (meshXf) * 2f;
                        rVerts[yPosition] = screenLeftSide.top + screenLeftSide.height() / 2f
                                - screenLeftSide.height() / meshRowsf * (meshRowsf / 2f - meshYf)
                                        * (1f + invOffsetRate / meshColsf * (meshColsf / 2f - meshXf));
                    }
                }
            }
            canvas.drawBitmapMesh(nextPageBmp, meshCols, meshRows, rVerts, 0, null, 0, null);

            //???
            final Paint pageRootPaint = new Paint() {
                {
                    int shadow2 = Color.argb((int) (30 * invOffsetRate), 0, 0, 0);
                    int shadow1 = Color.argb(0, 0, 0, 0);
                    this.setShader(new LinearGradient(
                            (float) screenLeftSide.right - screenRightSide.width() * offsetRate,
                            (float) screenLeftSide.top + screenRightSide.height() / 2,
                            (float) screenLeftSide.right,
                            (float) screenLeftSide.top + screenRightSide.height() / 2, shadow1, shadow2,
                            Shader.TileMode.CLAMP));
                }
            };
            canvas.drawRect(screenLeftSide.left, screenLeftSide.top, screenLeftSide.right,
                    screenLeftSide.bottom, pageRootPaint);
        }

        //           int y = 0;
        //           canvas.drawRect(0,0,canvas.getWidth(), 300, new Paint() {{this.setColor(Color.WHITE); this.setAlpha(100);}});
        //           Paint debugPrint = new Paint() {{this.setColor(Color.RED); this.setTextSize(30);}};
        //           canvas.drawText("b.width     = " + bkbmp.getWidth()   , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //           canvas.drawText("b.height    = " + bkbmp.getHeight()  , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //           canvas.drawText("c.width     = " + canvas.getWidth()  , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //           canvas.drawText("c.height    = " + canvas.getHeight() , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //         canvas.drawText("scrollX       = " + getScrollX()     , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //           canvas.drawText("mPosition     = " + mPosition        , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //           canvas.drawText("lastPosition  = " + lastDrawedPage   , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //           canvas.drawText("mOffset       = " + mOffset          , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //           canvas.drawText("offsetRate    = " + offsetRate       , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //           canvas.drawText("invOffsetRate = " + invOffsetRate    , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //           canvas.drawText("mOffsetPixels = " + mOffsetPixels    , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //           canvas.drawText("index         = " + getCurrentItem() , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);

    }
}