Search in sources :

Example 6 with SweepGradient

use of android.graphics.SweepGradient in project android_frameworks_base by DirtyUnicorns.

the class GradientDrawable method ensureValidRect.

/**
     * This checks mGradientIsDirty, 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 (mGradientIsDirty) {
        mGradientIsDirty = 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[] gradientColors = st.mGradientColors;
        if (gradientColors != null) {
            final RectF r = mRect;
            final float x0, x1, y0, y1;
            if (st.mGradient == LINEAR_GRADIENT) {
                final float level = st.mUseLevel ? 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, gradientColors, 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;
                float radius = st.mGradientRadius;
                if (st.mGradientRadiusType == RADIUS_TYPE_FRACTION) {
                    // Fall back to parent width or height if intrinsic
                    // size is not specified.
                    final float width = st.mWidth >= 0 ? st.mWidth : r.width();
                    final float height = st.mHeight >= 0 ? st.mHeight : r.height();
                    radius *= Math.min(width, height);
                } else if (st.mGradientRadiusType == RADIUS_TYPE_FRACTION_PARENT) {
                    radius *= Math.min(r.width(), r.height());
                }
                if (st.mUseLevel) {
                    radius *= getLevel() / 10000.0f;
                }
                mGradientRadius = radius;
                if (radius <= 0) {
                    // We can't have a shader with non-positive radius, so
                    // let's have a very, very small radius.
                    radius = 0.001f;
                }
                mFillPaint.setShader(new RadialGradient(x0, y0, radius, gradientColors, 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 = gradientColors;
                float[] tempPositions = null;
                if (st.mUseLevel) {
                    tempColors = st.mTempColors;
                    final int length = gradientColors.length;
                    if (tempColors == null || tempColors.length != length + 1) {
                        tempColors = st.mTempColors = new int[length + 1];
                    }
                    System.arraycopy(gradientColors, 0, tempColors, 0, length);
                    tempColors[length] = gradientColors[length - 1];
                    tempPositions = st.mTempPositions;
                    final float fraction = 1.0f / (length - 1);
                    if (tempPositions == null || tempPositions.length != length + 1) {
                        tempPositions = st.mTempPositions = new float[length + 1];
                    }
                    final float level = 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));
            }
            // maxed out so that alpha modulation works correctly.
            if (st.mSolidColors == null) {
                mFillPaint.setColor(Color.BLACK);
            }
        }
    }
    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)

Example 7 with SweepGradient

use of android.graphics.SweepGradient in project android_frameworks_base by ResurrectionRemix.

the class GradientColor method onColorsChange.

private void onColorsChange() {
    int[] tempColors = null;
    float[] tempOffsets = null;
    if (mItemColors != null) {
        int length = mItemColors.length;
        tempColors = new int[length];
        tempOffsets = new float[length];
        for (int i = 0; i < length; i++) {
            tempColors[i] = mItemColors[i];
            tempOffsets[i] = mItemOffsets[i];
        }
    } else {
        if (mHasCenterColor) {
            tempColors = new int[3];
            tempColors[0] = mStartColor;
            tempColors[1] = mCenterColor;
            tempColors[2] = mEndColor;
            tempOffsets = new float[3];
            tempOffsets[0] = 0.0f;
            // Since 0.5f is default value, try to take the one that isn't 0.5f
            tempOffsets[1] = 0.5f;
            tempOffsets[2] = 1f;
        } else {
            tempColors = new int[2];
            tempColors[0] = mStartColor;
            tempColors[1] = mEndColor;
        }
    }
    if (tempColors.length < 2) {
        Log.w(TAG, "<gradient> tag requires 2 color values specified!" + tempColors.length + " " + tempColors);
    }
    if (mGradientType == GradientDrawable.LINEAR_GRADIENT) {
        mShader = new LinearGradient(mStartX, mStartY, mEndX, mEndY, tempColors, tempOffsets, parseTileMode(mTileMode));
    } else {
        if (mGradientType == GradientDrawable.RADIAL_GRADIENT) {
            mShader = new RadialGradient(mCenterX, mCenterY, mGradientRadius, tempColors, tempOffsets, parseTileMode(mTileMode));
        } else {
            mShader = new SweepGradient(mCenterX, mCenterY, tempColors, tempOffsets);
        }
    }
    mDefaultColor = tempColors[0];
}
Also used : LinearGradient(android.graphics.LinearGradient) RadialGradient(android.graphics.RadialGradient) SweepGradient(android.graphics.SweepGradient)

Example 8 with SweepGradient

use of android.graphics.SweepGradient in project JustAndroid by chinaltz.

the class AbCircleProgressBar method onDraw.

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (reset) {
        canvas.drawColor(Color.TRANSPARENT);
        reset = false;
    }
    this.width = getMeasuredWidth();
    this.height = getMeasuredHeight();
    this.radius = getMeasuredWidth() / 2 - pathWidth;
    // 设置画笔颜色
    pathPaint.setColor(pathColor);
    // 设置画笔宽度
    pathPaint.setStrokeWidth(pathWidth);
    //添加浮雕效果
    pathPaint.setMaskFilter(emboss);
    // 在中心的地方画个半径为r的圆
    canvas.drawCircle(this.width / 2, this.height / 2, radius, pathPaint);
    //边线
    pathPaint.setStrokeWidth(0.5f);
    pathPaint.setColor(pathBorderColor);
    canvas.drawCircle(this.width / 2, this.height / 2, radius + pathWidth / 2 + 0.5f, pathPaint);
    canvas.drawCircle(this.width / 2, this.height / 2, radius - pathWidth / 2 - 0.5f, pathPaint);
    /*int[] gradientColors = new int[3];  
		gradientColors[0] = Color.GREEN;  
		gradientColors[1] = Color.YELLOW;  
		gradientColors[2] = Color.RED;  
		float[] gradientPositions = new float[3];  
		gradientPositions[0] = 0.0f;  
		gradientPositions[1] = 0.5f;  
		gradientPositions[2] = 1.0f;  
		
		//按颜色比例圆形填充
		RadialGradient radialGradientShader = new RadialGradient(this.width/2,this.height/2, 
				radius, gradientColors, gradientPositions, TileMode.CLAMP); 
		
		paint1.setShader(radialGradientShader);*/
    //环形颜色填充
    SweepGradient sweepGradient = new SweepGradient(this.width / 2, this.height / 2, arcColors, null);
    fillArcPaint.setShader(sweepGradient);
    // 设置画笔为白色
    //模糊效果
    fillArcPaint.setMaskFilter(mBlur);
    //设置线的类型,边是圆的
    fillArcPaint.setStrokeCap(Paint.Cap.ROUND);
    //fillArcPaint.setColor(Color.BLUE);
    fillArcPaint.setStrokeWidth(pathWidth);
    // 设置类似于左上角坐标,右下角坐标
    oval.set(this.width / 2 - radius, this.height / 2 - radius, this.width / 2 + radius, this.height / 2 + radius);
    // 画圆弧,第二个参数为:起始角度,第三个为跨的角度,第四个为true的时候是实心,false的时候为空心
    canvas.drawArc(oval, -90, ((float) progress / max) * 360, false, fillArcPaint);
}
Also used : SweepGradient(android.graphics.SweepGradient)

