Search in sources :

Example 96 with LinearGradient

use of android.graphics.LinearGradient in project ThinkAndroid by white-cat.

the class ImageUtils method createReflectedImage.

/**
	 * 生成图片倒影
	 * 
	 * @param originalImage
	 * @return
	 */
public static Bitmap createReflectedImage(Bitmap originalImage) {
    final int reflectionGap = 4;
    int width = originalImage.getWidth();
    int height = originalImage.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);
    Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 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(originalImage, 0, 0, null);
    Paint defaultPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.MIRROR);
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    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) Paint(android.graphics.Paint)

Example 97 with LinearGradient

use of android.graphics.LinearGradient in project ABPlayer by winkstu.

the class ColorPickerView method drawSatValPanel.

private void drawSatValPanel(Canvas canvas) {
    final RectF rect = mSatValRect;
    if (BORDER_WIDTH_PX > 0) {
        mBorderPaint.setColor(mBorderColor);
        canvas.drawRect(mDrawingRect.left, mDrawingRect.top, rect.right + BORDER_WIDTH_PX, rect.bottom + BORDER_WIDTH_PX, mBorderPaint);
    }
    if (mValShader == null) {
        mValShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom, 0xffffffff, 0xff000000, TileMode.CLAMP);
    }
    int rgb = Color.HSVToColor(new float[] { mHue, 1f, 1f });
    mSatShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top, 0xffffffff, rgb, TileMode.CLAMP);
    ComposeShader mShader = new ComposeShader(mValShader, mSatShader, PorterDuff.Mode.MULTIPLY);
    mSatValPaint.setShader(mShader);
    canvas.drawRect(rect, mSatValPaint);
    Point p = satValToPoint(mSat, mVal);
    mSatValTrackerPaint.setColor(0xff000000);
    canvas.drawCircle(p.x, p.y, PALETTE_CIRCLE_TRACKER_RADIUS - 1f * mDensity, mSatValTrackerPaint);
    mSatValTrackerPaint.setColor(0xffdddddd);
    canvas.drawCircle(p.x, p.y, PALETTE_CIRCLE_TRACKER_RADIUS, mSatValTrackerPaint);
}
Also used : RectF(android.graphics.RectF) LinearGradient(android.graphics.LinearGradient) ComposeShader(android.graphics.ComposeShader) Point(android.graphics.Point) Point(android.graphics.Point) Paint(android.graphics.Paint)

Example 98 with LinearGradient

use of android.graphics.LinearGradient in project ABPlayer by winkstu.

the class ColorPickerView method drawAlphaPanel.

private void drawAlphaPanel(Canvas canvas) {
    if (!mShowAlphaPanel || mAlphaRect == null || mAlphaPattern == null)
        return;
    final RectF rect = mAlphaRect;
    if (BORDER_WIDTH_PX > 0) {
        mBorderPaint.setColor(mBorderColor);
        canvas.drawRect(rect.left - BORDER_WIDTH_PX, rect.top - BORDER_WIDTH_PX, rect.right + BORDER_WIDTH_PX, rect.bottom + BORDER_WIDTH_PX, mBorderPaint);
    }
    mAlphaPattern.draw(canvas);
    float[] hsv = new float[] { mHue, mSat, mVal };
    int color = Color.HSVToColor(hsv);
    int acolor = Color.HSVToColor(0, hsv);
    mAlphaShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top, color, acolor, TileMode.CLAMP);
    mAlphaPaint.setShader(mAlphaShader);
    canvas.drawRect(rect, mAlphaPaint);
    if (mAlphaSliderText != null && mAlphaSliderText != "") {
        canvas.drawText(mAlphaSliderText, rect.centerX(), rect.centerY() + 4 * mDensity, mAlphaTextPaint);
    }
    float rectWidth = 4 * mDensity / 2;
    Point p = alphaToPoint(mAlpha);
    RectF r = new RectF();
    r.left = p.x - rectWidth;
    r.right = p.x + rectWidth;
    r.top = rect.top - RECTANGLE_TRACKER_OFFSET;
    r.bottom = rect.bottom + RECTANGLE_TRACKER_OFFSET;
    canvas.drawRoundRect(r, 2, 2, mHueTrackerPaint);
}
Also used : RectF(android.graphics.RectF) LinearGradient(android.graphics.LinearGradient) Point(android.graphics.Point) Point(android.graphics.Point) Paint(android.graphics.Paint)

