Example usage for android.graphics Paint Paint

List of usage examples for android.graphics Paint Paint

Introduction

In this page you can find the example usage for android.graphics Paint Paint.

Prototype

public Paint() 

Source Link

Document

Create a new paint with default settings.

Usage

From source file:com.jjoe64.graphview.series.TitleLineGraphSeries.java

public TitleLineGraphSeries(E[] data) {
    super(data);/*w w w.  j a v  a  2s.  c o  m*/

    paint = new Paint();
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setStyle(Paint.Style.STROKE);

    paintTitle = new TextPaint();
    paintTitle.setTextAlign(Paint.Align.CENTER);

    paintBackground = new Paint();

    path = new Path();
    pathBackground = new Path();
}

From source file:Main.java

/**
 * Load the image at {@code imagePath} as a {@link Bitmap}, scaling it to
 * the specified size and preserving the aspect ratio.
 * @param imagePath Path of the image to load.
 * @param width Required width of the resulting {@link Bitmap}.
 * @param height Required height of the resulting {@link Bitmap}.
 * @param fill {@code true} to fill the empty space with transparent color.
 * @param crop {@code true} to crop the image, {@code false} to resize without cutting the image.
 * @return {@link Bitmap} representing the image at {@code imagePath}.
 *//*from   w w  w. j a v a2  s.  com*/
public static Bitmap loadResizedBitmap(String imagePath, int width, int height, boolean fill, boolean crop) {
    Bitmap retVal;

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = getScale(imagePath, width, height);
    opts.inJustDecodeBounds = false;

    Bitmap image = BitmapFactory.decodeFile(imagePath, opts);

    if (image == null) {
        return null;
    }

    if (image.getWidth() != width || image.getHeight() != height) {
        int scaledWidth = (image.getWidth() * height) / image.getHeight();
        int scaledHeight; // = (image.getHeight() * width) / image.getWidth();
        if ((crop && scaledWidth > width) || (!crop && scaledWidth < width)) {
            scaledHeight = height;
        } else {
            scaledWidth = width;
            scaledHeight = (image.getHeight() * width) / image.getWidth();
        }
        //image = Bitmap.createScaledBitmap(image, scaledWidth, scaledHeight, true);

        Rect src = new Rect(0, 0, image.getWidth(), image.getHeight());
        Rect dst = new Rect(0, 0, scaledWidth, scaledHeight);

        if (fill) {
            retVal = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            dst.offset((width - scaledWidth) / 2, (height - scaledHeight) / 2);
        } else {
            retVal = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
        }
        retVal.eraseColor(Color.TRANSPARENT);

        synchronized (canvas) {
            if (antiAliasPaint == null) {
                antiAliasPaint = new Paint();
                antiAliasPaint.setAntiAlias(true);
                antiAliasPaint.setFilterBitmap(true);
                antiAliasPaint.setDither(true);
            }
            canvas.setBitmap(retVal);
            canvas.drawBitmap(image, src, dst, antiAliasPaint);
        }

        image.recycle();
    } else {
        //No need to scale.
        retVal = image;
    }

    return retVal;
}

From source file:com.richtodd.android.quiltdesign.block.PaperPiecedBlock.java

public PaperPiecedBlock(float width, float height, int backgroundColor) {
    if (width < 0)
        throw new InvalidParameterException("Invalid width " + width);
    if (height < 0)
        throw new InvalidParameterException("Invalid height " + height);

    m_dirty = false;/*from   w  ww.  j  a  va2  s. c  o  m*/
    m_width = width;
    m_height = height;
    m_backgroundColor = backgroundColor;
    m_pieces = new ArrayList<PaperPiecedBlockPiece>();

    m_backgroundPaint = new Paint();
    m_backgroundPaint.setStyle(Style.FILL);
    m_backgroundPaint.setColor(m_backgroundColor);

    m_backgroundPaintWhite = new Paint();
    m_backgroundPaintWhite.setStyle(Style.FILL);
    m_backgroundPaintWhite.setColor(Color.WHITE);
}

From source file:Main.java

private static Paint getGrayScalePaint() {
    Paint paint = new Paint();
    paint.setAntiAlias(true);//from  ww w.  j a v a  2 s .c  om
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(colorMatrixColorFilter);
    return paint;
}

From source file:android.support.v7.graphics.drawable.VectorDrawableCompat.java

@Override
public void draw(Canvas canvas) {
    Paint paint = new Paint();
    for (Pair<Path, Integer> pair : mPaths) {
        Path path = pair.first;/*from   w ww . ja va 2  s.  com*/
        int color = pair.second;
        paint.setColor(color);
        canvas.drawPath(path, paint);
    }
}