Example 9 with SweepGradient

use of android.graphics.SweepGradient in project GossipView by jianghejie.

the class GossipView method onMeasure.

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
    int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
    int min = Math.min(width, height);
    mWidth = min;
    outArctrokeWidth = min / 6;
    mInnerArctrokeWidth = outArctrokeWidth / 7;
    mInnerArcPaint.setStrokeWidth(mInnerArctrokeWidth);
    mOuterTextPaint.setTextSize(outArctrokeWidth / 3);
    mOuterArcPaint.setStrokeWidth(outArctrokeWidth);
    mOuterArcRadius = mWidth - outArctrokeWidth / 2 - padding;
    mInnerArcRadius = mWidth / 6;
    mProgressPaint.setStrokeWidth(mInnerArctrokeWidth);
    mSweepGradient = new SweepGradient(getOriginal().x, getOriginal().y, 0xfff39700, Color.WHITE);
    mProgressPaint.setShader(mSweepGradient);
    mOuterArcRectangle.set(outArctrokeWidth / 2 + padding, outArctrokeWidth / 2 + padding, mOuterArcRadius, mOuterArcRadius);
    mInnerArcRectangle.set(mWidth / 2 - mInnerArcRadius, mWidth / 2 - mInnerArcRadius, mWidth / 2 + mInnerArcRadius, mWidth / 2 + mInnerArcRadius);
    mInnerBackGroud.setBounds((int) outArctrokeWidth + padding, (int) outArctrokeWidth + padding, (int) (min - outArctrokeWidth - padding), (int) (min - outArctrokeWidth - padding));
    mOuterBackGroud.setBounds(0, 0, mWidth, mWidth);
    setMeasuredDimension(min, min);
}
Also used : SweepGradient(android.graphics.SweepGradient) Paint(android.graphics.Paint)

Example 10 with SweepGradient

use of android.graphics.SweepGradient in project k-9 by k9mail.

the class ColorPicker method init.

private void init(AttributeSet attrs, int defStyle) {
    final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ColorPicker, defStyle, 0);
    mColorWheelStrokeWidth = a.getInteger(R.styleable.ColorPicker_wheel_size, 16);
    mPointerRadius = a.getInteger(R.styleable.ColorPicker_pointer_size, 48);
    a.recycle();
    Shader s = new SweepGradient(0, 0, COLORS, null);
    mColorWheelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mColorWheelPaint.setShader(s);
    mColorWheelPaint.setStyle(Paint.Style.STROKE);
    mColorWheelPaint.setStrokeWidth(mColorWheelStrokeWidth);
    mPointerHaloPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPointerHaloPaint.setColor(Color.BLACK);
    mPointerHaloPaint.setStrokeWidth(5);
    mPointerHaloPaint.setAlpha(0x60);
    mPointerColor = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPointerColor.setStrokeWidth(5);
    mAngle = (float) (-Math.PI / 2);
    mPointerColor.setColor(calculateColor(mAngle));
}
Also used : TypedArray(android.content.res.TypedArray) SweepGradient(android.graphics.SweepGradient) Paint(android.graphics.Paint) Shader(android.graphics.Shader)

Aggregations

SweepGradient (android.graphics.SweepGradient)21 Paint (android.graphics.Paint)14 RadialGradient (android.graphics.RadialGradient)14 LinearGradient (android.graphics.LinearGradient)12 Rect (android.graphics.Rect)7 RectF (android.graphics.RectF)7 SuppressLint (android.annotation.SuppressLint)4 Bitmap (android.graphics.Bitmap)2 Canvas (android.graphics.Canvas)2 ComposeShader (android.graphics.ComposeShader)2 TypedArray (android.content.res.TypedArray)1 Matrix (android.graphics.Matrix)1 Path (android.graphics.Path)1 Shader (android.graphics.Shader)1