2012-08-14 71 views
0

我有以下问题:我有一个应用程序与相机和它的顶部和图像作为覆盖。该图像是在4/3我得到了我的屏幕的最佳尺寸,我画的图像。 问题是我尝试时看不到所有图像。我将图像切割在屏幕的顶部和底部。事情是,如果我拍了一张照片,然后我看到照片完美Android覆盖4/3相机上的图像

我正在使用android sdk 8并且相机处于横向模式。在我的三星Galaxy S中,覆盖图和表面预览会提供640:480的相同尺寸。

我玩了一段时间,我还没有找到解决方案。

这是代码。

我的xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/framecamera" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:gravity="center" 
    android:padding="0dip" 
    android:layout_margin="0dip" 
> 
<FrameLayout 
    android:id="@+id/surface" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="0dip" 
    android:layout_margin="0dip" 
> 
</FrameLayout> 
<ImageButton 
    android:id="@+id/button_capture" 
    android:contentDescription="@string/click" 
    android:text="@string/click" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center|right" 
    android:background="@drawable/camera" 
    android:src="@drawable/camera" 
/> 
</LinearLayout> 

我的代码的重要部分:

setContentView(R.layout.camera); 

//create an instance of Camera 
mcam = getCameraInstance(); 
Camera.Parameters parameters=mcam.getParameters(); 
size=getBestPreviewSize(parameters);//gets the 4/3 size 

horizontal = Bitmap.createScaledBitmap(horizontal, size.width, size.height, false); 

// Create our Preview view and set it as the content of our activity. 
mpreview = new CameraPreview(getBaseContext(), mcam); 
FrameLayout frame = (FrameLayout) findViewById(R.id.surface); 
SurfaceHolder mSurfaceHolder = mpreview.getHolder(); 
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

//set our view to 4:3 
ViewGroup.LayoutParams params = frame.getLayoutParams(); 
params.width = size.width; 
params.height = size.height; 
frame.setLayoutParams(params); 

//add the necessary margin to the view 
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); 
MarginLayoutParams ps=(MarginLayoutParams)frame.getLayoutParams(); 
ps.topMargin = 0; 
ps.bottomMargin = 0; 
ps.leftMargin = (display.getWidth()-size.width)/2;//to center the overlay 
frame.setLayoutParams(ps); 

frame.addView(mpreview); 

over = new OverlayPreview(getBaseContext(),horizontal);//inside I draw the canvas 
frame.addView(over); 

任何想法,将不胜感激。我认为问题来自填充也许或XML,但我不知道如何解决它。非常感谢你。

p.d:对不起,我的英语,不是我的第一语言。

回答

0

好的!我终于找到了该怎么做!

我改变了做法,现在我正在使用和ImageView的框架。

// Create our Preview view 
mpreview = new CameraPreview(getBaseContext(), mcam); 

FrameLayout frame = (FrameLayout) findViewById(R.id.surface); 

SurfaceHolder mSurfaceHolder = mpreview.getHolder(); 
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

ImageView imageframe = (ImageView) findViewById(R.id.iframe); 
imageframe.setImageBitmap(horizontal); 

//set our view to 4:3 
ViewGroup.LayoutParams params = frame.getLayoutParams(); 
params.width = size.width; 
params.height = size.height; 
frame.setLayoutParams(params); 

//add the necessary margin to the view 
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); 
MarginLayoutParams ps=(MarginLayoutParams)frame.getLayoutParams(); 
ps.topMargin = 0; 
ps.bottomMargin = 0; 
ps.leftMargin = (int)((display.getWidth()-size.width)/2) + 50; 
ps.rightMargin = (int)((display.getWidth()-size.width)/2) + 50; 
frame.setLayoutParams(ps); 

frame.addView(mpreview); 

我想这不是最好的解决方案,但它至少工作。