Example 99 with LinearGradient

use of android.graphics.LinearGradient in project XCL-Charts by xcltapestry.

the class AreaChart method renderArea.

private boolean renderArea(Canvas canvas, Paint paintAreaFill, Path pathArea, AreaData areaData, List<PointF> lstPathPoints) {
    int count = lstPathPoints.size();
    if (count < 3)
        // 没有或仅一个点就不需要了
        return false;
    // 设置当前填充色
    paintAreaFill.setColor(areaData.getAreaFillColor());
    if (areaData.getApplayGradient()) {
        LinearGradient linearGradient;
        if (areaData.getGradientDirection() == XEnum.Direction.VERTICAL) {
            float lineMaxY = getLineMaxMinY(Y_MAX, lstPathPoints);
            linearGradient = new LinearGradient(0, 0, 0, lineMaxY, areaData.getAreaBeginColor(), areaData.getAreaEndColor(), areaData.getGradientMode());
        } else {
            float lineMinY = getLineMaxMinY(Y_MIN, lstPathPoints);
            linearGradient = new LinearGradient(plotArea.getLeft(), plotArea.getBottom(), lstPathPoints.get(count - 1).x, lineMinY, areaData.getAreaBeginColor(), areaData.getAreaEndColor(), areaData.getGradientMode());
        }
        paintAreaFill.setShader(linearGradient);
    } else {
        paintAreaFill.setShader(null);
    }
    // 透明度
    paintAreaFill.setAlpha(this.mAreaAlpha);
    // 仅两点
    if (count == 3) {
        if (null == pathArea)
            pathArea = new Path();
        pathArea.moveTo(lstPathPoints.get(0).x, plotArea.getBottom());
        pathArea.lineTo(lstPathPoints.get(0).x, lstPathPoints.get(0).y);
        pathArea.lineTo(lstPathPoints.get(1).x, lstPathPoints.get(1).y);
        pathArea.lineTo(lstPathPoints.get(2).x, lstPathPoints.get(2).y);
        pathArea.lineTo(lstPathPoints.get(2).x, plotArea.getBottom());
        pathArea.close();
        // 绘制area
        canvas.drawPath(pathArea, paintAreaFill);
        pathArea.reset();
        return true;
    }
    for (int i = 0; i < count; i++) {
        PointF point = lstPathPoints.get(i);
        if (0 == i) {
            pathArea.moveTo(point.x, point.y);
        } else {
            pathArea.lineTo(point.x, point.y);
        }
    }
    pathArea.close();
    // 绘制area
    canvas.drawPath(pathArea, paintAreaFill);
    pathArea.reset();
    return true;
}
Also used : Path(android.graphics.Path) LinearGradient(android.graphics.LinearGradient) PointF(android.graphics.PointF) Paint(android.graphics.Paint)

Example 100 with LinearGradient

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

the class GradientDrawable method ensureValidRect.

/**
     * This checks mRectIsDirty, and if it is true, recomputes both our drawing
     * rectangle (mRect) and the gradient itself, since it depends on our
     * rectangle too.
     * @return true if the resulting rectangle is not empty, false otherwise
     */
