Search in sources :

Example 81 with Bitmap

use of android.graphics.Bitmap in project KJFrameForAndroid by kymjs.

the class ImageUtils method getSmallImageFile.

/**
     * 压缩图片
     *
     * @param filePath 源图片地址
     * @param width    想要的宽度
     * @param height   想要的高度
     * @param isAdjust 是否自动调整尺寸, true图片就不会拉伸,false严格按照你的尺寸压缩
     * @return Bitmap
     */
public static File getSmallImageFile(Context cxt, String filePath, int width, int height, boolean isAdjust) {
    Bitmap bitmap = reduce(BitmapFactory.decodeFile(filePath), width, height, isAdjust);
    File file = new File(getRandomFileName(cxt.getCacheDir().getPath()));
    BufferedOutputStream outputStream = null;
    try {
        outputStream = new BufferedOutputStream(new FileOutputStream(file));
        bitmap.compress(Bitmap.CompressFormat.JPEG, 60, outputStream);
        outputStream.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return file;
}
Also used : Bitmap(android.graphics.Bitmap) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException)

Example 82 with Bitmap

use of android.graphics.Bitmap in project KJFrameForAndroid by kymjs.

the class RoundImageView method onDraw.

@Override
protected void onDraw(Canvas canvas) {
    initCurrentBitmap();
    if (currentBitmap == null || getWidth() == 0 || getHeight() == 0) {
        return;
    }
    this.measure(0, 0);
    int width = getWidth();
    int height = getHeight();
    // 半径
    int radius = 0;
    if (mBorderInsideColor != 0 && mBorderOutsideColor != 0) {
        // 画两个边框
        radius = (width < height ? width : height) / 2 - 2 * mBorderThickness;
        // 画内圆
        drawCircleBorder(canvas, radius + mBorderThickness / 2, width, height, mBorderInsideColor);
        // 画外圆
        drawCircleBorder(canvas, radius + mBorderThickness + mBorderThickness / 2, width, height, mBorderOutsideColor);
    } else if (mBorderInsideColor != 0 && mBorderOutsideColor == 0) {
        // 画一个边框
        radius = (width < height ? width : height) / 2 - mBorderThickness;
        drawCircleBorder(canvas, radius + mBorderThickness / 2, width, height, mBorderInsideColor);
    } else if (mBorderInsideColor == 0 && mBorderOutsideColor != 0) {
        // 画一个边框
        radius = (width < height ? width : height) / 2 - mBorderThickness;
        drawCircleBorder(canvas, radius + mBorderThickness / 2, width, height, mBorderOutsideColor);
    } else {
        // 没有边框
        radius = (width < height ? width : height) / 2;
    }
    Bitmap roundBitmap = getCroppedRoundBitmap(currentBitmap, radius);
    canvas.drawBitmap(roundBitmap, width / 2 - radius, height / 2 - radius, null);
}
Also used : Bitmap(android.graphics.Bitmap) Paint(android.graphics.Paint)

Example 83 with Bitmap

use of android.graphics.Bitmap in project KJFrameForAndroid by kymjs.

the class RoundImageView method initCurrentBitmap.

private void initCurrentBitmap() {
    Bitmap temp = null;
    Drawable drawable = getDrawable();
    if (drawable instanceof BitmapDrawable) {
        temp = ((BitmapDrawable) drawable).getBitmap();
    }
    if (temp != null) {
        currentBitmap = temp;
    }
}
Also used : Bitmap(android.graphics.Bitmap) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Drawable(android.graphics.drawable.Drawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable)

Example 84 with Bitmap

use of android.graphics.Bitmap in project QuickReturn by lawloretienne.

the class RoundedDrawable method drawableToBitmap.

public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }
    Bitmap bitmap;
    int width = Math.max(drawable.getIntrinsicWidth(), 1);
    int height = Math.max(drawable.getIntrinsicHeight(), 1);
    try {
        bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
    } catch (Exception e) {
        e.printStackTrace();
        bitmap = null;
    }
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Paint(android.graphics.Paint)

Example 85 with Bitmap

use of android.graphics.Bitmap in project TwinklingRefreshLayout by lcodecorex.

the class CircleImageView method getBitmapFromDrawable.

private Bitmap getBitmapFromDrawable(Drawable drawable) {
    if (drawable == null) {
        return null;
    }
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }
    try {
        Bitmap bitmap;
        if (drawable instanceof ColorDrawable) {
            bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
        } else {
            if (!(drawable.getIntrinsicWidth() > 0) || !(drawable.getIntrinsicHeight() > 0)) {
                bitmap = Bitmap.createBitmap(DensityUtil.dip2px(getContext(), 40), DensityUtil.dip2px(getContext(), 40), BITMAP_CONFIG);
            } else {
                bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG);
            }
        }
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    } catch (OutOfMemoryError e) {
        return null;
    }
}
Also used : Bitmap(android.graphics.Bitmap) ColorDrawable(android.graphics.drawable.ColorDrawable) Canvas(android.graphics.Canvas) BitmapDrawable(android.graphics.drawable.BitmapDrawable)

Aggregations

Bitmap (android.graphics.Bitmap)3746 Canvas (android.graphics.Canvas)889 Paint (android.graphics.Paint)709 BitmapDrawable (android.graphics.drawable.BitmapDrawable)461 IOException (java.io.IOException)394 Rect (android.graphics.Rect)341 File (java.io.File)262 Test (org.junit.Test)255 Matrix (android.graphics.Matrix)254 Drawable (android.graphics.drawable.Drawable)243 BitmapFactory (android.graphics.BitmapFactory)240 View (android.view.View)225 ImageView (android.widget.ImageView)210 Intent (android.content.Intent)187 FileOutputStream (java.io.FileOutputStream)186 InputStream (java.io.InputStream)171 RectF (android.graphics.RectF)150 FileNotFoundException (java.io.FileNotFoundException)150 Point (android.graphics.Point)148 Uri (android.net.Uri)128