Search in sources :

Example 56 with Path

use of android.graphics.Path in project ZI by yixia.

the class CropImage method onSaveClicked.

private void onSaveClicked() {
    // bitmap doesn't have to be read into memory
    if (mSaving)
        return;
    if (mCrop == null) {
        return;
    }
    mSaving = true;
    Rect r = mCrop.getCropRect();
    int width = r.width();
    int height = r.height();
    // If we are circle cropping, we want alpha channel, which is the
    // third param here.
    Bitmap croppedImage = Bitmap.createBitmap(width, height, mCircleCrop ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
    {
        Canvas canvas = new Canvas(croppedImage);
        Rect dstRect = new Rect(0, 0, width, height);
        canvas.drawBitmap(mBitmap, r, dstRect, null);
    }
    if (mCircleCrop) {
        // OK, so what's all this about?
        // Bitmaps are inherently rectangular but we want to return
        // something that's basically a circle.  So we fill in the
        // area around the circle with alpha.  Note the all important
        // PortDuff.Mode.CLEAR.
        Canvas c = new Canvas(croppedImage);
        Path p = new Path();
        p.addCircle(width / 2F, height / 2F, width / 2F, Path.Direction.CW);
        c.clipPath(p, Region.Op.DIFFERENCE);
        c.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
    }
    /* If the output is required to a specific size then scale or fill */
    if (mOutputX != 0 && mOutputY != 0) {
        if (mScale) {
            /* Scale the image to the required dimensions */
            Bitmap old = croppedImage;
            croppedImage = BitmapUtils.transform(new Matrix(), croppedImage, mOutputX, mOutputY, mScaleUp);
            if (old != croppedImage) {
                old.recycle();
            }
        } else {
            /* Don't scale the image crop it to the size requested.
				 * Create an new image with the cropped image in the center and
				 * the extra space filled.
				 */
            // Don't scale the image but instead fill it so it's the
            // required dimension
            Bitmap b = Bitmap.createBitmap(mOutputX, mOutputY, Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(b);
            Rect srcRect = mCrop.getCropRect();
            Rect dstRect = new Rect(0, 0, mOutputX, mOutputY);
            int dx = (srcRect.width() - dstRect.width()) / 2;
            int dy = (srcRect.height() - dstRect.height()) / 2;
            /* If the srcRect is too big, use the center part of it. */
            srcRect.inset(Math.max(0, dx), Math.max(0, dy));
            /* If the dstRect is too big, use the center part of it. */
            dstRect.inset(Math.max(0, -dx), Math.max(0, -dy));
            /* Draw the cropped bitmap in the center */
            canvas.drawBitmap(mBitmap, srcRect, dstRect, null);
            /* Set the cropped bitmap as the new bitmap */
            croppedImage.recycle();
            croppedImage = b;
        }
    }
    // Return the cropped image directly or save it to the specified URI.
    Bundle myExtras = getIntent().getExtras();
    if (myExtras != null && (myExtras.getParcelable("data") != null || myExtras.getBoolean("return-data"))) {
        Bundle extras = new Bundle();
        extras.putParcelable("data", croppedImage);
        setResult(RESULT_OK, (new Intent()).setAction("inline-data").putExtras(extras));
        finish();
    } else if (myExtras != null && myExtras.getBoolean("set-wallpaper")) {
        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setMessage(getString(R.string.cropimage_wallpaper_is_setting));
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        new AsyncTask<Bitmap, Void, Boolean>() {

            protected void onPreExecute() {
                dialog.show();
            }

            protected void onPostExecute(Boolean result) {
                reset();
                if (result)
                    showToast(R.string.cropimage_wallpaper_success);
                else
                    showToast(R.string.cropimage_wallpaper_failed);
                CropImage.this.finish();
            }

            protected void onCancelled() {
                reset();
            }

            @Override
            protected Boolean doInBackground(Bitmap... image) {
                WallpaperManager myWallpaperManager = WallpaperManager.getInstance(CropImage.this);
                if (image != null && image[0] != null) {
                    try {
                        myWallpaperManager.setBitmap(image[0]);
                    } catch (IOException e) {
                        return false;
                    }
                } else {
                    return false;
                }
                return true;
            }

            private void reset() {
                dialog.dismiss();
            }

            private void showToast(int resID) {
                Toast.makeText(CropImage.this, getString(resID), Toast.LENGTH_SHORT).show();
            }
        }.execute(croppedImage);
    } else {
        final Bitmap b = croppedImage;
        BitmapUtils.startBackgroundJob(this, null, getString(R.string.cropimage_image_saving), new Runnable() {

            public void run() {
                saveOutput(b);
            }
        }, mHandler);
    }
}
Also used : Path(android.graphics.Path) Rect(android.graphics.Rect) Bundle(android.os.Bundle) Canvas(android.graphics.Canvas) AsyncTask(android.os.AsyncTask) Intent(android.content.Intent) IOException(java.io.IOException) ProgressDialog(android.app.ProgressDialog) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) WallpaperManager(android.app.WallpaperManager)

Example 57 with Path

use of android.graphics.Path in project PhotoNoter by yydcdut.

the class MaterialRippleLayout method draw.

/*
     * Drawing
     */
@Override
public void draw(Canvas canvas) {
    final boolean positionChanged = adapterPositionChanged();
    if (rippleOverlay) {
        if (!positionChanged) {
            rippleBackground.draw(canvas);
        }
        super.draw(canvas);
        if (!positionChanged) {
            if (rippleRoundedCorners != 0) {
                Path clipPath = new Path();
                RectF rect = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
                clipPath.addRoundRect(rect, rippleRoundedCorners, rippleRoundedCorners, Path.Direction.CW);
                canvas.clipPath(clipPath);
            }
            canvas.drawCircle(currentCoords.x, currentCoords.y, radius, paint);
        }
    } else {
        if (!positionChanged) {
            rippleBackground.draw(canvas);
            canvas.drawCircle(currentCoords.x, currentCoords.y, radius, paint);
        }
        super.draw(canvas);
    }
}
Also used : Path(android.graphics.Path) RectF(android.graphics.RectF)

Example 58 with Path

use of android.graphics.Path in project XobotOS by xamarin.

the class LockPatternView method onDraw.

@Override
protected void onDraw(Canvas canvas) {
    final ArrayList<Cell> pattern = mPattern;
    final int count = pattern.size();
    final boolean[][] drawLookup = mPatternDrawLookup;
    if (mPatternDisplayMode == DisplayMode.Animate) {
        // figure out which circles to draw
        // + 1 so we pause on complete pattern
        final int oneCycle = (count + 1) * MILLIS_PER_CIRCLE_ANIMATING;
        final int spotInCycle = (int) (SystemClock.elapsedRealtime() - mAnimatingPeriodStart) % oneCycle;
        final int numCircles = spotInCycle / MILLIS_PER_CIRCLE_ANIMATING;
        clearPatternDrawLookup();
        for (int i = 0; i < numCircles; i++) {
            final Cell cell = pattern.get(i);
            drawLookup[cell.getRow()][cell.getColumn()] = true;
        }
        // figure out in progress portion of ghosting line
        final boolean needToUpdateInProgressPoint = numCircles > 0 && numCircles < count;
        if (needToUpdateInProgressPoint) {
            final float percentageOfNextCircle = ((float) (spotInCycle % MILLIS_PER_CIRCLE_ANIMATING)) / MILLIS_PER_CIRCLE_ANIMATING;
            final Cell currentCell = pattern.get(numCircles - 1);
            final float centerX = getCenterXForColumn(currentCell.column);
            final float centerY = getCenterYForRow(currentCell.row);
            final Cell nextCell = pattern.get(numCircles);
            final float dx = percentageOfNextCircle * (getCenterXForColumn(nextCell.column) - centerX);
            final float dy = percentageOfNextCircle * (getCenterYForRow(nextCell.row) - centerY);
            mInProgressX = centerX + dx;
            mInProgressY = centerY + dy;
        }
        // TODO: Infinite loop here...
        invalidate();
    }
    final float squareWidth = mSquareWidth;
    final float squareHeight = mSquareHeight;
    float radius = (squareWidth * mDiameterFactor * 0.5f);
    mPathPaint.setStrokeWidth(radius);
    final Path currentPath = mCurrentPath;
    currentPath.rewind();
    // draw the circles
    final int paddingTop = mPaddingTop;
    final int paddingLeft = mPaddingLeft;
    for (int i = 0; i < 3; i++) {
        float topY = paddingTop + i * squareHeight;
        //float centerY = mPaddingTop + i * mSquareHeight + (mSquareHeight / 2);
        for (int j = 0; j < 3; j++) {
            float leftX = paddingLeft + j * squareWidth;
            drawCircle(canvas, (int) leftX, (int) topY, drawLookup[i][j]);
        }
    }
    // TODO: the path should be created and cached every time we hit-detect a cell
    // only the last segment of the path should be computed here
    // draw the path of the pattern (unless the user is in progress, and
    // we are in stealth mode)
    final boolean drawPath = (!mInStealthMode || mPatternDisplayMode == DisplayMode.Wrong);
    // draw the arrows associated with the path (unless the user is in progress, and
    // we are in stealth mode)
    boolean oldFlag = (mPaint.getFlags() & Paint.FILTER_BITMAP_FLAG) != 0;
    // draw with higher quality since we render with transforms
    mPaint.setFilterBitmap(true);
    if (drawPath) {
        for (int i = 0; i < count - 1; i++) {
            Cell cell = pattern.get(i);
            Cell next = pattern.get(i + 1);
            // of animation).
            if (!drawLookup[next.row][next.column]) {
                break;
            }
            float leftX = paddingLeft + cell.column * squareWidth;
            float topY = paddingTop + cell.row * squareHeight;
            drawArrow(canvas, leftX, topY, cell, next);
        }
    }
    if (drawPath) {
        boolean anyCircles = false;
        for (int i = 0; i < count; i++) {
            Cell cell = pattern.get(i);
            // of animation).
            if (!drawLookup[cell.row][cell.column]) {
                break;
            }
            anyCircles = true;
            float centerX = getCenterXForColumn(cell.column);
            float centerY = getCenterYForRow(cell.row);
            if (i == 0) {
                currentPath.moveTo(centerX, centerY);
            } else {
                currentPath.lineTo(centerX, centerY);
            }
        }
        // add last in progress section
        if ((mPatternInProgress || mPatternDisplayMode == DisplayMode.Animate) && anyCircles) {
            currentPath.lineTo(mInProgressX, mInProgressY);
        }
        canvas.drawPath(currentPath, mPathPaint);
    }
    // restore default flag
    mPaint.setFilterBitmap(oldFlag);
}
Also used : Path(android.graphics.Path) Paint(android.graphics.Paint)

Example 59 with Path

use of android.graphics.Path in project Transitions-Everywhere by andkulikov.

the class PathParser method createPathFromPathData.

/**
     * @param pathData The string representing a path, the same as "d" string in svg file.
     * @return the generated Path object.
     */
public static Path createPathFromPathData(String pathData) {
    Path path = new Path();
    PathDataNode[] nodes = createNodesFromPathData(pathData);
    if (nodes != null) {
        try {
            PathDataNode.nodesToPath(nodes, path);
        } catch (RuntimeException e) {
            throw new RuntimeException("Error in parsing " + pathData, e);
        }
        return path;
    }
    return null;
}
Also used : Path(android.graphics.Path)

Example 60 with Path

use of android.graphics.Path in project Transitions-Everywhere by andkulikov.

the class PatternPathMotion method getPath.

@Override
public Path getPath(float startX, float startY, float endX, float endY) {
    double dx = endX - startX;
    double dy = endY - startY;
    float length = (float) Math.hypot(dx, dy);
    double angle = Math.atan2(dy, dx);
    mTempMatrix.setScale(length, length);
    mTempMatrix.postRotate((float) Math.toDegrees(angle));
    mTempMatrix.postTranslate(startX, startY);
    Path path = new Path();
    mPatternPath.transform(mTempMatrix, path);
    return path;
}
Also used : Path(android.graphics.Path)

Aggregations

Path (android.graphics.Path)631 Paint (android.graphics.Paint)237 RectF (android.graphics.RectF)142 Rect (android.graphics.Rect)40 Canvas (android.graphics.Canvas)39 Matrix (android.graphics.Matrix)36 TextPaint (android.text.TextPaint)27 Bitmap (android.graphics.Bitmap)26 ObjectAnimator (android.animation.ObjectAnimator)18 LinearGradient (android.graphics.LinearGradient)18 PointF (android.graphics.PointF)18 Point (android.graphics.Point)16 View (android.view.View)16 RadialGradient (android.graphics.RadialGradient)15 Animator (android.animation.Animator)13 AnimatorListenerAdapter (android.animation.AnimatorListenerAdapter)12 PathMeasure (android.graphics.PathMeasure)11 PropertyValuesHolder (android.animation.PropertyValuesHolder)10 InflateException (android.view.InflateException)10 ViewGroup (android.view.ViewGroup)10