From source file:com.example.android.apis.graphics.FingerPaint.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bitmap loadedBitmap = loadBitmap();// ww  w  .  j  a  v a 2  s  .  c o m
    myView = new MyView(this, loadedBitmap);
    setContentView(myView);

    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(0xFFFF0000);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(12);

    mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);

    mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
    setTitle(R.string.paint_text);

}

From source file:Main.java

public static Bitmap toRoundBitmap(Bitmap bitmap, int roundPx) {
    if (bitmap == null) {
        return null;
    }/*ww w . j av a2 s.c  o  m*/
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
    if (width <= height) {
        top = 0;
        bottom = width;
        left = 0;
        right = width;
        height = width;
        dst_left = 0;
        dst_top = 0;
        dst_right = width;
        dst_bottom = width;
    } else {
        float clip = (width - height) / 2;
        left = clip;
        right = width - clip;
        top = 0;
        bottom = height;
        width = height;
        dst_left = 0;
        dst_top = 0;
        dst_right = height;
        dst_bottom = height;
    }

    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
    final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
    final RectF rectF = new RectF(dst);
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, src, dst, paint);
    return output;
}

From source file:com.manning.androidhacks.hack007.view.Rectangle.java

public Rectangle(Context context, DrawView drawView) {
    super(context);
    mDrawView = drawView;// w  w w . j  ava2  s .  c o m

    mInnerPaint = new Paint();

    mDrawRect = new RectF();

    /* Red is default */
    mInnerPaint.setARGB(ALPHA, 255, 0, 0);
    mInnerPaint.setAntiAlias(true);
}

From source file:com.sebible.cordova.videosnapshot.VideoSnapshot.java

private void drawTimestamp(Bitmap bm, String prefix, long timeMs, int textSize) {
    float w = bm.getWidth(), h = bm.getHeight();
    float size = (float) (textSize * bm.getWidth()) / 1280;
    float margin = (float) (w < h ? w : h) * 0.05f;

    Canvas c = new Canvas(bm);
    Paint p = new Paint();
    p.setColor(Color.WHITE);/*from   w  w  w.j  av a  2s .  c om*/
    p.setStrokeWidth((int) (size / 10));
    p.setTextSize((int) size); // Text Size
    p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text Overlapping Pattern

    long second = (timeMs / 1000) % 60;
    long minute = (timeMs / (1000 * 60)) % 60;
    long hour = (timeMs / (1000 * 60 * 60)) % 24;

    String text = String.format("%s %02d:%02d:%02d", prefix, hour, minute, second);
    Rect r = new Rect();
    p.getTextBounds(text, 0, text.length(), r);
    //c.drawBitmap(originalBitmap, 0, 0, paint);
    c.drawText(text, bm.getWidth() - r.width() - margin, bm.getHeight() - r.height() - margin, p);
}

From source file:de.treichels.hott.ui.android.html.AndroidCurveImageGenerator.java

