首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

设立图片在规定范围中间

2012-07-24 
设置图片在规定范围中间?Bitmap centerToFit(Bitmap bitmap, int width, int height, Context context) {?

设置图片在规定范围中间

?Bitmap centerToFit(Bitmap bitmap, int width, int height, Context context) {

? ? ? ? final int bitmapWidth = bitmap.getWidth();

? ? ? ? final int bitmapHeight = bitmap.getHeight();

?

? ? ? ? if (bitmapWidth < width || bitmapHeight < height) {

? ? ? ? ? ? int color = context.getResources().getColor(R.color.window_background);

?

? ? ? ? ? ? Bitmap centered = Bitmap.createBitmap(bitmapWidth < width ? width : bitmapWidth,

? ? ? ? ? ? ? ? ? ? bitmapHeight < height ? height : bitmapHeight, Bitmap.Config.RGB_565);

? ? ? ? ? ? centered.setDensity(bitmap.getDensity());

? ? ? ? ? ? Canvas canvas = new Canvas(centered);

? ? ? ? ? ? canvas.drawColor(color);

? ? ? ? ? ? canvas.drawBitmap(bitmap, (width - bitmapWidth) / 2.0f, (height - bitmapHeight) / 2.0f,

? ? ? ? ? ? ? ? ? ? null);

?

? ? ? ? ? ? bitmap = centered;

? ? ? ? }

?

? ? ? ? return bitmap;

? ? }

热点排行