2015-05-03 66 views
0

我試圖在相機預覽上繪製矩形疊加。我已經在FrameView中添加了疊加層,onDraw()被成功調用,但矩形永遠不會顯示出來。我已經四處搜尋,嘗試了不同的解決方案,但迄今爲止還沒有任何解決方案。無法在相機預覽上繪製矩形

這就是我對FrameView

public class FrameView extends View { 
    private Paint mFramePaint = null; 
    private final String TAG = "FrameView"; 

    public FrameView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     mFramePaint = new Paint(); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     mFramePaint.setStyle(Paint.Style.FILL); 
     mFramePaint.setColor(Color.GREEN); 

     Rect drawRect = new Rect(300,300,300,300); 

     canvas.drawRect(drawRect, mFramePaint); 
    } 
} 

這是我的相機預覽活動

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.eyesee.CameraPreviewActivity" 
    android:id="@+id/camera_preview"> 

    <com.example.eyesee.FrameView 
     android:id="@+id/frame_view" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 
</RelativeLayout> 

XML這是從我的相機預覽活動代碼段用於連接相機預覽

private Camera mCamera; 
private CameraPreview mPreview; 
private final String TAG = "CameraPreviewActivity"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_camera_preview); 

    mCamera = MainActivity.getCameraInstance(); 

    mPreview = new CameraPreview(this, mCamera); 
    RelativeLayout preview = (RelativeLayout) findViewById(R.id.camera_preview); 
    preview.addView(mPreview); 
} 

回答

0

在RelativeLayout子視圖按順序繪製, 你需要添加視圖下方的預覽:

preview.addView(mPreview,0); 

您的矩形爲0的大小 - 新的矩形(左,上,右,下)

Rect drawRect = new Rect(0,0,300,300); 
+0

我試過,但它仍然沒有按」解決這個問題。我仍然只能看到相機預覽,沒有顯示任何視圖。 – monopandora

+0

你的矩形也是錯的...我編輯帖子... –