Example usage for android.graphics Canvas drawBitmap

List of usage examples for android.graphics Canvas drawBitmap

Introduction

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

Prototype

public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst, @Nullable Paint paint) 

Source Link

Document

Draw the specified bitmap, scaling/translating automatically to fill the destination rectangle.

Usage

From source file:com.byagowi.persiancalendar.view.QiblaCompassView.java

public void drawQibla(Canvas canvas) {

    canvas.rotate((float) qiblaInfo.getHeading() - 360, px, py);
    qiblaPaint.reset();/*from   w w  w. j  a v a 2s. co m*/
    qiblaPaint.setColor(Color.GREEN);
    qiblaPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    qiblaPaint.setPathEffect(dashPath);
    qiblaPaint.setStrokeWidth(5.5f);

    canvas.drawLine(px, py - Radius, px, py + Radius, qiblaPaint);
    qiblaPaint.setPathEffect(null);
    canvas.drawBitmap(kaaba, px - kaaba.getWidth() / 2, py - Radius - kaaba.getHeight() / 2, qiblaPaint);
    canvas.restore();

}

From source file:com.android.leanlauncher.IconCache.java

public Bitmap createIconBitmapFromTheme(String iconDrawableName, Drawable defaultDrawable) {
    Bitmap icon = loadBitmapFromIconPack(iconDrawableName);

    if (icon == null) {
        Log.d(TAG, "Using default icon, can't find icon drawable: " + iconDrawableName + " in "
                + mCurrentIconTheme);//w w w  .  ja  v  a 2  s.  c o m
        icon = ((BitmapDrawable) defaultDrawable).getBitmap();
    }

    if (mIconBackgrounds.size() < 1) {
        // we are done
        return icon;
    } else {
        Random r = new Random();
        int backImageInd = r.nextInt(mIconBackgrounds.size());
        Bitmap background = mIconBackgrounds.get(backImageInd);
        if (background == null) {
            Log.d(TAG, "Can't load background image: " + mIconBackgrounds.get(backImageInd));
            return icon;
        }

        int w = background.getWidth(), h = background.getHeight();
        Bitmap result = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        final Canvas tempCanvas = new Canvas(result);

        // draw the background first
        tempCanvas.drawBitmap(background, 0, 0, null);

        // create a mutable mask bitmap with the same mask
        if (icon.getWidth() > w || icon.getHeight() > h) {
            mIconScaleFactor = (mIconScaleFactor == 0) ? 1 : mIconScaleFactor;
            icon = Bitmap.createScaledBitmap(icon, (int) (w * mIconScaleFactor), (int) (h * mIconScaleFactor),
                    false);
        }

        if (mIconMask != null) {
            renderIconBackground(icon, mIconMask, tempCanvas, w, h, true);
        } else {
            renderIconBackground(icon, background, tempCanvas, w, h, false);
        }

        // paint the front
        if (mIconFront != null) {
            tempCanvas.drawBitmap(mIconFront, 0, 0, null);
        }

        return result;
    }
}

From source file:com.abhinavjhanwar.android.egg.neko.Cat.java

@Override
public void draw(@NonNull Canvas canvas) {
    final int hw = Math.min(canvas.getWidth(), canvas.getHeight());

    if (mBitmap == null || mBitmap.getWidth() != hw || mBitmap.getHeight() != hw) {
        mBitmap = Bitmap.createBitmap(hw, hw, Bitmap.Config.ARGB_8888);
        final Canvas bitCanvas = new Canvas(mBitmap);
        slowDraw(bitCanvas, 0, 0, hw, hw);
    }/*  w w w .  java 2s  .c om*/
    canvas.drawBitmap(mBitmap, 0, 0, null);
}

From source file:com.clov4r.moboplayer.android.nil.codec.activity.MoboThumbnailTestActivity.java

public Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
    try {//from  w ww  .  ja v a 2s.  co m
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()));
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.BLACK);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        final Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

        canvas.drawBitmap(bitmap, src, rect, paint);
        return output;
    } catch (Exception e) {
        return bitmap;
    }
}

From source file:com.example.hudpassthrough.BluetoothChat.java

