use of android.graphics.LinearGradient in project AndroidUtilCode by Blankj.
the class ImageUtils method addReflection.
/**
* 添加倒影
*
* @param src 源图片的
* @param reflectionHeight 倒影高度
* @param recycle 是否回收
* @return 带倒影图片
*/
public static Bitmap addReflection(Bitmap src, int reflectionHeight, boolean recycle) {
if (isEmptyBitmap(src))
return null;
// 原图与倒影之间的间距
final int REFLECTION_GAP = 0;
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionBitmap = Bitmap.createBitmap(src, 0, srcHeight - reflectionHeight, srcWidth, reflectionHeight, matrix, false);
Bitmap ret = Bitmap.createBitmap(srcWidth, srcHeight + reflectionHeight, src.getConfig());
Canvas canvas = new Canvas(ret);
canvas.drawBitmap(src, 0, 0, null);
canvas.drawBitmap(reflectionBitmap, 0, srcHeight + REFLECTION_GAP, null);
Paint paint = new Paint();
paint.setAntiAlias(true);
LinearGradient shader = new LinearGradient(0, srcHeight, 0, ret.getHeight() + REFLECTION_GAP, 0x70FFFFFF, 0x00FFFFFF, Shader.TileMode.MIRROR);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));
canvas.drawRect(0, srcHeight + REFLECTION_GAP, srcWidth, ret.getHeight(), paint);
if (!reflectionBitmap.isRecycled())
reflectionBitmap.recycle();
if (recycle && !src.isRecycled())
src.recycle();
return ret;
}
use of android.graphics.LinearGradient in project android_packages_apps_DSPManager by CyanogenMod.
the class EqualizerSurface method onLayout.
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
final Resources res = getResources();
mWidth = right - left;
mHeight = bottom - top;
float barWidth = res.getDimension(R.dimen.bar_width);
mControlBar.setStrokeWidth(barWidth);
mControlBarKnob.setShadowLayer(barWidth * 0.5f, 0, 0, res.getColor(R.color.off_white));
/**
* red > +7
* yellow > +3
* holo_blue_bright > 0
* holo_blue < 0
* holo_blue_dark < 3
*/
int[] responseColors = new int[] { res.getColor(R.color.eq_red), res.getColor(R.color.eq_yellow), res.getColor(R.color.eq_holo_bright), res.getColor(R.color.eq_holo_blue), res.getColor(R.color.eq_holo_dark) };
float[] responsePositions = new float[] { 0, 0.2f, 0.45f, 0.6f, 1f };
mFrequencyResponseBg.setShader(new LinearGradient(0, 0, 0, mHeight, responseColors, responsePositions, Shader.TileMode.CLAMP));
int[] barColors = new int[] { res.getColor(R.color.cb_shader), res.getColor(R.color.cb_shader_alpha) };
float[] barPositions = new float[] { 0, 1 };
mControlBar.setShader(new LinearGradient(0, 0, 0, mHeight, barColors, barPositions, Shader.TileMode.CLAMP));
}
use of android.graphics.LinearGradient 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();
}
use of android.graphics.LinearGradient in project DiscreteScrollView by yarolegovich.
the class ForecastView method initGradient.
private void initGradient() {
float centerX = getWidth() * 0.5f;
Shader gradient = new LinearGradient(centerX, 0, centerX, getHeight(), currentGradient, null, Shader.TileMode.MIRROR);
gradientPaint.setShader(gradient);
}
use of android.graphics.LinearGradient in project android_frameworks_base by DirtyUnicorns.
the class FakeShadowDrawable method buildShadowCorners.
private void buildShadowCorners() {
RectF innerBounds = new RectF(-mCornerRadius, -mCornerRadius, mCornerRadius, mCornerRadius);
RectF outerBounds = new RectF(innerBounds);
outerBounds.inset(-mShadowSize, -mShadowSize);
if (mCornerShadowPath == null) {
mCornerShadowPath = new Path();
} else {
mCornerShadowPath.reset();
}
mCornerShadowPath.setFillType(Path.FillType.EVEN_ODD);
mCornerShadowPath.moveTo(-mCornerRadius, 0);
mCornerShadowPath.rLineTo(-mShadowSize, 0);
// outer arc
mCornerShadowPath.arcTo(outerBounds, 180f, 90f, false);
// inner arc
mCornerShadowPath.arcTo(innerBounds, 270f, -90f, false);
mCornerShadowPath.close();
float startRatio = mCornerRadius / (mCornerRadius + mShadowSize);
mCornerShadowPaint.setShader(new RadialGradient(0, 0, mCornerRadius + mShadowSize, new int[] { mShadowStartColor, mShadowStartColor, mShadowEndColor }, new float[] { 0f, startRatio, 1f }, Shader.TileMode.CLAMP));
// we offset the content shadowSize/2 pixels up to make it more realistic.
// this is why edge shadow shader has some extra space
// When drawing bottom edge shadow, we use that extra space.
mEdgeShadowPaint.setShader(new LinearGradient(0, -mCornerRadius + mShadowSize, 0, -mCornerRadius - mShadowSize, new int[] { mShadowStartColor, mShadowStartColor, mShadowEndColor }, new float[] { 0f, .5f, 1f }, Shader.TileMode.CLAMP));
}
Aggregations