本文整理汇总了Java中android.graphics.BitmapFactory类的典型用法代码示例。如果您正苦于以下问题:Java BitmapFactory类的具体用法?Java BitmapFactory怎么用?Java BitmapFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BitmapFactory类属于android.graphics包,在下文中一共展示了BitmapFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildNotification
import android.graphics.BitmapFactory; //导入依赖的package包/类
public void buildNotification(Context context, final String albumName, final String artistName,
final String trackName, final Long albumId, final Bitmap albumArt,
final boolean isPlaying, MediaSessionCompat.Token mediaSessionToken) {
if (Utils.hasOreo()){
mNotificationManager.createNotificationChannel(AppNotificationChannels.getAudioChannel(context));
}
// Notification Builder
mNotificationBuilder = new NotificationCompat.Builder(mService, AppNotificationChannels.AUDIO_CHANNEL_ID)
.setShowWhen(false)
.setSmallIcon(R.drawable.itunes)
.setContentTitle(artistName)
.setContentText(trackName)
.setContentIntent(getOpenIntent(context))
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.cover))
.setPriority(Notification.PRIORITY_MAX)
.setStyle(new MediaStyle()
.setMediaSession(mediaSessionToken)
.setShowCancelButton(true)
.setShowActionsInCompactView(0, 1, 2)
.setCancelButtonIntent(retreivePlaybackActions(4)))
.addAction(new android.support.v4.app.NotificationCompat.Action(R.drawable.page_first, ""
, retreivePlaybackActions(3)))
.addAction(new android.support.v4.app.NotificationCompat.Action(isPlaying ? R.drawable.pause : R.drawable.play, ""
, retreivePlaybackActions(1)))
.addAction(new android.support.v4.app.NotificationCompat.Action(R.drawable.page_last, ""
, retreivePlaybackActions(2)));
mService.startForeground(APOLLO_MUSIC_SERVICE, mNotificationBuilder.build());
}
示例2: onActivityResult
import android.graphics.BitmapFactory; //导入依赖的package包/类
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
paoPaoQuan.setSelection(paoPaoState);
// requestCode标示请求的标示 resultCode表示有数据
Log.d("js", requestCode + "//" + resultCode + "**" + data);
if (requestCode==4||data != null) {
int childCount = imageGrid.getChildCount();
if (childCount > 9) {
Toast.makeText(this, "最多选择9张图片", 1).show();
} else {
imageId++;
String fPath = null;
if (requestCode == 4) {
fPath = mCurrentPhotoPath;
}else{
Uri uri = data.getData(); // 得到Uri
if((uri!=null&&!uri.equals(""))){
fPath = StaticMethod.getImageAbsolutePath(this, uri); // 转化为路径
}
}
Bitmap b = BitmapFactory.decodeFile(fPath);
b = StaticMethod.getThumImg(b, 100);
ImageView image = new ImageView(this);
image.setLayoutParams(new LayoutParams(130, 130));
image.setScaleType(ScaleType.FIT_XY);
image.setId(imageId);
image.setTag(fPath);
image.setImageBitmap(b);
image.setOnClickListener(this);
imageGrid.addView(image, childCount - 1);
}
}
}
示例3: 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;
}
示例4: onCreate
import android.graphics.BitmapFactory; //导入依赖的package包/类
@Override
public void onCreate() {
super.onCreate();
appInstance = this;
AndroidNetworking.initialize(getApplicationContext());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
AndroidNetworking.setBitmapDecodeOptions(options);
AndroidNetworking.enableLogging();
AndroidNetworking.setConnectionQualityChangeListener(new ConnectionQualityChangeListener() {
@Override
public void onChange(ConnectionQuality currentConnectionQuality, int currentBandwidth) {
Log.d(TAG, "onChange: currentConnectionQuality : " + currentConnectionQuality + " currentBandwidth : " + currentBandwidth);
}
});
}
示例5: compressImage
import android.graphics.BitmapFactory; //导入依赖的package包/类
/**
* 质量压缩
*
* @param image
* @return
*/
public static Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 100;
while (baos.toByteArray().length / 1024 > MAXSIZEKB) { //循环判断如果压缩后图片是否大于300kb,大于继续压缩
// Log.i("-----", "compressImage: options="+options+"--baos.toByteArray().length="+baos.toByteArray().length);
baos.reset();//重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
options -= 10;//每次都减少10
}
Log.e("压缩之后的图片大小", "compressImage: 111111options= " + options + "--baos.toByteArray().length= " + baos.toByteArray().length);
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
return bitmap;
}
示例6: decodeSampledBitmapFromResource
import android.graphics.BitmapFactory; //导入依赖的package包/类
/**
* Decode and sample down a bitmap from resources to the requested width and height.
*
* @param res The resources object containing the image data
* @param resId The resource id of the image data
* @param reqWidth The requested width of the resulting bitmap
* @param reqHeight The requested height of the resulting bitmap
* @param cache The ImageCache used to find candidate bitmaps for use with inBitmap
* @return A bitmap sampled down from the original with the same aspect ratio and dimensions
* that are equal to or greater than the requested width and height
*/
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight, ImageCache cache) {
// BEGIN_INCLUDE (read_bitmap_dimensions)
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// END_INCLUDE (read_bitmap_dimensions)
// If we're running on Honeycomb or newer, try to use inBitmap
if (Utils.hasHoneycomb()) {
addInBitmapOptions(options, cache);
}
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
示例7: getDiskBitmap
import android.graphics.BitmapFactory; //导入依赖的package包/类
/**
* 根据磁盘路径取得
* @param pathString
* @return
*/
public Bitmap getDiskBitmap(String pathString)
{
Bitmap bitmap = null;
try
{
File file = new File(pathString);
if(file.exists())
{
bitmap = BitmapFactory.decodeFile(pathString);
}
} catch (Exception e)
{
e.printStackTrace();
return null;
}
return bitmap;
}
示例8: calculateSampleSize
import android.graphics.BitmapFactory; //导入依赖的package包/类
private int calculateSampleSize(BitmapFactory.Options options) {
int outHeight = options.outHeight;
int outWidth = options.outWidth;
int sampleSize = 1;
int destHeight = 1000;
int destWidth = 1000;
if (outHeight > destHeight || outWidth > destHeight) {
if (outHeight > outWidth) {
sampleSize = outHeight / destHeight;
} else {
sampleSize = outWidth / destWidth;
}
}
if (sampleSize < 1) {
sampleSize = 1;
}
return sampleSize;
}
示例9: computeInitialSampleSize
import android.graphics.BitmapFactory; //导入依赖的package包/类
private static int computeInitialSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1 : (int) Math
.ceil(Math.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == UNCONSTRAINED) ? 128 : (int) Math
.min(Math.floor(w / minSideLength),
Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
// return the larger one when there is no overlapping zone.
return lowerBound;
}
if ((maxNumOfPixels == UNCONSTRAINED)
&& (minSideLength == UNCONSTRAINED)) {
return 1;
} else if (minSideLength == UNCONSTRAINED) {
return lowerBound;
} else {
return upperBound;
}
}
示例10: 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) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is getUrl 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;
}
示例11: onCreate
import android.graphics.BitmapFactory; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_define);
setCanceledOnTouchOutside(true);
ImageView img =findViewById(R.id.dialog_img);
pro =findViewById(R.id.dialog_progress);
ContentResolver cr = context.getContentResolver();
try {
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(path));
img.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Log.e("Exception", e.getMessage(),e);
}
pro.setMax(progreesmax);
new SendSocketService() .setProgressListener(new SendSocketService.setProgessIml() {
@Override
public void setProgress(int size) {
pro.setMax(size);
}
});
pro.setProgress(getProgrees);
}
示例12: getScaledBitmap
import android.graphics.BitmapFactory; //导入依赖的package包/类
public static Bitmap getScaledBitmap(int targetLength, File sourceImage) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(sourceImage.getAbsolutePath(), options);
// Get the dimensions of the original bitmap
int originalWidth = options.outWidth;
int originalHeight = options.outHeight;
float aspectRatio = (float) originalWidth / originalHeight;
// Calculate the target dimensions
int targetWidth, targetHeight;
if (originalWidth > originalHeight) {
targetWidth = targetLength;
targetHeight = Math.round(targetWidth / aspectRatio);
} else {
aspectRatio = 1 / aspectRatio;
targetHeight = targetLength;
targetWidth = Math.round(targetHeight / aspectRatio);
}
return Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, true);
}
示例13: decodeScaleImage
import android.graphics.BitmapFactory; //导入依赖的package包/类
/**
* 根据宽高路径返回图片
*/
public static Bitmap decodeScaleImage(String paramString, int width, int height)
{
BitmapFactory.Options localOptions = getBitmapOptions(paramString);
int i = calculateInSampleSize(localOptions, width, height);
localOptions.inSampleSize = i;
localOptions.inJustDecodeBounds = false;
Bitmap localBitmap1= null;
try{
localBitmap1 = BitmapFactory.decodeFile(paramString, localOptions);
}
catch (Exception e){
e.printStackTrace();
}
int j = readPictureDegree(paramString); //获取旋转角度
Bitmap localBitmap2 = null;
if ((localBitmap1 != null) && (j != 0))
{
localBitmap2 = rotaingImageView(j, localBitmap1);
localBitmap1.recycle();
localBitmap1 = null;
return localBitmap2;
}
return localBitmap1;
}
示例14: onPreExecute
import android.graphics.BitmapFactory; //导入依赖的package包/类
@Override
protected void onPreExecute() {
b = new NotificationCompat.Builder(context);
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent resultIntent = new Intent(context, MainActivity.class);
resultPendingIntent =
PendingIntent.getActivity(
context,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
b.setAutoCancel(false)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon))
.setSmallIcon(R.drawable.ic_battery_mgr_mod)
.setPriority(NotificationCompat.PRIORITY_MIN)
.setContentIntent(resultPendingIntent)
.setOngoing(true)
;
}
示例15: getImageMimeType
import android.graphics.BitmapFactory; //导入依赖的package包/类
/**
* Returns the mime type of the given item.
*/
public String getImageMimeType(FileItem item){
String mime = "";
try {
mime = URLConnection.guessContentTypeFromName(item.getPath());
} catch (StringIndexOutOfBoundsException e){
// Not sure the cause of this issue but it occurred on production so handling as blank mime.
}
if (mime == null || mime.isEmpty()){
// Test mime type by loading the image
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true;
BitmapFactory.decodeFile(item.getPath(), opt);
mime = opt.outMimeType;
}
return mime;
}