private void initPaint() { DisplayMetrics dm = getContext().getResources().getDisplayMetrics(); textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, textSize, dm); mPaint = new Paint(); mPaint.setColor(Color.RED); mPaint.setAntiAlias(true); mPaint.setStrokeWidth(1); mPaint.setTextSize(35); mPaint.setColorFilter(new ColorFilter()); // mPaint.setStyle(Paint.Style.STROKE); mPath = new Path(); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg1); bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.bg2); bitmapShader1 = new BitmapShader(bitmap1, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); shader = new ComposeShader(bitmapShader, bitmapShader1, PorterDuff.Mode.SRC_OVER); }
private Bitmap createColorWheelBitmap(int width, int height) { Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); int colorCount = 12; int colorAngleStep = 360 / 12; int colors[] = new int[colorCount + 1]; float hsv[] = new float[] { 0f, 1f, 1f }; for (int i = 0; i < colors.length; i++) { hsv[0] = (i * colorAngleStep + 180) % 360; colors[i] = Color.HSVToColor(hsv); } colors[colorCount] = colors[0]; SweepGradient sweepGradient = new SweepGradient(width / 2, height / 2, colors, null); RadialGradient radialGradient = new RadialGradient(width / 2, height / 2, colorWheelRadius, 0xFFFFFFFF, 0x00FFFFFF, TileMode.CLAMP); ComposeShader composeShader = new ComposeShader(sweepGradient, radialGradient, PorterDuff.Mode.SRC_OVER); colorWheelPaint.setShader(composeShader); Canvas canvas = new Canvas(bitmap); canvas.drawCircle(width / 2, height / 2, colorWheelRadius, colorWheelPaint); return bitmap; }
protected void updateColorDependant(boolean hsvChanged, boolean hueChanged){ if (hueChanged) { Shader base = new LinearGradient(A.x, A.y, (B.x + C.x) / 2, (B.y + C.y) / 2, Color.HSVToColor(new float[]{mColor.hue(), 1, 1}), Color.BLACK, Shader.TileMode.CLAMP); Shader light = new LinearGradient((A.x + C.x) / 2, (A.y + C.y) / 2, B.x, B.y, Color.BLACK, Color.WHITE, Shader.TileMode.CLAMP); Shader both = new ComposeShader(base, light, PorterDuff.Mode.ADD); paint.setShader(both); } if (hsvChanged) { dotPaint.setColor(mColor.inverted().rgb()); dot = new PointF( C.x + (B.x - C.x + (A.x - B.x) * mColor.sat()) * mColor.val(), C.y + (B.y - C.y + (A.y - B.y) * mColor.sat()) * mColor.val()); } }
@SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width = getMeasuredWidth(); int height = getMeasuredHeight(); int new_color = Color.HSVToColor(hsv); if(color != new_color) { Shader shader_x = new LinearGradient(0.0F, 0.0F, width, 0.0F, Color.WHITE, new_color, TileMode.CLAMP); ComposeShader shader = new ComposeShader(shader_y, shader_x, PorterDuff.Mode.MULTIPLY); paint.setShader(shader); color = new_color; } canvas.drawRect(0.0F, 0.0F, width, height, paint); }
private Bitmap createColorWheelBitmap(int width, int height) { Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); int colorCount = 12; int colorAngleStep = 360 / 12; int colors[] = new int[colorCount + 1]; float hsv[] = new float[]{0f, 1f, 1f}; for (int i = 0; i < colors.length; i++) { hsv[0] = (i * colorAngleStep + 180) % 360; colors[i] = Color.HSVToColor(hsv); } colors[colorCount] = colors[0]; SweepGradient sweepGradient = new SweepGradient(width / 2, height / 2, colors, null); RadialGradient radialGradient = new RadialGradient(width / 2, height / 2, colorWheelRadius, 0xFFFFFFFF, 0x00FFFFFF, TileMode.CLAMP); ComposeShader composeShader = new ComposeShader(sweepGradient, radialGradient, PorterDuff.Mode.SRC_OVER); colorWheelPaint.setShader(composeShader); Canvas canvas = new Canvas(bitmap); canvas.drawCircle(width / 2, height / 2, colorWheelRadius, colorWheelPaint); return bitmap; }
@Override protected void onDraw(final Canvas canvas) { super.onDraw(canvas); if (this.paint == null) { this.paint = new Paint(); this.luar = new LinearGradient(0.f, 0.f, 0.f, this.ukuranUiPx, 0xffffffff, 0xff000000, TileMode.CLAMP); } this.tmp00[1] = this.tmp00[2] = 1.f; this.tmp00[0] = this.hue; int rgb = Color.HSVToColor(this.tmp00); this.dalam = new LinearGradient(0.f, 0.f, this.ukuranUiPx, 0.f, 0xffffffff, rgb, TileMode.CLAMP); ComposeShader shader = new ComposeShader(this.luar, this.dalam, PorterDuff.Mode.MULTIPLY); this.paint.setShader(shader); canvas.drawRect(0.f, 0.f, this.ukuranUiPx, this.ukuranUiPx, this.paint); }
@Override public void onClick(final View v) { switch (new Random().nextInt(5)) { case 0: mPaint.setShader(sweepGradient); break; case 1: mPaint.setShader(linearGradient); break; case 2: mPaint.setShader(radialGradient); break; case 3: mPaint.setShader(bitmapShader1); break; case 4: final Shader composeShader = new ComposeShader(bitmapShader1, new BitmapShader(BitmapFactory.decodeResource(getResources(), R.drawable.ic_msg_unread), Shader.TileMode.REPEAT, Shader.TileMode.REPEAT) , intToMode(new Random().nextInt(16))); mPaint.setShader(composeShader); break; } invalidate(); }
private void drawSatValPanel(Canvas canvas) { RectF rect = this.mSatValRect; this.mBorderPaint.setColor(this.mBorderColor); canvas.drawRect(this.mDrawingRect.left, this.mDrawingRect.top, BORDER_WIDTH_PX + rect.right, BORDER_WIDTH_PX + rect.bottom, this.mBorderPaint); if (this.mValShader == null) { this.mValShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom, -1, ViewCompat.MEASURED_STATE_MASK, TileMode.CLAMP); } this.mSatShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top, -1, Color.HSVToColor(new float[]{this.mHue, BORDER_WIDTH_PX, BORDER_WIDTH_PX}), TileMode.CLAMP); this.mSatValPaint.setShader(new ComposeShader(this.mValShader, this.mSatShader, Mode.MULTIPLY)); canvas.drawRect(rect, this.mSatValPaint); Point p = satValToPoint(this.mSat, this.mVal); this.mSatValTrackerPaint.setColor(ViewCompat.MEASURED_STATE_MASK); canvas.drawCircle((float) p.x, (float) p.y, this.PALETTE_CIRCLE_TRACKER_RADIUS - (BORDER_WIDTH_PX * this.mDensity), this.mSatValTrackerPaint); this.mSatValTrackerPaint.setColor(0xffdddddd); canvas.drawCircle((float) p.x, (float) p.y, this.PALETTE_CIRCLE_TRACKER_RADIUS, this.mSatValTrackerPaint); }
private void updateMainColors(int color) { if (baseColor == color) { return; } baseColor = color; Shader gradientShader = new LinearGradient(0.f, 0.f, NUMBER_OF_GRADIENTS, 0.f, Color.WHITE, baseColor, Shader.TileMode.CLAMP); ComposeShader shader = new ComposeShader(gradientShader, baseGradientShader, PorterDuff.Mode.MULTIPLY); gradientsPaint.setShader(shader); preRenderingCanvas .drawRect(0.f, 0.f, NUMBER_OF_GRADIENTS, NUMBER_OF_GRADIENTS, gradientsPaint); }
/** * Set default outer rim paint * * @return paint * Paint for outer rim */ private Paint getDefaultOuterRimPaint() { /** Linear gradient to create the 3D effect */ final LinearGradient verticalGradient = new LinearGradient(mOuterRimRect.left, mOuterRimRect.top, mOuterRimRect.left, mOuterRimRect.bottom, Color.rgb(255, 255, 255), Color.rgb(84, 90, 100), TileMode.REPEAT); /** Bitmap for outer rim */ final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.darkwood); /** Bitmap shader for the metallic style */ final BitmapShader outerRimTile = new BitmapShader(bitmap, TileMode.REPEAT, TileMode.REPEAT); /** Matrix for outer rim */ final Matrix matrix = new Matrix(); matrix.setScale(1.0f / bitmap.getWidth(), 1.0f / bitmap.getHeight()); outerRimTile.setLocalMatrix(matrix); /** Paint for outer rim */ final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setShader(new ComposeShader(verticalGradient, outerRimTile, PorterDuff.Mode.MULTIPLY)); paint.setFilterBitmap(true); return paint; }
/** * Set default inner rim paint * * @return paint * Paint for inner rim */ private Paint getDefaultInnerRimPaint() { /** Linear gradient to create the 3D effect */ final LinearGradient verticalGradient = new LinearGradient(mOuterRimRect.left, mOuterRimRect.top, mOuterRimRect.left, mOuterRimRect.bottom, Color.rgb(255, 255, 255), Color.rgb(84, 90, 100), TileMode.REPEAT); /** Bitmap for inner rim */ final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.darkerwood); /** Bitmap shader for the metallic style */ final BitmapShader innerRimTile = new BitmapShader(bitmap, TileMode.REPEAT, TileMode.REPEAT); /** Matrix for inner rim */ final Matrix matrix = new Matrix(); matrix.setScale(1.0f / bitmap.getWidth(), 1.0f / bitmap.getHeight()); innerRimTile.setLocalMatrix(matrix); /** Paint for outer rim */ final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setShader(new ComposeShader(verticalGradient, innerRimTile, PorterDuff.Mode.MULTIPLY)); paint.setFilterBitmap(true); return paint; }
public Paint getDefaultOuterRimPaint() { // Use a linear gradient to create the 3D effect final LinearGradient verticalGradient = new LinearGradient(mOuterRimRect.left, mOuterRimRect.top, mOuterRimRect.left, mOuterRimRect.bottom, Color.rgb(255, 255, 255), Color.rgb(84, 90, 100), TileMode.REPEAT); // Use a Bitmap shader for the metallic style final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.light_alu); final BitmapShader aluminiumTile = new BitmapShader(bitmap, TileMode.REPEAT, TileMode.REPEAT); final Matrix matrix = new Matrix(); matrix.setScale(1.0f / bitmap.getWidth(), 1.0f / bitmap.getHeight()); aluminiumTile.setLocalMatrix(matrix); final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setShader(new ComposeShader(verticalGradient, aluminiumTile, PorterDuff.Mode.MULTIPLY)); paint.setFilterBitmap(true); return paint; }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if(paint==null) { paint = new Paint(); lg = new LinearGradient(0.0f, 0.0f, 0.0f, this.getMeasuredHeight(), 0xffffffff, 0xff000000, TileMode.CLAMP); } if(updateColor){ int rgb = Color.HSVToColor(color); lg2 = new LinearGradient(0.0f, 0.0f, this.getMeasuredWidth(), 0.0f, 0xffffffff, rgb, TileMode.CLAMP); shader = new ComposeShader(lg, lg2, PorterDuff.Mode.MULTIPLY); updateColor = false; } paint.setShader(shader); canvas.drawRect(0.0f, 0.0f, this.getMeasuredWidth(), this.getMeasuredHeight(), paint); }
@Override protected void onBoundsChange(Rect bounds) { super.onBoundsChange(bounds); mRect.set(mMargin, mMargin, bounds.width() - mMargin, bounds.height() - mMargin); if (USE_VIGNETTE) { RadialGradient vignette = new RadialGradient( mRect.centerX(), mRect.centerY() * 1.0f / 0.7f, mRect.centerX() * 1.3f, new int[] { 0, 0, 0x7f000000 }, new float[] { 0.0f, 0.7f, 1.0f }, Shader.TileMode.CLAMP); Matrix oval = new Matrix(); oval.setScale(1.0f, 0.7f); vignette.setLocalMatrix(oval); mPaint.setShader( new ComposeShader(mBitmapShader, vignette, PorterDuff.Mode.SRC_OVER)); } }
@Override public Bitmap transform(Bitmap source) { Bitmap outBitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), source.getConfig()); // Create shaders Shader bitmapShader = new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); Shader linearGradient = new LinearGradient(0, source.getHeight() / 2, 0, source.getHeight(), Color.TRANSPARENT, 0xB4000000, Shader.TileMode.CLAMP); // create a shader that combines both effects ComposeShader shader = new ComposeShader(bitmapShader, linearGradient, PorterDuff.Mode.DST_OUT); Paint p = new Paint(); p.setAntiAlias(true); p.setShader(shader); Paint black = new Paint(); black.setColor(Color.BLACK); Canvas c = new Canvas(outBitmap); c.drawRect(0, 0, source.getWidth(), source.getHeight(), black); c.drawPaint(p); source.recycle(); return outBitmap; }