private Bitmap toGrayscale(Bitmap bmpOriginal) {
    int width, height;
    height = bmpOriginal.getHeight();//from w w w.j a  va  2s  . c om
    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.concavenp.artistrymuse.BaseAppCompatActivity.java

private BitmapImageViewTarget createBitmapImageViewTarget(final ImageView imageView) {

    return new BitmapImageViewTarget(imageView) {
        @Override//  w ww .j  av a  2 s.co m
        protected void setResource(Bitmap bitmap) {

            int bitmapWidth = bitmap.getWidth();
            int bitmapHeight = bitmap.getHeight();
            int borderWidthHalf = 10;
            int bitmapSquareWidth = Math.min(bitmapWidth, bitmapHeight);
            int newBitmapSquare = bitmapSquareWidth + borderWidthHalf;

            Bitmap roundedBitmap = Bitmap.createBitmap(newBitmapSquare, newBitmapSquare,
                    Bitmap.Config.ARGB_8888);

            // Initialize a new Canvas to draw empty bitmap
            Canvas canvas = new Canvas(roundedBitmap);

            // Calculation to draw bitmap at the circular bitmap center position
            int x = borderWidthHalf + bitmapSquareWidth - bitmapWidth;
            int y = borderWidthHalf + bitmapSquareWidth - bitmapHeight;

            canvas.drawBitmap(bitmap, x, y, null);

            // Initializing a new Paint instance to draw circular border
            Paint borderPaint = new Paint();
            borderPaint.setStyle(Paint.Style.STROKE);
            borderPaint.setStrokeWidth(borderWidthHalf * 2);
            borderPaint.setColor(ResourcesCompat.getColor(getResources(), R.color.myApp_accent_700, null));

            canvas.drawCircle(canvas.getWidth() / 2, canvas.getWidth() / 2, newBitmapSquare / 2, borderPaint);

            RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(),
                    roundedBitmap);
            circularBitmapDrawable.setCircular(true);
            imageView.setImageDrawable(circularBitmapDrawable);

        }
    };
}

From source file:com.flyingcrop.ScreenCaptureFragment.java

void savePlanes() {

    Image.Plane[] planes = image.getPlanes();
    Buffer imageBuffer = planes[0].getBuffer().rewind();
    //Log.i(TAG, "Time 2 " + (System.currentTimeMillis() - time_now));

    // create bitmap
    bitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
    bitmap.copyPixelsFromBuffer(imageBuffer);

    int offset = 0;
    int pixelStride = planes[0].getPixelStride();
    int rowStride = planes[0].getRowStride();
    int rowPadding = rowStride - pixelStride * mWidth;
    ByteBuffer buffer = planes[0].getBuffer();
    //Log.e(TAG, "Time 3 " + (System.currentTimeMillis() - time_now));
    for (int i = 0; i < mHeight; ++i) {
        for (int j = 0; j < mWidth; ++j) {
            int pixel = 0;
            pixel |= (buffer.get(offset) & 0xff) << 16; // R
            pixel |= (buffer.get(offset + 1) & 0xff) << 8; // G
            pixel |= (buffer.get(offset + 2) & 0xff); // B
            pixel |= (buffer.get(offset + 3) & 0xff) << 24; // A
            bitmap.setPixel(j, i, pixel);
            offset += pixelStride;//from   w ww .j  a va2  s.co  m
        }
        offset += rowPadding;
    }
    //Log.i(TAG, "Time 4 " + (System.currentTimeMillis() - time_now));

    Log.i(TAG, "x_inicial " + x_inicial);
    Log.i(TAG, "x_final " + x_final);
    Log.i(TAG, "y_inicial " + y_inicial);
    Log.i(TAG, "y_final " + y_final);

    bitmap = Bitmap.createBitmap(bitmap, (int) x_inicial, (int) status_bar_height + (int) y_inicial,
            Math.abs((int) x_final - (int) x_inicial), Math.abs((int) y_final - (int) y_inicial));
    //bitmap = Bitmap.createBitmap(bitmap, 0 ,0,mWidth, mHeight);
    // write bitmap to a file
    SharedPreferences settings = getActivity().getSharedPreferences("data", 0);

    if (!settings.getBoolean("watermark", false)) {
        Canvas mCanvas = new Canvas(bitmap);

        Bitmap watermark = resize(
                BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.watermark));

        mCanvas.drawBitmap(watermark, 0, 0, null);

    }

    String date = getDate();
    String dir = STORE_DIRECTORY + "/FlyingCrop/" + date + ".png";
    try {
        fos = new FileOutputStream(dir);
    } catch (Exception e) {

    }

    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
    //Log.e(TAG, "Time 5 " + (System.currentTimeMillis() - time_now));
    File file = new File(dir);
    // MediaStore.Images.Media.insertImage(getActivity().getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());

    if (settings.getBoolean("toast", true)) {
        Toast.makeText(getActivity(), getResources().getString(R.string.fragment_img_saved) + " " + dir,
                Toast.LENGTH_SHORT).show();
    }

    Intent mIntent = new Intent(getActivity(), Brush.class);
    getActivity().stopService(mIntent);

    notifySS(bitmap, date, dir);

    MediaScannerConnection.scanFile(getActivity(), new String[] { dir }, null, null);

    mImageReader = null;
    getActivity().finish();
}

