我有一台相机,我拍了一张照片并保存了位图。覆盖到这个,有一个视图寻呼机,具有不同的布局。当我拍照时,我也希望保存当前的叠加布局。
问题在于照片和布局都已保存,布局在位图区域中正确修复,但与预览相机相比,它更小。我需要放大布局(看起来像照片之前一样)并将其保存在我的位图中。
拍照前:
之后(按钮仍然存在,这只是一个测试):
我的代码:
Bitmap cameraBitmap = BitmapFactory.decodeByteArray
(data, 0, data.length);
int wid = cameraBitmap.getWidth();
int hgt = cameraBitmap.getHeight();
bitmap = Bitmap.createBitmap
(wid, hgt, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(cameraBitmap, 0f, 0f, null);
ViewPager pager = (ViewPager) main_activity.findViewById(R.id.pager);
ScreenSlidePagerAdapter a = (ScreenSlidePagerAdapter) pager.getAdapter();
ScreenSlidePageFragment currFrag = (ScreenSlidePageFragment) a.instantiateItem(pager, a.getFocusedPage());
View currView = currFrag.getView();
Rect rect = new Rect();
rect.set(0, 0, wid, hgt);
//Measure the view at the exact dimensions (otherwise the text won't center correctly)
int widthSpec = View.MeasureSpec.makeMeasureSpec(rect.width(), View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(rect.height(), View.MeasureSpec.EXACTLY);
currView.measure(widthSpec, heightSpec);
//Lay the view out at the rect width and height
currView.layout(0, 0, rect.width(), rect.height());
//Translate the Canvas into position and draw it
canvas.save();
canvas.translate(rect.left, rect.top);
currView.draw(canvas);
canvas.restore();
答案 0 :(得分:1)
为什么不用porportions创建位图。为此,您需要创建一个可缩放的位图,并将宽度设置为屏幕的1/4和屏幕高度的四分之一。
要这样做,首先在onCreate()方法中,还要添加以下代码以获取screenWidth和Height。 -
metrics = getResources().getDisplayMetrics();
display = getWindowManager().getDefaultDisplay();
dpi = metrics.densityDpi;
size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
要创建可伸缩位图,请使用以下代码,但请记住设置porportional的宽度和高度值,如下所示 -
public static int height = MainActivity.screenWidth* 4/10;
public static int width = MainActivity.screenWidth* 4/10;
bg = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
if (width + heigt > 0) {
bg = Bitmap.createScaledBitmap(bg,
(width), (heigt),
false);
}
另外,为了确保可绘制图像保持高质量,请确保创建单独的可绘制文件夹,例如“xxhdpi和hdpi”。请参阅http://developer.android.com/guide/practices/screens_support.html。希望有所帮助。祝你好运。
答案 1 :(得分:0)
我用视图创建了一个新的位图,并将它与前一个位图合并,结果很好:
Bitmap cameraBitmap = BitmapFactory.decodeByteArray
(data, 0, data.length);
int wid = cameraBitmap.getWidth();
int hgt = cameraBitmap.getHeight();
bitmap = Bitmap.createBitmap
(wid, hgt, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(cameraBitmap, 0f, 0f, null);
ViewPager pager = (ViewPager) main_activity.findViewById(R.id.pager);
ScreenSlidePagerAdapter a = (ScreenSlidePagerAdapter) pager.getAdapter();
ScreenSlidePageFragment currFrag = (ScreenSlidePageFragment) a.instantiateItem(pager, a.getFocusedPage());
View currView = currFrag.getView();
Bitmap viewBitmap = CustomUtil.loadBitmapFromView(currView);
viewBitmap = Bitmap.createScaledBitmap(viewBitmap, wid, wid, false);
canvas.drawBitmap(viewBitmap, 0f, (hgt-wid)/2 /* to center my square view in my rectangular original bitmap */, null);
实用方法:
public static Bitmap loadBitmapFromView(View view) {
int width = view.getWidth();
int height = view.getHeight();
int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
//Cause the view to re-layout
view.measure(measuredWidth, measuredHeight);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
//Create a bitmap backed Canvas to draw the view into
Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
//Now that the view is laid out and we have a canvas, ask the view to draw itself into the canvas
view.draw(c);
return b;
}