List of usage examples for android.graphics Paint Paint
public Paint()
From source file:org.cryptsecure.Utility.java
/** * Cut text into one line./*w w w . ja v a2 s .c o m*/ * * @param text * the text * @param maxWidth * the max width * @param textSize * the text size * @return the string */ public static String cutTextIntoOneLine(String text, int maxWidth, int textSize) { try { Paint paint = new Paint(); Rect bounds = new Rect(); int textWidth = 0; paint.setTypeface(Typeface.DEFAULT);// your preference here paint.setTextSize(textSize);// have this the same as your text size String outText = text; String outText2 = text; boolean modified = false; boolean cutDown = false; while (true) { if (modified) { paint.getTextBounds(outText2, 0, outText2.length(), bounds); } else { paint.getTextBounds(outText, 0, outText.length(), bounds); } textWidth = bounds.width(); if (textWidth <= maxWidth) { break; } else { modified = true; if (!cutDown) { cutDown = true; int estimatedLen = (outText.length() * maxWidth) / textWidth; estimatedLen += 20; // be carefull! if (estimatedLen > outText.length()) { estimatedLen = outText.length(); } outText = outText.substring(0, estimatedLen); outText2 = outText + "..."; } else { // reduce by one character outText = outText.substring(0, outText.length() - 1); outText2 = outText + "..."; } } } if (modified) { return outText2; } else { return outText; } } catch (Exception e) { e.printStackTrace(); } return ""; }
From source file:com.dwdesign.tweetings.util.Utils.java
public static Bitmap getColorPreviewBitmap(final Context context, final int color) { if (context == null) return null; final float density = context.getResources().getDisplayMetrics().density; final int width = (int) (32 * density), height = (int) (32 * density); final Bitmap bm = Bitmap.createBitmap(width, height, Config.ARGB_8888); final Canvas canvas = new Canvas(bm); final int rectrangle_size = (int) (density * 5); final int numRectanglesHorizontal = (int) Math.ceil(width / rectrangle_size); final int numRectanglesVertical = (int) Math.ceil(height / rectrangle_size); final Rect r = new Rect(); boolean verticalStartWhite = true; for (int i = 0; i <= numRectanglesVertical; i++) { boolean isWhite = verticalStartWhite; for (int j = 0; j <= numRectanglesHorizontal; j++) { r.top = i * rectrangle_size; r.left = j * rectrangle_size; r.bottom = r.top + rectrangle_size; r.right = r.left + rectrangle_size; final Paint paint = new Paint(); paint.setColor(isWhite ? Color.WHITE : Color.GRAY); canvas.drawRect(r, paint);/*from w w w. ja va2s .co m*/ isWhite = !isWhite; } verticalStartWhite = !verticalStartWhite; } canvas.drawColor(color); final Paint paint = new Paint(); paint.setColor(Color.WHITE); paint.setStrokeWidth(2.0f); final float[] points = new float[] { 0, 0, width, 0, 0, 0, 0, height, width, 0, width, height, 0, height, width, height }; canvas.drawLines(points, paint); return bm; }
From source file:com.initialxy.cordova.themeablebrowser.ThemeableBrowser.java
private void setButtonImages(View view, BrowserButton buttonProps, int disabledAlpha) { Drawable normalDrawable = null;//from w ww . java 2s .co m Drawable disabledDrawable = null; Drawable pressedDrawable = null; CharSequence description = view.getContentDescription(); if (buttonProps.image != null || buttonProps.wwwImage != null) { try { normalDrawable = getImage(buttonProps.image, buttonProps.wwwImage, buttonProps.wwwImageDensity); ViewGroup.LayoutParams params = view.getLayoutParams(); params.width = normalDrawable.getIntrinsicWidth(); params.height = normalDrawable.getIntrinsicHeight(); } catch (Resources.NotFoundException e) { emitError(ERR_LOADFAIL, String.format("Image for %s, %s, failed to load", description, buttonProps.image)); } catch (IOException ioe) { emitError(ERR_LOADFAIL, String.format("Image for %s, %s, failed to load", description, buttonProps.wwwImage)); } } else { emitWarning(WRN_UNDEFINED, String.format("Image for %s is not defined. Button will not be shown", description)); } if (buttonProps.imagePressed != null || buttonProps.wwwImagePressed != null) { try { pressedDrawable = getImage(buttonProps.imagePressed, buttonProps.wwwImagePressed, buttonProps.wwwImageDensity); } catch (Resources.NotFoundException e) { emitError(ERR_LOADFAIL, String.format("Pressed image for %s, %s, failed to load", description, buttonProps.imagePressed)); } catch (IOException e) { emitError(ERR_LOADFAIL, String.format("Pressed image for %s, %s, failed to load", description, buttonProps.wwwImagePressed)); } } else { emitWarning(WRN_UNDEFINED, String.format("Pressed image for %s is not defined.", description)); } if (normalDrawable != null) { // Create the disabled state drawable by fading the normal state // drawable. Drawable.setAlpha() stopped working above Android 4.4 // so we gotta bring out some bitmap magic. Credit goes to: // http://stackoverflow.com/a/7477572 Bitmap enabledBitmap = ((BitmapDrawable) normalDrawable).getBitmap(); Bitmap disabledBitmap = Bitmap.createBitmap(normalDrawable.getIntrinsicWidth(), normalDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(disabledBitmap); Paint paint = new Paint(); paint.setAlpha(disabledAlpha); canvas.drawBitmap(enabledBitmap, 0, 0, paint); Resources activityRes = cordova.getActivity().getResources(); disabledDrawable = new BitmapDrawable(activityRes, disabledBitmap); } StateListDrawable states = new StateListDrawable(); if (pressedDrawable != null) { states.addState(new int[] { android.R.attr.state_pressed }, pressedDrawable); } if (normalDrawable != null) { states.addState(new int[] { android.R.attr.state_enabled }, normalDrawable); } if (disabledDrawable != null) { states.addState(new int[] {}, disabledDrawable); } setBackground(view, states); }
From source file:android.support.design.widget.CoordinatorLayout.java
@Override protected boolean drawChild(Canvas canvas, View child, long drawingTime) { final LayoutParams lp = (LayoutParams) child.getLayoutParams(); if (lp.mBehavior != null) { final float scrimAlpha = lp.mBehavior.getScrimOpacity(this, child); if (scrimAlpha > 0f) { if (mScrimPaint == null) { mScrimPaint = new Paint(); }/* w ww .jav a2 s . c o m*/ mScrimPaint.setColor(lp.mBehavior.getScrimColor(this, child)); mScrimPaint.setAlpha(MathUtils.constrain(Math.round(255 * scrimAlpha), 0, 255)); final int saved = canvas.save(); if (child.isOpaque()) { // If the child is opaque, there is no need to draw behind it so we'll inverse // clip the canvas canvas.clipRect(child.getLeft(), child.getTop(), child.getRight(), child.getBottom(), Region.Op.DIFFERENCE); } // Now draw the rectangle for the scrim canvas.drawRect(getPaddingLeft(), getPaddingTop(), getWidth() - getPaddingRight(), getHeight() - getPaddingBottom(), mScrimPaint); canvas.restoreToCount(saved); } } return super.drawChild(canvas, child, drawingTime); }
From source file:com.FluksoViz.FluksoVizActivity.java
private void make_graph_pretty(XYPlot p) { p.setDomainStep(XYStepMode.INCREMENT_BY_VAL, 56); // Reduce the number // of range labels // Plot1.setTicksPerDomainLabel(1); p.setDomainValueFormat(new DateFormat_p1()); p.setRangeStep(XYStepMode.SUBDIVIDE, 5);// Skala Y pionowa // Plot1.setRangeStep(XYStepMode.INCREMENT_BY_VAL, 1); // Plot1.setTicksPerRangeLabel(1); p.getTitleWidget().setClippingEnabled(false); p.getTitleWidget().pack();//from w ww. jav a2 s .c om int axis_font_size = 15; int title_font_size = 15; int domain_font_size = 12; if (screen_width == 320) { axis_font_size = 12; title_font_size = 9; domain_font_size = 10; } p.getTitleWidget().getLabelPaint().setTextSize(title_font_size); p.getGraphWidget().getDomainLabelPaint().setTextSize(domain_font_size); p.getGraphWidget().getDomainLabelPaint().setColor(Color.WHITE); p.getGraphWidget().getRangeLabelPaint().setColor(Color.WHITE); p.getGraphWidget().getRangeLabelPaint().setTextSize(axis_font_size); p.getGraphWidget().getDomainOriginLabelPaint().setTextSize(domain_font_size); p.getGraphWidget().getRangeOriginLabelPaint().setTextSize(axis_font_size); p.getGraphWidget().setClippingEnabled(false); p.setDomainValueFormat(new DecimalFormat("#")); p.getLegendWidget().setVisible(false); p.getDomainLabelWidget().setVisible(false); p.getRangeLabelWidget().setVisible(false); p.getGraphWidget().getGridLinePaint().setPathEffect(new DashPathEffect(new float[] { 1, 2, 1, 2 }, 0)); p.getBackgroundPaint().setColor(Color.TRANSPARENT); p.getGraphWidget().getBackgroundPaint().setColor(Color.TRANSPARENT); p.getGraphWidget().getGridBackgroundPaint().setColor(Color.TRANSPARENT); p.setGridPadding(0, 10, 0, 0); // left top right bottom p.getGraphWidget().getGridLinePaint().setColor(Color.TRANSPARENT); if (sensor_number != 4) { p.setRangeLowerBoundary(0, BoundaryMode.GROW);// to ustawia } if (sensor_number == 4) { p.addMarker(new YValueMarker(0, "0", new XPositionMetric(-11, XLayoutStyle.ABSOLUTE_FROM_LEFT), Color.WHITE, Color.WHITE)); p.setRangeStep(XYStepMode.SUBDIVIDE, 2); p.getGraphWidget().getRangeOriginLinePaint().setAlpha(0); series1mFormat = new LineAndPointFormatter( // FAZA Color.rgb(0, 220, 0), // line color Color.rgb(0, 150, 0), // point color null); line1mFill.setShader( new LinearGradient(0, 0, 0, 200, Color.rgb(0, 200, 0), Color.BLACK, Shader.TileMode.MIRROR)); series1mFormat.getLinePaint().setStrokeWidth(4); series1mFormat.setFillPaint(line1mFill); series2mFormat = new LineAndPointFormatter( // faza 2 solar Color.rgb(200, 200, 0), // line Color.rgb(100, 100, 0), // point color null); line2mFill.setShader( new LinearGradient(0, 150, 0, 120, Color.rgb(250, 250, 0), Color.BLACK, Shader.TileMode.CLAMP)); series2mFormat.setFillDirection(FillDirection.TOP); series2mFormat.setFillPaint(line2mFill); series2mFormat.getLinePaint().setStrokeWidth(5); series3mFormat = new LineAndPointFormatter( // FAZA 3 formater Color.rgb(0, 220, 0), // line color Color.rgb(0, 150, 0), // point color null); line3mFill.setAlpha(255); line3mFill.setShader(new LinearGradient(0, 0, 0, 50, Color.BLACK, Color.BLACK, Shader.TileMode.MIRROR)); series3mFormat.getLinePaint().setStrokeWidth(7); series3mFormat.setFillPaint(line3mFill); series4mFormat = new LineAndPointFormatter(Color.rgb(0, 140, 220), // line Color.rgb(0, 120, 190), // point color null); line4mFill = new Paint(); line4mFill.setAlpha(190); line4mFill.setShader(new LinearGradient(0, 0, 0, 50, Color.BLACK, Color.BLACK, Shader.TileMode.MIRROR)); series4mFormat.getLinePaint().setStrokeWidth(5); series4mFormat.setFillPaint(line4mFill); series4mFormat.setFillDirection(FillDirection.TOP); // XYRegionFormatter region4Formatter = new // XYRegionFormatter(Color.BLUE); // series4mFormat.addRegion(new RectRegion(Double.NEGATIVE_INFINITY, // Double.POSITIVE_INFINITY, 0, -1000, "R1"), region4Formatter); } // p.setRangeLowerBoundary(0, BoundaryMode.GROW);// to ustawia // min i max // Plot1.setRangeUpperBoundary(11, BoundaryMode.FIXED); p.setRangeValueFormat(new DecimalFormat("#")); p.setBorderStyle(Plot.BorderStyle.SQUARE, null, null); p.setBorderPaint(null); p.disableAllMarkup(); // To get rid of them call disableAllMarkup(): }
From source file:es.javocsoft.android.lib.toolbox.ToolBox.java
/** * Convers a Bitmap to grayscale.//from w w w . j av a 2 s .c o m * * @param bmpOriginal * @return */ public static Bitmap media_getGrayScale(Bitmap bmpOriginal) { int width, height; height = bmpOriginal.getHeight(); width = bmpOriginal.getWidth(); Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); Canvas c = new Canvas(bmpGrayscale); Paint paint = new Paint(); ColorMatrix cm = new ColorMatrix(); cm.setSaturation(0); ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); paint.setColorFilter(f); c.drawBitmap(bmpOriginal, 0, 0, paint); return bmpGrayscale; }
From source file:com.skytree.epubtest.BookViewActivity.java
@SuppressLint({ "DrawAllocation", "DrawAllocation" }) @Override// w w w .j av a 2 s . co m protected void onDraw(Canvas canvas) { Paint paint = new Paint(); float sl, sr, st, sb; sl = 0; sr = this.getWidth(); float ah = this.arrowHeight; // arrow Height; if (this.isArrowDown) { st = 0; sb = this.getHeight() - ah; } else { st = ah - 10; sb = this.getHeight() - 10; } Path boxPath = new Path(); boxPath.addRoundRect(new RectF(sl, st, sr, sb), 20, 20, Path.Direction.CW); if (arrowPosition <= arrowHeight * 1.5f) { arrowPosition = arrowHeight * 1.5f; } else if (arrowPosition >= this.getWidth() - arrowHeight * 1.5f) { arrowPosition = this.getWidth() - arrowHeight * 1.5f; } Path arrowPath = new Path(); if (isArrowDown) { arrowPath.moveTo(arrowPosition, sb + ah); arrowPath.lineTo((float) (arrowPosition - ah * 0.75), sb - 10); arrowPath.lineTo((float) (arrowPosition + ah * 0.75), sb - 10); arrowPath.close(); } else { arrowPath.moveTo(arrowPosition, 0); arrowPath.lineTo((float) (arrowPosition - ah * 0.75), ah + 10); arrowPath.lineTo((float) (arrowPosition + ah * 0.75), ah + 10); arrowPath.close(); } paint.setColor(this.strokeColor); paint.setStyle(Paint.Style.FILL); boxPath.addPath(arrowPath); canvas.drawPath(boxPath, paint); paint.setColor(this.boxColor); paint.setStyle(Paint.Style.FILL); boxPath.addPath(arrowPath); canvas.save(); float sf = 0.995f; float ox = (this.getWidth() - (this.getWidth() * sf)) / 2.0f; float oy = ((this.getHeight() - arrowHeight) - ((this.getHeight() - arrowHeight) * sf)) / 2.0f; canvas.translate(ox, oy); canvas.scale(sf, sf); canvas.drawPath(boxPath, paint); canvas.restore(); if (layoutChanged) { this.recalcLayout(); layoutChanged = false; } }
From source file:com.skytree.epubtest.BookViewActivity.java
public DottedDrawable(int color) { mPaint = new Paint(); mPaint.setStrokeWidth(3);//from w ww . j a va 2 s .c om this.color = color; this.inactiveColor = color; this.value = 100; }
From source file:com.skytree.epubtest.BookViewActivity.java
public DottedDrawable(int activeColor, int inactiveColor, int value) { mPaint = new Paint(); mPaint.setStrokeWidth(3);//w w w . jav a 2 s . c om this.color = activeColor; this.inactiveColor = inactiveColor; this.value = value; }