下面列出了android.graphics.drawable.BitmapDrawable#draw() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Method to add style to bitmap
*
* @param context
* @param bitmap Bitmap object to change, we don't operate on this bitmap because it's immutable, you should use the returned bitmap object
* @param mode
* @param brightness if you don't want to change brightness, pass 0
* @param contrast if you don't want to change contrast, pass 1
* @param saturation if you don't want to change saturation, pass 1. If saturation is set, then the mode must be Styler.Mode.SATURATION
* @return
*/
public static Bitmap addStyleToBitmap(Context context, Bitmap bitmap, int mode, int brightness, float contrast, float saturation) {
if (saturation != 1 && mode != Mode.SATURATION && mode != Mode.NONE) {
throw new IllegalArgumentException("saturation must be 1.0 when mode is not Styler.Mode.SATURATION");
}
if (brightness > 255) {
throw new IllegalArgumentException("brightness can't be bigger than 255");
} else if (brightness < -255) {
throw new IllegalArgumentException("brightness can't be smaller than -255");
}
if (contrast < 0) {
throw new IllegalArgumentException("contrast can't be smaller than 0");
}
if (saturation < 0) {
throw new IllegalArgumentException("saturation can't be smaller than 0");
}
Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
context = context.getApplicationContext();
BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap);
drawable.setColorFilter(new ColorMatrixColorFilter(calculateMatrix(mode, brightness, contrast, saturation)));
drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
drawable.draw(canvas);
return newBitmap;
}
private Bitmap drawVipType(UserModel model, Bitmap bitmap) {
if (!model.verified || model.verified_type < 0) return bitmap;
BitmapDrawable drawable = mVipDrawable[model.verified_type > 1 ? 1 : model.verified_type];
Bitmap copy = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(copy);
int w1 = bitmap.getWidth();
int w2 = w1 / 4;
int h1 = bitmap.getHeight();
int h2 = h1 / 4;
drawable.setBounds(w1 - w2, h1 - h2, w1, h1);
drawable.draw(canvas);
bitmap.recycle();
return copy;
}
@Override
public Bitmap transform(Bitmap source) {
if( color == 0 ) {
return source;
}
BitmapDrawable drawable = new BitmapDrawable(Resources.getSystem(), source );
Bitmap result = Bitmap.createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888 );
Canvas canvas = new Canvas( result );
drawable.setBounds( 0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight() );
drawable.setColorFilter( color, PorterDuff.Mode.SRC_IN );
drawable.draw(canvas);
drawable.setColorFilter(null);
drawable.setCallback(null);
if( result != source ) {
source.recycle();
}
return result;
}
private void drawEndGameState(Canvas canvas) {
double alphaChange = 1;
continueButtonEnabled = false;
for (AnimationCell animation : game.aGrid.globalAnimation) {
if (animation.getAnimationType() == MainGame.FADE_GLOBAL_ANIMATION) {
alphaChange = animation.getPercentageDone();
}
}
BitmapDrawable displayOverlay = null;
if (game.gameWon()) {
if (game.canContinue()) {
continueButtonEnabled = true;
displayOverlay = winGameContinueOverlay;
} else {
displayOverlay = winGameFinalOverlay;
}
} else if (game.gameLost()) {
displayOverlay = loseGameOverlay;
}
if (displayOverlay != null) {
displayOverlay.setBounds(startingX, startingY, endingX, endingY);
displayOverlay.setAlpha((int) (255 * alphaChange));
displayOverlay.draw(canvas);
}
}
public static Bitmap getTiled(final Bitmap source, final int xSize, final int ySize, final boolean tile, final String background_file) {
Bitmap tiledBitmap = null;
if (background_file != null) {
try {
tiledBitmap = BitmapLoader.bitmapFromBundleCache(background_file);
if (tiledBitmap == null) {
android.util.Log.d("NumberWall", "Regenerating image");
final Uri background_uri = Uri.parse(background_file);
FileDescriptor fileDescriptor = xdrip.getAppContext().getContentResolver().openFileDescriptor(background_uri, "r").getFileDescriptor();
final Bitmap image_bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
Bitmap rotated_bitmap = image_bitmap;
final Matrix image_matrix = new Matrix();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
fileDescriptor = xdrip.getAppContext().getContentResolver().openFileDescriptor(background_uri, "r").getFileDescriptor(); // reset
final ExifInterface exif = new ExifInterface(fileDescriptor);
int rotation = exifOrientationToDegrees(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
android.util.Log.d("NumberWall", "Rotation: " + rotation);
if (rotation != 0) {
image_matrix.preRotate(rotation);
rotated_bitmap = Bitmap.createBitmap(image_bitmap, 0, 0, image_bitmap.getWidth(), image_bitmap.getHeight(), image_matrix, true);
image_bitmap.recycle();
}
}
tiledBitmap = getBestCroppedScaled(rotated_bitmap, xSize, ySize);
BitmapLoader.saveBitmapAsBundle(background_file, Bitmap.createBitmap(tiledBitmap));
} else {
tiledBitmap = Bitmap.createBitmap(tiledBitmap); // make a copy
android.util.Log.d("NumberWall", "cache hit");
}
} catch (Exception e) {
// cannot load bitmap
android.util.Log.e("NumberWall", "Cannot load bitmap: " + e);
}
}
if (tiledBitmap == null) {
tiledBitmap = Bitmap.createBitmap(xSize, ySize, Bitmap.Config.ARGB_8888);
}
if (source != null) {
final Canvas canvas = new Canvas(tiledBitmap);
final BitmapDrawable drawable = new BitmapDrawable(xdrip.getAppContext().getResources(), source);
drawable.setBounds(0, 0, xSize, ySize);
if (tile) {
drawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
drawable.draw(canvas);
} else {
double y_ratio = JoH.tolerantParseDouble(Pref.getString("numberwall_y_param", ""), 50d) / 100d; // TODO move to method signature
int yoffset = Math.max(0, (int) ((getScreenDpi() * y_ratio * 1.2d) - (getScreenDpi() * 0.30d)));
final double spacer_ratio = JoH.tolerantParseDouble(Pref.getString("numberwall_s_param", ""), 10d) / 100d;
int xoffset = Math.max(0, (int) ((getScreenDpi() * spacer_ratio * 1.2d) - (getScreenDpi() * 0.30d)));
canvas.drawBitmap(source, xoffset, yoffset, new Paint());
}
source.recycle();
} // returns blank bitmap if source is null
return tiledBitmap;
}
public static Bitmap getTiled(final Bitmap source, final int xSize, final int ySize, final boolean tile, final String background_file) {
Bitmap tiledBitmap = null;
if (background_file != null) {
try {
tiledBitmap = BitmapLoader.bitmapFromBundleCache(background_file);
if (tiledBitmap == null) {
android.util.Log.d("NumberWall", "Regenerating image");
final Uri background_uri = Uri.parse(background_file);
FileDescriptor fileDescriptor = xdrip.getAppContext().getContentResolver().openFileDescriptor(background_uri, "r").getFileDescriptor();
final Bitmap image_bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
Bitmap rotated_bitmap = image_bitmap;
final Matrix image_matrix = new Matrix();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
fileDescriptor = xdrip.getAppContext().getContentResolver().openFileDescriptor(background_uri, "r").getFileDescriptor(); // reset
final ExifInterface exif = new ExifInterface(fileDescriptor);
int rotation = exifOrientationToDegrees(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
android.util.Log.d("NumberWall", "Rotation: " + rotation);
if (rotation != 0) {
image_matrix.preRotate(rotation);
rotated_bitmap = Bitmap.createBitmap(image_bitmap, 0, 0, image_bitmap.getWidth(), image_bitmap.getHeight(), image_matrix, true);
image_bitmap.recycle();
}
}
tiledBitmap = getBestCroppedScaled(rotated_bitmap, xSize, ySize);
BitmapLoader.saveBitmapAsBundle(background_file, Bitmap.createBitmap(tiledBitmap));
} else {
tiledBitmap = Bitmap.createBitmap(tiledBitmap); // make a copy
android.util.Log.d("NumberWall", "cache hit");
}
} catch (Exception e) {
// cannot load bitmap
android.util.Log.e("NumberWall", "Cannot load bitmap: " + e);
}
}
if (tiledBitmap == null) {
tiledBitmap = Bitmap.createBitmap(xSize, ySize, Bitmap.Config.ARGB_8888);
}
if (source != null) {
final Canvas canvas = new Canvas(tiledBitmap);
final BitmapDrawable drawable = new BitmapDrawable(xdrip.getAppContext().getResources(), source);
drawable.setBounds(0, 0, xSize, ySize);
if (tile) {
drawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
drawable.draw(canvas);
} else {
double y_ratio = JoH.tolerantParseDouble(Pref.getString("numberwall_y_param", ""), 50d) / 100d; // TODO move to method signature
int yoffset = Math.max(0, (int) ((getScreenDpi() * y_ratio * 1.2d) - (getScreenDpi() * 0.30d)));
final double spacer_ratio = JoH.tolerantParseDouble(Pref.getString("numberwall_s_param", ""), 10d) / 100d;
int xoffset = Math.max(0, (int) ((getScreenDpi() * spacer_ratio * 1.2d) - (getScreenDpi() * 0.30d)));
canvas.drawBitmap(source, xoffset, yoffset, new Paint());
}
source.recycle();
} // returns blank bitmap if source is null
return tiledBitmap;
}