private Bitmap getBitmap(final Curve curve, final float scale, final boolean description) {
    final boolean pitchCurve = curve.getPoint()[0].getPosition() == 0;
    final float scale1 = scale * 0.75f; // smaller images on the android
    // platform//  ww w  .  ja  va  2 s  .  com

    final Bitmap image = Bitmap.createBitmap((int) (10 + 200 * scale1), (int) (10 + 250 * scale1),
            Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(image);

    final Paint backgroundPaint = new Paint();
    backgroundPaint.setColor(Color.WHITE);
    backgroundPaint.setStyle(Style.FILL);

    final Paint forgroundPaint = new Paint();
    forgroundPaint.setColor(Color.BLACK);
    forgroundPaint.setStyle(Style.STROKE);
    forgroundPaint.setStrokeWidth(1.0f);
    forgroundPaint.setStrokeCap(Cap.BUTT);
    forgroundPaint.setStrokeJoin(Join.ROUND);
    forgroundPaint.setStrokeMiter(0.0f);

    final Paint curvePaint = new Paint(forgroundPaint);
    curvePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    curvePaint.setStrokeWidth(2.0f);

    final Paint pointPaint = new Paint(curvePaint);
    pointPaint.setStrokeWidth(5.0f);
    pointPaint.setStyle(Style.FILL_AND_STROKE);

    final Paint helpLinePaint = new Paint(forgroundPaint);
    helpLinePaint.setColor(Color.GRAY);
    helpLinePaint.setPathEffect(new DashPathEffect(new float[] { 5.0f, 5.0f }, 2.5f));

    final Paint textPaint = new Paint(forgroundPaint);
    textPaint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD));
    textPaint.setTextSize(12.0f);
    textPaint.setTextAlign(Align.CENTER);
    textPaint.setStyle(Style.FILL);

    canvas.drawRect(0, 0, 10 + 200 * scale1, 10 + 250 * scale1, backgroundPaint);
    canvas.drawRect(5, 5, 5 + 200 * scale1, 5 + 250 * scale1, forgroundPaint);

    canvas.drawLine(5, 5 + 25 * scale1, 5 + 200 * scale1, 5 + 25 * scale1, helpLinePaint);
    canvas.drawLine(5, 5 + 225 * scale1, 5 + 200 * scale1, 5 + 225 * scale1, helpLinePaint);
    if (!pitchCurve) {
        canvas.drawLine(5, 5 + 125 * scale1, 5 + 200 * scale1, 5 + 125 * scale1, helpLinePaint);
        canvas.drawLine(5 + 100 * scale1, 5, 5 + 100 * scale1, 5 + 250 * scale1, helpLinePaint);
    }

    if (curve.getPoint() != null) {
        int numPoints = 0;
        for (final CurvePoint p : curve.getPoint()) {
            if (p.isEnabled()) {
                numPoints++;
            }
        }

        final double[] xVals = new double[numPoints];
        final double[] yVals = new double[numPoints];

        int i = 0;
        for (final CurvePoint p : curve.getPoint()) {
            if (p.isEnabled()) {
                if (i == 0) {
                    xVals[i] = pitchCurve ? 0 : -100;
                } else if (i == numPoints - 1) {
                    xVals[i] = 100;
                } else {
                    xVals[i] = p.getPosition();
                }
                yVals[i] = p.getValue();

                if (description) {
                    float x0;
                    float y0;
                    if (pitchCurve) {
                        x0 = (float) (5 + xVals[i] * 2 * scale1);
                        y0 = (float) (5 + (225 - yVals[i] * 2) * scale1);
                    } else {
                        x0 = (float) (5 + (100 + xVals[i]) * scale1);
                        y0 = (float) (5 + (125 - yVals[i]) * scale1);
                    }

                    canvas.drawPoint(x0, y0, pointPaint);
                    if (y0 < 5 + 125 * scale1) {
                        canvas.drawRect(x0 - 4, y0 + 5, x0 + 3, y0 + 18, backgroundPaint);
                        canvas.drawText(Integer.toString(p.getNumber() + 1), x0 - 1, y0 + 16, textPaint);
                    } else {
                        canvas.drawRect(x0 - 4, y0 - 5, x0 + 3, y0 - 18, backgroundPaint);
                        canvas.drawText(Integer.toString(p.getNumber() + 1), x0 - 1, y0 - 7, textPaint);
                    }
                }

                i++;
            }
        }

        if (numPoints > 2 && curve.isSmoothing()) {
            final SplineInterpolator s = new SplineInterpolator();
            final PolynomialSplineFunction function = s.interpolate(xVals, yVals);

            float x0 = 5;
            float y0;
            if (pitchCurve) {
                y0 = (float) (5 + (225 - yVals[0] * 2) * scale1);
            } else {
                y0 = (float) (5 + (125 - yVals[0]) * scale1);
            }

            while (x0 < 4 + 200 * scale1) {
                final float x1 = x0 + 1;
                float y1;
                if (pitchCurve) {
                    y1 = (float) (5 + (225 - function.value((x1 - 5) / scale1 / 2) * 2) * scale1);
                } else {
                    y1 = (float) (5 + (125 - function.value((x1 - 5) / scale1 - 100)) * scale1);
                }

                canvas.drawLine(x0, y0, x1, y1, curvePaint);

                x0 = x1;
                y0 = y1;
            }
        } else {
            for (i = 0; i < numPoints - 1; i++) {
                float x0, y0, x1, y1;

                if (pitchCurve) {
                    x0 = (float) (5 + xVals[i] * 2 * scale1);
                    y0 = (float) (5 + (225 - yVals[i] * 2) * scale1);

                    x1 = (float) (5 + xVals[i + 1] * 2 * scale1);
                    y1 = (float) (5 + (225 - yVals[i + 1] * 2) * scale1);
                } else {
                    x0 = (float) (5 + (100 + xVals[i]) * scale1);
                    y0 = (float) (5 + (125 - yVals[i]) * scale1);

                    x1 = (float) (5 + (100 + xVals[i + 1]) * scale1);
                    y1 = (float) (5 + (125 - yVals[i + 1]) * scale1);
                }

                canvas.drawLine(x0, y0, x1, y1, curvePaint);
            }
        }
    }

    return image;
}