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: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 .  ja  va  2  s . c  om*/
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) {
        if (imagePath != null) {
            Log.w("Helper", "Cannot decode " + imagePath);
        } else {
            Log.w("Helper", "Path is null: Cannot decode");
        }
        return null;
    }

    if (image.getWidth() != width || image.getHeight() != height) {
        //Image need to be resized.
        int scaledWidth = (image.getWidth() * height) / image.getHeight();
        int scaledHeight;
        if ((crop && scaledWidth > width) || (!crop && scaledWidth < width)) {
            scaledHeight = height;
        } else {
            scaledWidth = width;
            scaledHeight = (image.getHeight() * width) / image.getWidth();
        }

        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

private PaperPiecedBlock(Parcel in) {
    m_dirty = in.readInt() == 0 ? false : true;
    m_width = in.readFloat();//from w w w  .  j  a  v a 2 s.co m
    m_height = in.readFloat();
    m_backgroundColor = in.readInt();
    m_pieces = new ArrayList<PaperPiecedBlockPiece>();

    in.readTypedList(m_pieces, PaperPiecedBlockPiece.CREATOR);

    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:com.google.android.gms.samples.vision.face.facetracker.FaceGraphic.java

FaceGraphic(GraphicOverlay overlay) {
    super(overlay);

    mCurrentColorIndex = (mCurrentColorIndex + 1) % COLOR_CHOICES.length;
    final int selectedColor = COLOR_CHOICES[mCurrentColorIndex];

    mFacePositionPaint = new Paint();
    mFacePositionPaint.setColor(selectedColor);

    mIdPaint = new Paint();
    mIdPaint.setColor(selectedColor);//from w  w  w . j  a v  a 2 s .  c  o m
    mIdPaint.setTextSize(ID_TEXT_SIZE);

    mBoxPaint = new Paint();
    mBoxPaint.setColor(selectedColor);
    mBoxPaint.setStyle(Paint.Style.STROKE);
    mBoxPaint.setStrokeWidth(BOX_STROKE_WIDTH);
    audioPlayer("celebrate.mp3");
}

From source file:com.nextgis.maplib.display.SimpleMarkerStyle.java

protected void onDraw(GeoPoint pt, GISDisplay display) {
    if (null == pt)
        return;//from   ww  w .  j  a  v a  2 s  .com

    switch (mType) {
    case MarkerStylePoint:
        Paint ptPaint = new Paint();
        ptPaint.setColor(mColor);
        ptPaint.setStrokeWidth((float) (mSize / display.getScale()));
        ptPaint.setStrokeCap(Paint.Cap.ROUND);
        ptPaint.setAntiAlias(true);

        display.drawPoint((float) pt.getX(), (float) pt.getY(), ptPaint);
        break;
    case MarkerStyleCircle:
        Paint fillCirclePaint = new Paint();
        fillCirclePaint.setColor(mColor);
        fillCirclePaint.setStrokeCap(Paint.Cap.ROUND);

        display.drawCircle((float) pt.getX(), (float) pt.getY(), mSize, fillCirclePaint);

        Paint outCirclePaint = new Paint();
        outCirclePaint.setColor(mOutColor);
        outCirclePaint.setStrokeWidth((float) (mWidth / display.getScale()));
        outCirclePaint.setStyle(Paint.Style.STROKE);
        outCirclePaint.setAntiAlias(true);
        display.drawCircle((float) pt.getX(), (float) pt.getY(), mSize, outCirclePaint);

        break;
    case MarkerStyleDiamond:
        break;
    case MarkerStyleCross:
        break;
    case MarkerStyleTriangle:
        break;
    case MarkerStyleBox:
        break;
    }
}

From source file:foam.jellyfish.StarwispCanvas.java

public void DrawText(Canvas canvas, JSONArray prim, float sx, float sy) {
    try {/*from w  w  w . ja v a 2  s .c  o  m*/
        canvas.save();
        if (prim.getString(6).equals("vertical"))
            canvas.rotate(-90, prim.getInt(2) * sx, prim.getInt(3) * sy);

        Paint myPaint = new Paint();
        JSONArray c = prim.getJSONArray(4);
        myPaint.setColor(Color.rgb(c.getInt(0), c.getInt(1), c.getInt(2)));
        myPaint.setTextSize(prim.getInt(5));
        myPaint.setTypeface(m_Typeface);
        canvas.drawText(prim.getString(1), prim.getInt(2) * sx, prim.getInt(3) * sy, myPaint);
        canvas.restore();
    } catch (JSONException e) {
        Log.e("starwisp", "Error parsing data " + e.toString());
    }
}

From source file:jp.envision.android.cloudfingerpaint.CloudFingerPaint.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    myView = new MyView(this);
    setContentView(myView);/*from  w ww  .j  a  va  2  s  . c  o  m*/

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

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

    mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);

    alertDialogBuilder = new AlertDialog.Builder(this);
}

From source file:Main.java

public static Bitmap toRoundBitmap(Bitmap bitmap, int roundPx) {
    if (bitmap == null) {
        return null;
    }// ww  w.ja va2s . 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, 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(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, src, dst, paint);
    return output;
}

From source file:com.nextgis.maplib.display.TMSRenderer.java

public TMSRenderer(ILayer layer) {
    super(layer);
    mRasterPaint = new Paint();

    mAntiAlias = true;//www.  j  ava  2s.  c om
    mFilterBitmap = true;
    mDither = true;
    mContrast = 1;
    mBrightness = 0;
    mForceToGrayScale = false;

    mRasterPaint.setAntiAlias(mAntiAlias);
    mRasterPaint.setFilterBitmap(mFilterBitmap);
    mRasterPaint.setDither(mDither);

}

From source file:com.dragondevs.pubnubexample.DrawingActivity.java

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

    Intent intent = getIntent();//from ww  w .  ja  v  a  2  s.c o m
    channelName = intent.getStringExtra("CHANNEL");

    //Create Handler to update screen from PubnubClient
    mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message inputMessage) {
            if (inputMessage.obj instanceof JSONObject) {
                JSONObject json = (JSONObject) inputMessage.obj;
                int x = -1;
                int y = -1;
                int state = -1;
                try {
                    x = json.getInt("X");
                    y = json.getInt("Y");
                    state = json.getInt("STATE");
                } catch (JSONException e) {
                    Log.d(TAG, "Error: " + e.toString());
                }
                Log.d(TAG, "Drawing coord at x, y, state: " + x + " " + y + " " + state);
                if (x >= 0 && y >= 0 && state >= 0)
                    myView.drawLine(x, y, state);
                else
                    Log.d(TAG, "Json values read was not valid");
            }
        }
    };

    myView = new MyView(this, mHandler);

    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);

}

From source file:com.crs4.roodin.moduletester.CustomView.java

/**
 * @param context//from  w  w w. j  ava  2  s  .co m
 */
public CustomView(Context context) {
    super(context);
    blockImg = BitmapFactory.decodeResource(context.getResources(), R.drawable.block); //carichiamo l'immagine in una bitmap

    mBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_132); //carichiamo l'immagine in una bitmap
    bw = mBmp.getWidth(); //larghezza bitmap
    bh = mBmp.getHeight();//altezza   
    mPaint = new Paint(); // pennello
    mPaint.setColor(Color.CYAN);
    mPaint.setAntiAlias(true);
    mRnd = new Random();

}