From source file:com.dgnt.dominionCardPicker.view.DynamicListView.java

/** Draws a black border over the screenshot of the view passed in. */
private Bitmap getBitmapWithBorder(View v) {
    Bitmap bitmap = getBitmapFromView(v);
    Canvas can = new Canvas(bitmap);

    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(LINE_THICKNESS);
    paint.setColor(ContextCompat.getColor(getContext(), R.color.colorAccent));

    can.drawBitmap(bitmap, 0, 0, null);
    can.drawRect(rect, paint);//w w  w. j a  v  a 2 s  . c o  m

    return bitmap;
}

From source file:com.android.andryyu.lifehelper.widget.RippleView.java

private Bitmap getCircleBitmap(final int radius) {
    final Bitmap output = Bitmap.createBitmap(originBitmap.getWidth(), originBitmap.getHeight(),
            Bitmap.Config.ARGB_8888);/*w  w w.j a v a 2s  .c om*/
    final Canvas canvas = new Canvas(output);
    final Paint paint = new Paint();
    final Rect rect = new Rect((int) (x - radius), (int) (y - radius), (int) (x + radius), (int) (y + radius));

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawCircle(x, y, radius, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(originBitmap, rect, rect, paint);

    return output;
}

From source file:com.example.ysh.myapplication.view.readview.PageFactory.java

/**
 * ?//from  w  ww. j a v  a  2  s .  c o m
 *
 * @param canvas
 */
public synchronized void onDraw(Canvas canvas) {
    if (mLines.size() == 0) {
        curEndPos = curBeginPos;
        mLines = pageDown();
    }
    if (mLines.size() > 0) {
        int y = marginHeight + (mLineSpace << 1);
        // 
        if (mBookPageBg != null) {
            canvas.drawBitmap(mBookPageBg, null, rectF, null);
        } else {
            canvas.drawColor(Color.WHITE);
        }
        // 
        canvas.drawText(chaptersList.get(currentChapter - 1).title, marginWidth, y, mTitlePaint);
        y += mLineSpace + mNumFontSize;
        // ?
        for (String line : mLines) {
            y += mLineSpace;
            if (line.endsWith("@")) {
                canvas.drawText(line.substring(0, line.length() - 1), marginWidth, y, mPaint);
                y += mLineSpace;
            } else {
                canvas.drawText(line, marginWidth, y, mPaint);
            }
            y += mFontSize;
        }
        // ??
        if (batteryBitmap != null) {
            canvas.drawBitmap(batteryBitmap, marginWidth + 2,
                    mHeight - marginHeight - ScreenUtils.dpToPxInt(12), mTitlePaint);
        }

        float percent = (float) currentChapter * 100 / chapterSize;
        canvas.drawText(decimalFormat.format(percent) + "%", (mWidth - percentLen) / 2, mHeight - marginHeight,
                mTitlePaint);

        String mTime = dateFormat.format(new Date());
        canvas.drawText(mTime, mWidth - marginWidth - timeLen, mHeight - marginHeight, mTitlePaint);

        // ?
        SettingManager.getInstance().saveReadProgress(bookId, currentChapter, curBeginPos, curEndPos);
    }
}