本文整理汇总了Java中android.graphics.BitmapFactory.Options方法的典型用法代码示例。如果您正苦于以下问题:Java BitmapFactory.Options方法的具体用法?Java BitmapFactory.Options怎么用?Java BitmapFactory.Options使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.graphics.BitmapFactory
的用法示例。
在下文中一共展示了BitmapFactory.Options方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: decodeSampledBitmapFromResource
import android.graphics.BitmapFactory; //导入方法依赖的package包/类
/**
* 根据计算的inSampleSize,得到压缩后图片
*
* @param pathName
* @param reqWidth
* @param reqHeight
* @return
*/
private Bitmap decodeSampledBitmapFromResource(String pathName,
int reqWidth, int reqHeight)
{
// 第一次解析将inJustDecodeBounds设置为true,来获取图片大小
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pathName, options);
// 调用上面定义的方法计算inSampleSize值
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// 使用获取到的inSampleSize值再次解析图片
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(pathName, options);
return bitmap;
}
示例2: decodeBitmapFromResLruCache
import android.graphics.BitmapFactory; //导入方法依赖的package包/类
/**
* 读取图片 优先从缓存中获取图片
*/
static BitmapDrawable decodeBitmapFromResLruCache(Resources resources, @RawRes int resId,
int reqWidth, int reqHeight,
ImageCache cache) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
decodeStream(resources.openRawResource(resId), null, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
BitmapDrawable bitmapFromCache;
String resourceName = resources.getResourceName(resId);
bitmapFromCache = cache.getBitmapFromCache(resourceName);
if (bitmapFromCache == null) {
bitmapFromCache = readBitmapFromRes(resources, resId, cache, options);
cache.addBitmapToCache(resourceName, bitmapFromCache);
}
return bitmapFromCache;
}
示例3: getBitmapFromFile
import android.graphics.BitmapFactory; //导入方法依赖的package包/类
public static Bitmap getBitmapFromFile(File dst, int width, int height) {
if (null != dst && dst.exists()) {
BitmapFactory.Options opts = null;
if (width > 0 && height > 0) {
opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(dst.getPath(), opts);
final int minSideLength = Math.min(width, height);
opts.inSampleSize = computeSampleSize(opts, minSideLength, width * height);
opts.inJustDecodeBounds = false;
opts.inInputShareable = true;
opts.inPurgeable = true;
}
try {
return BitmapFactory.decodeFile(dst.getPath(), opts);
} catch (OutOfMemoryError e) {
try {
return BitmapFactory.decodeFile(dst.getPath(), opts);
} catch (OutOfMemoryError e2) {
e2.printStackTrace();
}
}
}
return null;
}
示例4: compressImage
import android.graphics.BitmapFactory; //导入方法依赖的package包/类
public static void compressImage(String savePath, String filepath,
int quality, Display display) {
int scale = 1;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filepath, options);
if (options.outWidth > IMAGE_MAX_WIDTH
|| options.outHeight > IMAGE_MAX_HEIGHT) {
scale = calculateInSampleSize(options, display.getWidth(),
display.getHeight());
}
options.inJustDecodeBounds = false;
options.inSampleSize = scale;
Bitmap bitmap = BitmapFactory.decodeFile(filepath, options);
Matrix matrix = new Matrix();
matrix.setRotate(readPictureDegree(filepath));
Bitmap saveBitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
writeToFile(savePath, saveBitmap, quality);
}
示例5: calculateInSampleSize
import android.graphics.BitmapFactory; //导入方法依赖的package包/类
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
}
return inSampleSize;
}
示例6: tryToGetBitmap
import android.graphics.BitmapFactory; //导入方法依赖的package包/类
private Bitmap tryToGetBitmap(File file, BitmapFactory.Options options, int rotate, boolean shouldScale) throws IOException, OutOfMemoryError {
Bitmap bmp;
if (options == null) {
bmp = BitmapFactory.decodeFile(file.getAbsolutePath());
} else {
bmp = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
}
if (bmp == null) {
throw new IOException("The image file could not be opened.");
}
if (options != null && shouldScale) {
float scale = calculateScale(options.outWidth, options.outHeight);
bmp = this.getResizedBitmap(bmp, scale);
}
if (rotate != 0) {
Matrix matrix = new Matrix();
matrix.setRotate(rotate);
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
}
return bmp;
}
示例7: createBitmapOptions
import android.graphics.BitmapFactory; //导入方法依赖的package包/类
/**
* Lazily create {@link BitmapFactory.Options} based in given
* {@link Request}, only instantiating them if needed.
*/
static BitmapFactory.Options createBitmapOptions(Request data) {
final boolean justBounds = data.hasSize();
final boolean hasConfig = data.config != null;
BitmapFactory.Options options = null;
if (justBounds || hasConfig || data.purgeable) {
options = new BitmapFactory.Options();
options.inJustDecodeBounds = justBounds;
options.inInputShareable = data.purgeable;
options.inPurgeable = data.purgeable;
if (hasConfig) {
options.inPreferredConfig = data.config;
}
}
return options;
}
示例8: calculateInSampleSize
import android.graphics.BitmapFactory; //导入方法依赖的package包/类
private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (reqWidth <= 0 || reqHeight <= 0) {
return 1;
}
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
示例9: loadTexture
import android.graphics.BitmapFactory; //导入方法依赖的package包/类
private int loadTexture(int resId) {
final int[] textureIds = new int[1];
GLES20.glGenTextures(1, textureIds, 0);
if (textureIds[0] == 0) return -1;
// do not scale the bitmap depending on screen density
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
final Bitmap textureBitmap = BitmapFactory.decodeResource(getResources(), resId, options);
attachBitmapToTexture(textureIds[0], textureBitmap);
return textureIds[0];
}
示例10: decodeSampledBitmap
import android.graphics.BitmapFactory; //导入方法依赖的package包/类
public static Bitmap decodeSampledBitmap(String path,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
示例11: hookDecodeStream
import android.graphics.BitmapFactory; //导入方法依赖的package包/类
@DoNotStrip
public static Bitmap hookDecodeStream(
InputStream inputStream,
Rect outPadding,
BitmapFactory.Options opts) {
StaticWebpNativeLoader.ensure();
inputStream = wrapToMarkSupportedStream(inputStream);
Bitmap bitmap;
byte[] header = getWebpHeader(inputStream, opts);
if (WebpSupportStatus.sIsWebpSupportRequired && isWebpHeader(header, 0, HEADER_SIZE)) {
bitmap = nativeDecodeStream(
inputStream,
opts,
getScaleFromOptions(opts),
getInTempStorageFromOptions(opts));
// We notify that the direct decoder failed
if (bitmap == null) {
sendWebpErrorLog("webp_direct_decode_stream");
}
setWebpBitmapOptions(bitmap, opts);
setPaddingDefaultValues(outPadding);
} else {
bitmap = originalDecodeStream(inputStream, outPadding, opts);
if (bitmap == null) {
sendWebpErrorLog("webp_direct_decode_stream_failed_on_no_webp");
}
}
return bitmap;
}
示例12: getImageSize
import android.graphics.BitmapFactory; //导入方法依赖的package包/类
/**
* obtain the image's width and height
*
* @param imagePath the path of image
*/
public static int[] getImageSize(String imagePath) {
int[] res = new int[2];
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inSampleSize = 1;
BitmapFactory.decodeFile(imagePath, options);
res[0] = options.outWidth;
res[1] = options.outHeight;
return res;
}
示例13: compressDisplay
import android.graphics.BitmapFactory; //导入方法依赖的package包/类
/**
* @param width must > 0
* @param height must > 0
*/
private Bitmap compressDisplay(String imagePath, int width, int height) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, options);
int outH = options.outHeight;
int outW = options.outWidth;
int inSampleSize = 1;
if (outH > height || outW > width) {
int halfH = outH / 2;
int halfW = outW / 2;
while ((halfH / inSampleSize) > height && (halfW / inSampleSize) > width) {
inSampleSize *= 2;
}
}
options.inSampleSize = inSampleSize;
options.inJustDecodeBounds = false;
int heightRatio = (int) Math.ceil(options.outHeight / (float) height);
int widthRatio = (int) Math.ceil(options.outWidth / (float) width);
if (heightRatio > 1 || widthRatio > 1) {
if (heightRatio > widthRatio) {
options.inSampleSize = heightRatio;
} else {
options.inSampleSize = widthRatio;
}
}
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(imagePath, options);
}
示例14: loadTexture
import android.graphics.BitmapFactory; //导入方法依赖的package包/类
public static int loadTexture(final Context context, final int resourceId)
{
final int[] textureHandle = new int[1];
GLES20.glGenTextures(1, textureHandle, 0);
if (textureHandle[0] != 0)
{
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false; // No pre-scaling
// Read in the resource
final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);
// Bind to the texture in OpenGL
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);
// Set filtering
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
// Load the bitmap into the bound texture.
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
// Recycle the bitmap, since its data has been loaded into OpenGL.
bitmap.recycle();
}
if (textureHandle[0] == 0)
{
throw new RuntimeException("Error loading texture.");
}
return textureHandle[0];
}
示例15: decodeBitmapFromPath
import android.graphics.BitmapFactory; //导入方法依赖的package包/类
public static Bitmap decodeBitmapFromPath(String srcPath, int reqWidth, int reqHeight) {
// First decode with <code>inJustDecodeBounds = true</code> to check dimensions.
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(srcPath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(srcPath, options);
}