Search in sources :

Example 71 with LinearGradient

use of android.graphics.LinearGradient in project nmid-headline by miao1007.

the class GradientTransformation method transform.

@Override
public Bitmap transform(Bitmap source) {
    int x = source.getWidth();
    int y = source.getHeight();
    Bitmap grandientBitmap = source.copy(source.getConfig(), true);
    Canvas canvas = new Canvas(grandientBitmap);
    //left-top == (0,0) , right-bottom == (x,y);
    LinearGradient grad = new LinearGradient(x / 2, y, x / 2, y / 2, startColor, endColor, Shader.TileMode.CLAMP);
    Paint p = new Paint(Paint.DITHER_FLAG);
    p.setShader(null);
    p.setDither(true);
    p.setFilterBitmap(true);
    p.setShader(grad);
    canvas.drawPaint(p);
    source.recycle();
    return grandientBitmap;
}
Also used : LinearGradient(android.graphics.LinearGradient) Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) Paint(android.graphics.Paint)

Example 72 with LinearGradient

use of android.graphics.LinearGradient in project Lazy by l123456789jy.

the class BitmapUtil method createReflectionBitmap.

/**
     * 获得带倒影的图片方法
     *
     * @param bitmap 源Bitmap
     * @return 带倒影的Bitmap
     */
public static Bitmap createReflectionBitmap(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);
    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);
    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);
    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);
    return bitmapWithReflection;
}
Also used : LinearGradient(android.graphics.LinearGradient) Bitmap(android.graphics.Bitmap) ColorMatrix(android.graphics.ColorMatrix) Matrix(android.graphics.Matrix) PorterDuffXfermode(android.graphics.PorterDuffXfermode) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) SuppressLint(android.annotation.SuppressLint) Paint(android.graphics.Paint)

Example 73 with LinearGradient

use of android.graphics.LinearGradient in project UltimateRecyclerView by cymcsg.

the class FloatingActionButton method createStrokesDrawable.

protected Drawable createStrokesDrawable(RectF circleRect) {
    final Bitmap bitmap = Bitmap.createBitmap(mDrawableSize, mDrawableSize, Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);
    final float strokeWidth = getDimension(R.dimen.fab_stroke_width);
    final float halfStrokeWidth = strokeWidth / 2f;
    RectF outerStrokeRect = new RectF(circleRect.left - halfStrokeWidth, circleRect.top - halfStrokeWidth, circleRect.right + halfStrokeWidth, circleRect.bottom + halfStrokeWidth);
    RectF innerStrokeRect = new RectF(circleRect.left + halfStrokeWidth, circleRect.top + halfStrokeWidth, circleRect.right - halfStrokeWidth, circleRect.bottom - halfStrokeWidth);
    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStrokeWidth(strokeWidth);
    paint.setStyle(Style.STROKE);
    // outer
    paint.setColor(Color.BLACK);
    paint.setAlpha(opacityToAlpha(0.02f));
    canvas.drawOval(outerStrokeRect, paint);
    // inner bottom
    paint.setShader(new LinearGradient(innerStrokeRect.centerX(), innerStrokeRect.top, innerStrokeRect.centerX(), innerStrokeRect.bottom, new int[] { Color.TRANSPARENT, HALF_TRANSPARENT_BLACK, Color.BLACK }, new float[] { 0f, 0.8f, 1f }, TileMode.CLAMP));
    paint.setAlpha(opacityToAlpha(0.04f));
    canvas.drawOval(innerStrokeRect, paint);
    // inner top
    paint.setShader(new LinearGradient(innerStrokeRect.centerX(), innerStrokeRect.top, innerStrokeRect.centerX(), innerStrokeRect.bottom, new int[] { Color.WHITE, HALF_TRANSPARENT_WHITE, Color.TRANSPARENT }, new float[] { 0f, 0.2f, 1f }, TileMode.CLAMP));
    paint.setAlpha(opacityToAlpha(0.8f));
    canvas.drawOval(innerStrokeRect, paint);
    return new BitmapDrawable(getResources(), bitmap);
}
Also used : RectF(android.graphics.RectF) LinearGradient(android.graphics.LinearGradient) Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) BitmapDrawable(android.graphics.drawable.BitmapDrawable)

Example 74 with LinearGradient

use of android.graphics.LinearGradient in project UltimateAndroid by cymcsg.

the class ShimmerFrameLayout method getMaskBitmap.

// Return the mask bitmap, creating it if necessary.
private Bitmap getMaskBitmap() {
    if (mMaskBitmap != null) {
        return mMaskBitmap;
    }
    int width = mMask.maskWidth(getWidth());
    int height = mMask.maskHeight(getHeight());
    mMaskBitmap = createBitmapAndGcIfNecessary(width, height);
    Canvas canvas = new Canvas(mMaskBitmap);
    Shader gradient;
    switch(mMask.shape) {
        default:
        case LINEAR:
            {
                int x1, y1;
                int x2, y2;
                switch(mMask.angle) {
                    default:
                    case CW_0:
                        x1 = 0;
                        y1 = 0;
                        x2 = width;
                        y2 = 0;
                        break;
                    case CW_90:
                        x1 = 0;
                        y1 = 0;
                        x2 = 0;
                        y2 = height;
                        break;
                    case CW_180:
                        x1 = width;
                        y1 = 0;
                        x2 = 0;
                        y2 = 0;
                        break;
                    case CW_270:
                        x1 = 0;
                        y1 = height;
                        x2 = 0;
                        y2 = 0;
                        break;
                }
                gradient = new LinearGradient(x1, y1, x2, y2, mMask.getGradientColors(), mMask.getGradientPositions(), Shader.TileMode.REPEAT);
                break;
            }
        case RADIAL:
            {
                int x = width / 2;
                int y = height / 2;
                gradient = new RadialGradient(x, y, (float) (Math.max(width, height) / Math.sqrt(2)), mMask.getGradientColors(), mMask.getGradientPositions(), Shader.TileMode.REPEAT);
                break;
            }
    }
    canvas.rotate(mMask.tilt, width / 2, height / 2);
    Paint paint = new Paint();
    paint.setShader(gradient);
    // We need to increase the rect size to account for the tilt
    int padding = (int) (Math.sqrt(2) * Math.max(width, height)) / 2;
    canvas.drawRect(-padding, -padding, width + padding, height + padding, paint);
    return mMaskBitmap;
}
Also used : LinearGradient(android.graphics.LinearGradient) Canvas(android.graphics.Canvas) RadialGradient(android.graphics.RadialGradient) Paint(android.graphics.Paint) Shader(android.graphics.Shader) Paint(android.graphics.Paint)

