List of usage examples for android.graphics Paint setShader
public Shader setShader(Shader shader)
From source file:org.linphone.ContactEditorFragment.java
private void editContactPicture(final String filePath, final Bitmap image) { int SIZE_SMALL = 256; int COMPRESSOR_QUALITY = 100; Bitmap bitmapUnknown = BitmapFactory.decodeResource(getResources(), R.drawable.avatar); Bitmap bm = null;//from w w w . j a va 2s. c o m if (filePath != null) { int pixelsMax = SIZE_SMALL; //Resize image bm = BitmapFactory.decodeFile(filePath); if (bm != null) { if (bm.getWidth() > bm.getHeight() && bm.getWidth() > pixelsMax) { bm = Bitmap.createScaledBitmap(bm, 256, 256, false); } } } else if (image != null) { bm = image; } // Rotate the bitmap if possible/needed, using EXIF data try { if (imageToUploadUri != null && filePath != null) { ExifInterface exif = new ExifInterface(filePath); int pictureOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0); Matrix matrix = new Matrix(); if (pictureOrientation == 6) { matrix.postRotate(90); } else if (pictureOrientation == 3) { matrix.postRotate(180); } else if (pictureOrientation == 8) { matrix.postRotate(270); } bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); } } catch (Exception e) { e.printStackTrace(); } Bitmap bitmapRounded; if (bm != null) { bitmapRounded = Bitmap.createScaledBitmap(bm, bitmapUnknown.getWidth(), bitmapUnknown.getWidth(), false); Canvas canvas = new Canvas(bitmapRounded); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setShader(new BitmapShader(bitmapRounded, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); canvas.drawCircle(bitmapRounded.getWidth() / 2 + 0.7f, bitmapRounded.getHeight() / 2 + 0.7f, bitmapRounded.getWidth() / 2 + 0.1f, paint); contactPicture.setImageBitmap(bitmapRounded); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, COMPRESSOR_QUALITY, outStream); photoToAdd = outStream.toByteArray(); } }
From source file:com.viewpagerindicator.RichTitlePageIndicator.java
private void drawLeftItem(Canvas canvas, int pos, Rect bound, Paint paint, boolean drawIcon) { int c1 = paint.getColor(); int c2 = 0;//from w w w . ja v a2 s. c o m Paint blur = new Paint(paint); Shader shader = new LinearGradient(bound.left, 0, bound.right, 0, new int[] { c2, c1 }, new float[] { 0, 1 }, TileMode.CLAMP); blur.setShader(shader); drawItem(canvas, pos, bound, blur, drawIcon); //paint.setShader(null); }
From source file:com.viewpagerindicator.RichTitlePageIndicator.java
private void drawRightItem(Canvas canvas, int pos, Rect bound, Paint paint, boolean drawIcon) { int c1 = paint.getColor(); int c2 = 0;// w w w.j a va 2 s . com Paint blur = new Paint(paint); Shader shader = new LinearGradient(bound.left, 0, bound.right, 0, new int[] { c1, c2 }, new float[] { 0, 1 }, TileMode.CLAMP); blur.setShader(shader); drawItem(canvas, pos, bound, blur, drawIcon); //paint.setShader(null); }
From source file:info.tellmetime.TellmetimeActivity.java
/** * Generates rainbow gradient and sets it as #mSeekBarHighlight progress drawable. *//* w w w .ja v a 2 s .c o m*/ private void drawRainbow() { float gradientWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 256, getResources().getDisplayMetrics()); int[] colors = { 0xFFFFFFFF, 0xFFFF0000, 0xFFFFFF00, 0xFF00FF00, 0xFF00FFFF, 0xFF0000FF, 0xFFFF00FF, 0xFF888888, 0xFF000000 }; LinearGradient rainbowGradient = new LinearGradient(0f, 0f, gradientWidth, 0f, colors, null, Shader.TileMode.CLAMP); float r = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics()); ShapeDrawable shape = new ShapeDrawable( new RoundRectShape(new float[] { r, r, r, r, r, r, r, r }, null, null)); shape.getPaint().setShader(rainbowGradient); mSeekBarHighlight.setProgressDrawable(shape); // Generate bitmap with the same rainbow gradient and cache it for later use to retrieve color at given position. Bitmap bitmap = Bitmap.createBitmap((int) gradientWidth, 1, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setStyle(Paint.Style.FILL); paint.setShader(rainbowGradient); canvas.drawRect(0, 0, gradientWidth, 1, paint); mRainbow = bitmap; // Set max value to gradient's width and restore relative position. mSeekBarHighlight.setMax((int) (gradientWidth - 1)); mSeekBarHighlight.setProgress((int) ((gradientWidth - 1) * mHighlightPosition)); }
From source file:org.onebusaway.android.map.googlemapsv2.StopOverlay.java
/** * Creates a bus stop icon with the given direction arrow, or without a direction arrow if * the direction is NO_DIRECTION/*from w ww.ja v a 2 s . co m*/ * * @param direction Bus stop direction, obtained from ObaStop.getDirection() and defined in * constants in this class, or NO_DIRECTION if the stop icon shouldn't have a * direction arrow * @return a bus stop icon bitmap with the arrow pointing the given direction, or with no arrow * if direction is NO_DIRECTION */ private static Bitmap createBusStopIcon(String direction) throws NullPointerException { if (direction == null) { throw new IllegalArgumentException(direction); } Resources r = Application.get().getResources(); Context context = Application.get(); Float directionAngle = null; // 0-360 degrees Bitmap bm; Canvas c; Drawable shape; Float rotationX = null, rotationY = null; // Point around which to rotate the arrow Paint arrowPaintFill = new Paint(); arrowPaintFill.setStyle(Paint.Style.FILL); arrowPaintFill.setAntiAlias(true); if (direction.equals(NO_DIRECTION)) { // Don't draw the arrow bm = Bitmap.createBitmap(mPx, mPx, Bitmap.Config.ARGB_8888); c = new Canvas(bm); shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon); shape.setBounds(0, 0, bm.getWidth(), bm.getHeight()); } else if (direction.equals(NORTH)) { directionAngle = 0f; bm = Bitmap.createBitmap(mPx, (int) (mPx + mBuffer), Bitmap.Config.ARGB_8888); c = new Canvas(bm); shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon); shape.setBounds(0, (int) mBuffer, mPx, bm.getHeight()); // Shade with darkest color at tip of arrow arrowPaintFill.setShader(new LinearGradient(bm.getWidth() / 2, 0, bm.getWidth() / 2, mArrowHeightPx, r.getColor(R.color.theme_primary), r.getColor(R.color.theme_accent), Shader.TileMode.MIRROR)); // For NORTH, no rotation occurs - use center of image anyway so we have some value rotationX = bm.getWidth() / 2f; rotationY = bm.getHeight() / 2f; } else if (direction.equals(NORTH_WEST)) { directionAngle = 315f; // Arrow is drawn N, rotate 315 degrees bm = Bitmap.createBitmap((int) (mPx + mBuffer), (int) (mPx + mBuffer), Bitmap.Config.ARGB_8888); c = new Canvas(bm); shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon); shape.setBounds((int) mBuffer, (int) mBuffer, bm.getWidth(), bm.getHeight()); // Shade with darkest color at tip of arrow arrowPaintFill.setShader(new LinearGradient(0, 0, mBuffer, mBuffer, r.getColor(R.color.theme_primary), r.getColor(R.color.theme_accent), Shader.TileMode.MIRROR)); // Rotate around below coordinates (trial and error) rotationX = mPx / 2f + mBuffer / 2f; rotationY = bm.getHeight() / 2f - mBuffer / 2f; } else if (direction.equals(WEST)) { directionAngle = 0f; // Arrow is drawn pointing West, so no rotation bm = Bitmap.createBitmap((int) (mPx + mBuffer), mPx, Bitmap.Config.ARGB_8888); c = new Canvas(bm); shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon); shape.setBounds((int) mBuffer, 0, bm.getWidth(), bm.getHeight()); arrowPaintFill.setShader(new LinearGradient(0, bm.getHeight() / 2, mArrowHeightPx, bm.getHeight() / 2, r.getColor(R.color.theme_primary), r.getColor(R.color.theme_accent), Shader.TileMode.MIRROR)); // For WEST rotationX = bm.getHeight() / 2f; rotationY = bm.getHeight() / 2f; } else if (direction.equals(SOUTH_WEST)) { directionAngle = 225f; // Arrow is drawn N, rotate 225 degrees bm = Bitmap.createBitmap((int) (mPx + mBuffer), (int) (mPx + mBuffer), Bitmap.Config.ARGB_8888); c = new Canvas(bm); shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon); shape.setBounds((int) mBuffer, 0, bm.getWidth(), mPx); arrowPaintFill.setShader(new LinearGradient(0, bm.getHeight(), mBuffer, bm.getHeight() - mBuffer, r.getColor(R.color.theme_primary), r.getColor(R.color.theme_accent), Shader.TileMode.MIRROR)); // Rotate around below coordinates (trial and error) rotationX = bm.getWidth() / 2f - mBuffer / 4f; rotationY = mPx / 2f + mBuffer / 4f; } else if (direction.equals(SOUTH)) { directionAngle = 180f; // Arrow is drawn N, rotate 180 degrees bm = Bitmap.createBitmap(mPx, (int) (mPx + mBuffer), Bitmap.Config.ARGB_8888); c = new Canvas(bm); shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon); shape.setBounds(0, 0, bm.getWidth(), (int) (bm.getHeight() - mBuffer)); arrowPaintFill.setShader(new LinearGradient(bm.getWidth() / 2, bm.getHeight(), bm.getWidth() / 2, bm.getHeight() - mArrowHeightPx, r.getColor(R.color.theme_primary), r.getColor(R.color.theme_accent), Shader.TileMode.MIRROR)); rotationX = bm.getWidth() / 2f; rotationY = bm.getHeight() / 2f; } else if (direction.equals(SOUTH_EAST)) { directionAngle = 135f; // Arrow is drawn N, rotate 135 degrees bm = Bitmap.createBitmap((int) (mPx + mBuffer), (int) (mPx + mBuffer), Bitmap.Config.ARGB_8888); c = new Canvas(bm); shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon); shape.setBounds(0, 0, mPx, mPx); arrowPaintFill.setShader(new LinearGradient(bm.getWidth(), bm.getHeight(), bm.getWidth() - mBuffer, bm.getHeight() - mBuffer, r.getColor(R.color.theme_primary), r.getColor(R.color.theme_accent), Shader.TileMode.MIRROR)); // Rotate around below coordinates (trial and error) rotationX = (mPx + mBuffer / 2) / 2f; rotationY = bm.getHeight() / 2f; } else if (direction.equals(EAST)) { directionAngle = 180f; // Arrow is drawn pointing West, so rotate 180 bm = Bitmap.createBitmap((int) (mPx + mBuffer), mPx, Bitmap.Config.ARGB_8888); c = new Canvas(bm); shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon); shape.setBounds(0, 0, mPx, bm.getHeight()); arrowPaintFill.setShader(new LinearGradient(bm.getWidth(), bm.getHeight() / 2, bm.getWidth() - mArrowHeightPx, bm.getHeight() / 2, r.getColor(R.color.theme_primary), r.getColor(R.color.theme_accent), Shader.TileMode.MIRROR)); rotationX = bm.getWidth() / 2f; rotationY = bm.getHeight() / 2f; } else if (direction.equals(NORTH_EAST)) { directionAngle = 45f; // Arrow is drawn pointing N, so rotate 45 degrees bm = Bitmap.createBitmap((int) (mPx + mBuffer), (int) (mPx + mBuffer), Bitmap.Config.ARGB_8888); c = new Canvas(bm); shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon); shape.setBounds(0, (int) mBuffer, mPx, bm.getHeight()); // Shade with darkest color at tip of arrow arrowPaintFill.setShader(new LinearGradient(bm.getWidth(), 0, bm.getWidth() - mBuffer, mBuffer, r.getColor(R.color.theme_primary), r.getColor(R.color.theme_accent), Shader.TileMode.MIRROR)); // Rotate around middle of circle rotationX = (float) mPx / 2; rotationY = bm.getHeight() - (float) mPx / 2; } else { throw new IllegalArgumentException(direction); } shape.draw(c); if (direction.equals(NO_DIRECTION)) { // Everything after this point is for drawing the arrow image, so return the bitmap as-is for no arrow return bm; } /** * Draw the arrow - all dimensions should be relative to px so the arrow is drawn the same * size for all orientations */ // Height of the cutout in the bottom of the triangle that makes it an arrow (0=triangle) final float CUTOUT_HEIGHT = mPx / 12; Path path = new Path(); float x1 = 0, y1 = 0; // Tip of arrow float x2 = 0, y2 = 0; // lower left float x3 = 0, y3 = 0; // cutout in arrow bottom float x4 = 0, y4 = 0; // lower right if (direction.equals(NORTH) || direction.equals(SOUTH) || direction.equals(NORTH_EAST) || direction.equals(SOUTH_EAST) || direction.equals(NORTH_WEST) || direction.equals(SOUTH_WEST)) { // Arrow is drawn pointing NORTH // Tip of arrow x1 = mPx / 2; y1 = 0; // lower left x2 = (mPx / 2) - (mArrowWidthPx / 2); y2 = mArrowHeightPx; // cutout in arrow bottom x3 = mPx / 2; y3 = mArrowHeightPx - CUTOUT_HEIGHT; // lower right x4 = (mPx / 2) + (mArrowWidthPx / 2); y4 = mArrowHeightPx; } else if (direction.equals(EAST) || direction.equals(WEST)) { // Arrow is drawn pointing WEST // Tip of arrow x1 = 0; y1 = mPx / 2; // lower left x2 = mArrowHeightPx; y2 = (mPx / 2) - (mArrowWidthPx / 2); // cutout in arrow bottom x3 = mArrowHeightPx - CUTOUT_HEIGHT; y3 = mPx / 2; // lower right x4 = mArrowHeightPx; y4 = (mPx / 2) + (mArrowWidthPx / 2); } path.setFillType(Path.FillType.EVEN_ODD); path.moveTo(x1, y1); path.lineTo(x2, y2); path.lineTo(x3, y3); path.lineTo(x4, y4); path.lineTo(x1, y1); path.close(); // Rotate arrow around (rotationX, rotationY) point Matrix matrix = new Matrix(); matrix.postRotate(directionAngle, rotationX, rotationY); path.transform(matrix); c.drawPath(path, arrowPaintFill); c.drawPath(path, mArrowPaintStroke); return bm; }
From source file:org.mariotaku.twidere.util.Utils.java
public static Bitmap createAlphaGradientBanner(final Bitmap orig) { if (orig == null) return null; final int width = orig.getWidth(), height = orig.getHeight(); final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); final Paint paint = new Paint(); final LinearGradient shader = new LinearGradient(width / 2, 0, width / 2, height, 0xffffffff, 0x00ffffff, Shader.TileMode.CLAMP);//from w w w . j a va2s . c om paint.setShader(shader); paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); canvas.drawBitmap(orig, 0, 0, null); canvas.drawRect(0, 0, width, height, paint); return bitmap; }
From source file:com.android.mail.ui.FolderDisplayer.java
public static void drawFolder(Canvas canvas, float x, float y, int width, int height, String name, int fgColor, int bgColor, FolderDisplayer.FolderDrawableResources res, BidiFormatter formatter, Paint paint) { canvas.save();/* w ww .j a va 2 s . com*/ canvas.translate(x, y + res.folderVerticalOffset); // Draw the box. paint.setColor(bgColor); paint.setStyle(Paint.Style.FILL); final RectF rect = new RectF(0, 0, width, height); canvas.drawRoundRect(rect, res.folderRoundedCornerRadius, res.folderRoundedCornerRadius, paint); // Draw the text based on the language locale and layout direction. paint.setColor(fgColor); paint.setStyle(Paint.Style.FILL); // Compute the text/gradient indices final int textLength = (int) paint.measureText(name); final int gradientX0; final int gradientX1; final int textX; /*************************************************************************************************** * width - the actual folder chip rectangle. * * textLength - the length of the folder's full name (can be longer than * * the actual chip, which is what overflow gradient is for). * * innerPadding - the padding between the text and the chip edge. * * overflowPadding - the padding between start of overflow and the chip edge. * * * * * * text is in a RTL language * * * * index-0 * * |<---------------------------- width ---------------------------->| * * |<-------------------------textLength------------------>| | * * | |<----- overflowPadding ----->| | * * | |<- innerPadding ->|<-------->|<--------->|<- horizontalPadding ->| * * textX gX1 gX0 * * * * * * text is in a LTR language. * * * * index-0 * * |<------------------------------ width ------------------------------->| * * | |<-------------------------textLength-------------------->| * * | |<-------- overflowPadding ------->| * * |<- horizontalPadding ->|<--------->|<-------->|<- horizontalPadding ->| * * textX gX0 gX1 * * * **************************************************************************************************/ if (formatter.isRtl(name)) { gradientX0 = res.overflowGradientPadding; gradientX1 = res.folderHorizontalPadding; textX = width - res.folderHorizontalPadding - textLength; } else { gradientX0 = width - res.overflowGradientPadding; gradientX1 = width - res.folderHorizontalPadding; textX = res.folderHorizontalPadding; } // Draw the text and the possible overflow gradient // Overflow happens when the text is longer than the chip width minus side paddings. if (textLength > width - 2 * res.folderHorizontalPadding) { final Shader shader = new LinearGradient(gradientX0, 0, gradientX1, 0, fgColor, Utils.getTransparentColor(fgColor), Shader.TileMode.CLAMP); paint.setShader(shader); } final int textY = height / 2 - (int) (paint.descent() + paint.ascent()) / 2; canvas.drawText(name, textX, textY, paint); paint.setShader(null); canvas.restore(); }