本文整理汇总了Java中android.graphics.Bitmap.getConfig方法的典型用法代码示例。如果您正苦于以下问题:Java Bitmap.getConfig方法的具体用法?Java Bitmap.getConfig怎么用?Java Bitmap.getConfig使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.graphics.Bitmap
的用法示例。
在下文中一共展示了Bitmap.getConfig方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getBitmapByteSize
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* Returns the in memory size of the given {@link Bitmap} in bytes.
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public static int getBitmapByteSize(Bitmap bitmap) {
// The return value of getAllocationByteCount silently changes for recycled bitmaps from the
// internal buffer size to row bytes * height. To avoid random inconsistencies in caches, we
// instead assert here.
if (bitmap.isRecycled()) {
throw new IllegalStateException("Cannot obtain size for recycled Bitmap: " + bitmap
+ "[" + bitmap.getWidth() + "x" + bitmap.getHeight() + "] " + bitmap.getConfig());
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// Workaround for KitKat initial release NPE in Bitmap, fixed in MR1. See issue #148.
try {
return bitmap.getAllocationByteCount();
} catch (NullPointerException e) {
// Do nothing.
}
}
return bitmap.getHeight() * bitmap.getRowBytes();
}
示例2: transform
import android.graphics.Bitmap; //导入方法依赖的package包/类
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
Bitmap source = resource.get();
int size = Math.min(source.getWidth(), source.getHeight());
mWidth = (source.getWidth() - size) / 2;
mHeight = (source.getHeight() - size) / 2;
Bitmap.Config config =
source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
Bitmap bitmap = mBitmapPool.get(mWidth, mHeight, config);
if (bitmap == null) {
bitmap = Bitmap.createBitmap(source, mWidth, mHeight, size, size);
}
return BitmapResource.obtain(bitmap, mBitmapPool);
}
示例3: countBitmaps
import android.graphics.Bitmap; //导入方法依赖的package包/类
@Override
public int countBitmaps(int width, int height, Config config) {
int c = 0;
synchronized (mCache) {
Iterator<Bitmap> it = mCache.iterator();
while (it.hasNext()) {
Bitmap bmp = it.next();
if (bmp.isRecycled()) {
it.remove();
continue;
}
if (bmp.getWidth() == width && bmp.getHeight() == height && bmp.getConfig() == config) {
c++;
}
}
}
return c;
}
示例4: getBitmapString
import android.graphics.Bitmap; //导入方法依赖的package包/类
@Nullable
@TargetApi(Build.VERSION_CODES.KITKAT)
private static String getBitmapString(Bitmap bitmap) {
if (bitmap == null) {
return null;
}
String sizeString = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
? " (" + bitmap.getAllocationByteCount() + ")" : "";
return "[" + bitmap.getWidth() + "x" + bitmap.getHeight() + "] " + bitmap.getConfig()
+ sizeString;
}
示例5: getRoundedBitmap
import android.graphics.Bitmap; //导入方法依赖的package包/类
public static Bitmap getRoundedBitmap(Bitmap input, int targetWidth, int targetHeight) {
if (input == null) {
return null;
}
final Bitmap.Config inputConfig = input.getConfig();
final Bitmap result = Bitmap.createBitmap(targetWidth, targetHeight,
inputConfig != null ? inputConfig : Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(result);
final Paint paint = new Paint();
canvas.drawARGB(0, 0, 0, 0);
paint.setAntiAlias(true);
final RectF dst = new RectF(0, 0, targetWidth, targetHeight);
canvas.drawOval(dst, paint);
// Specifies that only pixels present in the destination (i.e. the drawn oval) should
// be overwritten with pixels from the input bitmap.
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
final int inputWidth = input.getWidth();
final int inputHeight = input.getHeight();
// Choose the largest scale factor that will fit inside the dimensions of the
// input bitmap.
final float scaleBy = Math.min((float) inputWidth / targetWidth,
(float) inputHeight / targetHeight);
final int xCropAmountHalved = (int) (scaleBy * targetWidth / 2);
final int yCropAmountHalved = (int) (scaleBy * targetHeight / 2);
final Rect src = new Rect(
inputWidth / 2 - xCropAmountHalved,
inputHeight / 2 - yCropAmountHalved,
inputWidth / 2 + xCropAmountHalved,
inputHeight / 2 + yCropAmountHalved);
canvas.drawBitmap(input, src, dst, paint);
return result;
}
示例6: drawTextToBitmap
import android.graphics.Bitmap; //导入方法依赖的package包/类
public static Bitmap drawTextToBitmap(Context gContext, int gResId, String gText) {
Log.i(TAG, "drawTextToBitmap = " + gText);
Resources resources = gContext.getResources();
float scale = resources.getDisplayMetrics().density;
Bitmap bitmap =
BitmapFactory.decodeResource(resources, gResId);
Bitmap.Config bitmapConfig =
bitmap.getConfig();
// set default bitmap config if none
if(bitmapConfig == null) {
bitmapConfig = Bitmap.Config.ARGB_8888;
}
// resource bitmaps are imutable,
// so we need to convert it to mutable one
bitmap = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bitmap);
// new antialised Paint
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// text color - #3D3D3D
paint.setColor(Color.WHITE);
// text size in pixels
paint.setTextSize((int) (12 * scale));
// text shadow
// paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
// draw text to the Canvas center
Rect bounds = new Rect();
paint.getTextBounds(gText, 0, gText.length(), bounds);
int x = (bitmap.getWidth() - bounds.width())/2;
int y = (bitmap.getHeight())/2 + (int)scale*2;
canvas.drawText(gText, x, y, paint);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
return bitmap;
}
示例7: getConfig
import android.graphics.Bitmap; //导入方法依赖的package包/类
private static Bitmap.Config getConfig(Bitmap bitmap) {
Bitmap.Config config = bitmap.getConfig();
if (config == null) {
config = Bitmap.Config.ARGB_8888;
}
return config;
}
示例8: internalCopyBitmap
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* Copies the content of <code>sourceBitmap</code> to <code>destBitmap</code>. Both bitmaps must
* have the same width and height. If their {@link Bitmap.Config} are identical, the memory is
* directly copied. Otherwise, the <code>sourceBitmap</code> is drawn into
* <code>destBitmap</code>.
*/
private static void internalCopyBitmap(Bitmap destBitmap, Bitmap sourceBitmap) {
if (destBitmap.getConfig() == sourceBitmap.getConfig()) {
Bitmaps.copyBitmap(destBitmap, sourceBitmap);
} else {
// The bitmap configurations might be different when the source bitmap's configuration is
// null, because it uses an internal configuration and the destination bitmap's configuration
// is the FALLBACK_BITMAP_CONFIGURATION. This is the case for static images for animated GIFs.
Canvas canvas = new Canvas(destBitmap);
canvas.drawBitmap(sourceBitmap, 0, 0, null);
}
}
示例9: getImageBitmap
import android.graphics.Bitmap; //导入方法依赖的package包/类
public Bitmap getImageBitmap(String username, int size)
{
Bitmap bitmap = cache.get(getKey(username, size));
if (bitmap != null && !bitmap.isRecycled())
{
Bitmap.Config config = bitmap.getConfig();
return bitmap.copy(config, false);
}
return null;
}
示例10: doBlur
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* blur a given bitmap
*
* @param sentBitmap bitmap to blur
* @param radius blur radius
* @param canReuseInBitmap true if bitmap must be reused without blur
* @param context used by RenderScript, can be null if RenderScript disabled
* @return blurred bitmap
*/
public static Bitmap doBlur(Bitmap sentBitmap, int radius, boolean canReuseInBitmap, Context context) {
Bitmap bitmap;
if (canReuseInBitmap) {
bitmap = sentBitmap;
} else {
bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
}
if (bitmap.getConfig() == Bitmap.Config.RGB_565) {
// RenderScript hates RGB_565 so we convert it to ARGB_8888
// (see http://stackoverflow.com/questions/21563299/
// defect-of-image-with-scriptintrinsicblur-from-support-library)
bitmap = convertRGB565toARGB888(bitmap);
}
try {
final RenderScript rs = RenderScript.create(context);
final Allocation input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
final Allocation output = Allocation.createTyped(rs, input.getType());
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setRadius(radius);
script.setInput(input);
script.forEach(output);
output.copyTo(bitmap);
return bitmap;
} catch (RSRuntimeException e) {
Log.e(TAG, "RenderScript known error : https://code.google.com/p/android/issues/detail?id=71347 "
+ "continue with the FastBlur approach.");
}
return null;
}
示例11: doBlur
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* blur a given bitmap
*
* @param sentBitmap bitmap to blur
* @param radius blur radius
* @param canReuseInBitmap true if bitmap must be reused without blur
* @param context used by RenderScript, can be null if RenderScript disabled
* @return blurred bitmap
*/
public static Bitmap doBlur(Bitmap sentBitmap, int radius, boolean canReuseInBitmap, Context context) {
Bitmap bitmap;
if (canReuseInBitmap) {
bitmap = sentBitmap;
} else {
bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
}
if (bitmap.getConfig() == Bitmap.Config.RGB_565) {
// RenderScript hates RGB_565 so we convert it to ARGB_8888
// (see http://stackoverflow.com/questions/21563299/
// defect-of-image-with-scriptintrinsicblur-from-support-library)
bitmap = convertRGB565toARGB888(bitmap);
}
try {
final RenderScript rs = RenderScript.create(context);
final Allocation input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
final Allocation output = Allocation.createTyped(rs, input.getType());
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setRadius(radius);
script.setInput(input);
script.forEach(output);
output.copyTo(bitmap);
return bitmap;
} catch (RSRuntimeException e) {
Log.e(TAG, "RenderScript known error : https://code.google.com/p/android/issues/detail?id=71347 "
+ "continue with the FastBlur approach.");
}
return null;
}
示例12: cropFace
import android.graphics.Bitmap; //导入方法依赖的package包/类
public static Bitmap cropFace(FaceResult face, Bitmap bitmap, int rotate) {
Bitmap bmp;
float eyesDis = face.eyesDistance();
PointF mid = new PointF();
face.getMidPoint(mid);
Rect rect = new Rect(
(int) (mid.x - eyesDis * 1.20f),
(int) (mid.y - eyesDis * 0.55f),
(int) (mid.x + eyesDis * 1.20f),
(int) (mid.y + eyesDis * 1.85f));
Bitmap.Config config = Bitmap.Config.RGB_565;
if (bitmap.getConfig() != null) config = bitmap.getConfig();
bmp = bitmap.copy(config, true);
switch (rotate) {
case 90:
bmp = ImageUtils.rotate(bmp, 90);
break;
case 180:
bmp = ImageUtils.rotate(bmp, 180);
break;
case 270:
bmp = ImageUtils.rotate(bmp, 270);
break;
}
bmp = ImageUtils.cropBitmap(bmp, rect);
return bmp;
}
示例13: TiledBitmapCanvas
import android.graphics.Bitmap; //导入方法依赖的package包/类
public TiledBitmapCanvas(Bitmap bitmap, int tileSize, int maxVersions) {
mWidth = bitmap.getWidth();
mHeight = bitmap.getHeight();
mConfig = bitmap.getConfig();
mTileSize = tileSize;
mMaxVersions = maxVersions;
load(bitmap);
}
示例14: drawTextToBitmap
import android.graphics.Bitmap; //导入方法依赖的package包/类
public static Bitmap drawTextToBitmap(Context mContext, int resourceId, String mText) {
try {
Resources resources = mContext.getResources();
float scale = resources.getDisplayMetrics().density;
Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId);
android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
// set default bitmap config if none
if(bitmapConfig == null) {
bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
}
// resource bitmaps are imutable,
// so we need to convert it to mutable one
bitmap = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bitmap);
// new antialised Paint
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// text color - #3D3D3D
paint.setColor(Color.WHITE);
// text size in pixels
paint.setTextSize((int) (12 * scale));
// text shadow
paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY);
// draw text to the Canvas center
Rect bounds = new Rect();
paint.getTextBounds(mText, 0, mText.length(), bounds);
int x = (bitmap.getWidth() - bounds.width())/6;
int y = (bitmap.getHeight() - bounds.height())/3;
canvas.drawText(mText, x * scale, y * scale, paint);
return bitmap;
} catch (Exception e) {
// TODO: handle exception
return null;
}
}
示例15: getSafeConfig
import android.graphics.Bitmap; //导入方法依赖的package包/类
private static Bitmap.Config getSafeConfig(Bitmap bitmap) {
return bitmap.getConfig() != null ? bitmap.getConfig() : Bitmap.Config.ARGB_8888;
}