Example 75 with LinearGradient

use of android.graphics.LinearGradient in project book-animation by briangriffey.

the class PageTurnLayout method draw.

@Override
public void draw(Canvas canvas) {
    if (mLastTouchPoint != null && mIsTurning && mDirection != null) {
        View topView;
        View bottomView;
        if (mDirection == PageTurnDirection.LEFT) {
            topView = getChildAt(mCurrentPage);
            bottomView = getChildAt(mCurrentPage + 1);
        } else {
            topView = getChildAt(mCurrentPage - 1);
            bottomView = getChildAt(mCurrentPage);
        }
        int height = getMeasuredHeight();
        int width = getMeasuredWidth();
        int halfWidth = (int) (width * .5);
        int distanceToEnd = width - mLastTouchPoint.x;
        int backOfPageWidth = Math.min(halfWidth, distanceToEnd / 2);
        int shadowLength = Math.max(5, backOfPageWidth / 20);
        // The rect that represents the backofthepage
        Rect backOfPageRect = new Rect(mLastTouchPoint.x, 0, mLastTouchPoint.x + backOfPageWidth, height);
        // The along the crease of the turning page
        Rect shadowRect = new Rect(mLastTouchPoint.x - shadowLength, 0, mLastTouchPoint.x, height);
        // The shadow cast onto the next page by teh turning page
        Rect backShadowRect = new Rect(backOfPageRect.right, 0, backOfPageRect.right + (backOfPageWidth / 2), height);
        // set the top view to be the current page to the crease of the
        // turning page
        mTopViewRect.set(0, 0, mLastTouchPoint.x, getMeasuredHeight());
        mBottomViewRect.set(backOfPageRect.right, 0, width, height);
        canvas.save();
        // clip and draw the top page to your touch
        canvas.clipRect(mTopViewRect);
        topView.draw(canvas);
        canvas.restore();
        // clip and draw the first shadow
        canvas.save();
        canvas.clipRect(shadowRect);
        mPaint.setShader(new LinearGradient(shadowRect.left, shadowRect.top, shadowRect.right, shadowRect.top, 0x00000000, 0x44000000, Shader.TileMode.REPEAT));
        canvas.drawPaint(mPaint);
        canvas.restore();
        mPaint.setShader(null);
        // clip and draw the gradient that makes the page look bent
        canvas.save();
        canvas.clipRect(backOfPageRect);
        mPaint.setShadowLayer(0, 0, 0, 0x00000000);
        mPaint.setShader(new LinearGradient(backOfPageRect.left, backOfPageRect.top, backOfPageRect.right, backOfPageRect.top, new int[] { 0xFFEEEEEE, 0xFFDDDDDD, 0xFFEEEEEE, 0xFFD6D6D6 }, new float[] { .35f, .73f, 9f, 1.0f }, Shader.TileMode.REPEAT));
        canvas.drawPaint(mPaint);
        canvas.restore();
        // draw the second page in the remaining space
        canvas.save();
        canvas.clipRect(mBottomViewRect);
        bottomView.draw(canvas);
        canvas.restore();
        // now draw a shadow
        if (backShadowRect.left > 0) {
            canvas.save();
            canvas.clipRect(backShadowRect);
            mPaint.setShader(new LinearGradient(backShadowRect.left, backShadowRect.top, backShadowRect.right, backShadowRect.top, 0x44000000, 0x00000000, Shader.TileMode.REPEAT));
            canvas.drawPaint(mPaint);
            // canvas.drawColor(0xFF000000);
            canvas.restore();
        }
    } else {
        getChildAt(mCurrentPage).draw(canvas);
    }
}
Also used : LinearGradient(android.graphics.LinearGradient) Rect(android.graphics.Rect) View(android.view.View) Paint(android.graphics.Paint) Point(android.graphics.Point)

Aggregations

LinearGradient (android.graphics.LinearGradient)128 Paint (android.graphics.Paint)74 RectF (android.graphics.RectF)41 RadialGradient (android.graphics.RadialGradient)27 Canvas (android.graphics.Canvas)23 Rect (android.graphics.Rect)21 Shader (android.graphics.Shader)19 Path (android.graphics.Path)18 Bitmap (android.graphics.Bitmap)16 Matrix (android.graphics.Matrix)16 Point (android.graphics.Point)16 SweepGradient (android.graphics.SweepGradient)12 PorterDuffXfermode (android.graphics.PorterDuffXfermode)10 ShapeDrawable (android.graphics.drawable.ShapeDrawable)10 SuppressLint (android.annotation.SuppressLint)7 ComposeShader (android.graphics.ComposeShader)7 PaintDrawable (android.graphics.drawable.PaintDrawable)5 RectShape (android.graphics.drawable.shapes.RectShape)5 ColorDrawable (android.graphics.drawable.ColorDrawable)4 ShaderFactory (android.graphics.drawable.ShapeDrawable.ShaderFactory)4