private boolean ensureValidRect() {
    if (mRectIsDirty) {
        mRectIsDirty = false;
        Rect bounds = getBounds();
        float inset = 0;
        if (mStrokePaint != null) {
            inset = mStrokePaint.getStrokeWidth() * 0.5f;
        }
        final GradientState st = mGradientState;
        mRect.set(bounds.left + inset, bounds.top + inset, bounds.right - inset, bounds.bottom - inset);
        final int[] colors = st.mColors;
        if (colors != null) {
            RectF r = mRect;
            float x0, x1, y0, y1;
            if (st.mGradient == LINEAR_GRADIENT) {
                final float level = st.mUseLevel ? (float) getLevel() / 10000.0f : 1.0f;
                switch(st.mOrientation) {
                    case TOP_BOTTOM:
                        x0 = r.left;
                        y0 = r.top;
                        x1 = x0;
                        y1 = level * r.bottom;
                        break;
                    case TR_BL:
                        x0 = r.right;
                        y0 = r.top;
                        x1 = level * r.left;
                        y1 = level * r.bottom;
                        break;
                    case RIGHT_LEFT:
                        x0 = r.right;
                        y0 = r.top;
                        x1 = level * r.left;
                        y1 = y0;
                        break;
                    case BR_TL:
                        x0 = r.right;
                        y0 = r.bottom;
                        x1 = level * r.left;
                        y1 = level * r.top;
                        break;
                    case BOTTOM_TOP:
                        x0 = r.left;
                        y0 = r.bottom;
                        x1 = x0;
                        y1 = level * r.top;
                        break;
                    case BL_TR:
                        x0 = r.left;
                        y0 = r.bottom;
                        x1 = level * r.right;
                        y1 = level * r.top;
                        break;
                    case LEFT_RIGHT:
                        x0 = r.left;
                        y0 = r.top;
                        x1 = level * r.right;
                        y1 = y0;
                        break;
                    default:
                        /* TL_BR */
                        x0 = r.left;
                        y0 = r.top;
                        x1 = level * r.right;
                        y1 = level * r.bottom;
                        break;
                }
                mFillPaint.setShader(new LinearGradient(x0, y0, x1, y1, colors, st.mPositions, Shader.TileMode.CLAMP));
            } else if (st.mGradient == RADIAL_GRADIENT) {
                x0 = r.left + (r.right - r.left) * st.mCenterX;
                y0 = r.top + (r.bottom - r.top) * st.mCenterY;
                final float level = st.mUseLevel ? (float) getLevel() / 10000.0f : 1.0f;
                mFillPaint.setShader(new RadialGradient(x0, y0, level * st.mGradientRadius, colors, null, Shader.TileMode.CLAMP));
            } else if (st.mGradient == SWEEP_GRADIENT) {
                x0 = r.left + (r.right - r.left) * st.mCenterX;
                y0 = r.top + (r.bottom - r.top) * st.mCenterY;
                int[] tempColors = colors;
                float[] tempPositions = null;
                if (st.mUseLevel) {
                    tempColors = st.mTempColors;
                    final int length = colors.length;
                    if (tempColors == null || tempColors.length != length + 1) {
                        tempColors = st.mTempColors = new int[length + 1];
                    }
                    System.arraycopy(colors, 0, tempColors, 0, length);
                    tempColors[length] = colors[length - 1];
                    tempPositions = st.mTempPositions;
                    final float fraction = 1.0f / (float) (length - 1);
                    if (tempPositions == null || tempPositions.length != length + 1) {
                        tempPositions = st.mTempPositions = new float[length + 1];
                    }
                    final float level = (float) getLevel() / 10000.0f;
                    for (int i = 0; i < length; i++) {
                        tempPositions[i] = i * fraction * level;
                    }
                    tempPositions[length] = 1.0f;
                }
                mFillPaint.setShader(new SweepGradient(x0, y0, tempColors, tempPositions));
            }
        }
    }
    return !mRect.isEmpty();
}
Also used : RectF(android.graphics.RectF) LinearGradient(android.graphics.LinearGradient) Rect(android.graphics.Rect) RadialGradient(android.graphics.RadialGradient) SweepGradient(android.graphics.SweepGradient) Paint(android.graphics.Paint)

Aggregations

LinearGradient (android.graphics.LinearGradient)123 Paint (android.graphics.Paint)70 RectF (android.graphics.RectF)38 RadialGradient (android.graphics.RadialGradient)27 Canvas (android.graphics.Canvas)22 Rect (android.graphics.Rect)21 Path (android.graphics.Path)18 Shader (android.graphics.Shader)18 Bitmap (android.graphics.Bitmap)15 Matrix (android.graphics.Matrix)15 Point (android.graphics.Point)13 SweepGradient (android.graphics.SweepGradient)12 PorterDuffXfermode (android.graphics.PorterDuffXfermode)9 ShapeDrawable (android.graphics.drawable.ShapeDrawable)9 SuppressLint (android.annotation.SuppressLint)6 ComposeShader (android.graphics.ComposeShader)6 PaintDrawable (android.graphics.drawable.PaintDrawable)5 RectShape (android.graphics.drawable.shapes.RectShape)5 ColorMatrix (android.graphics.ColorMatrix)3 PointF (android.graphics.PointF)3