2015-05-02 73 views
0

我試圖與在使用本爲導向的攝像機視圖可調整大小的矩形建立一個Android程序可調整大小的矩形: How to create a resizable rectangle with user touch events on Android?安卓顯示上述相機

我一直停留了一天,因爲我無法觀看在相對佈局上顯示並找不到問題(我相對較新)。

這裏是我的XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<RelativeLayout 
    android:id="@+id/camera_preview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <View 
     android:id="@+id/camera_box" 
     android:layout_alignParentTop="true" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@android:color/transparent"> 
    </View> 

    <Button 
     android:id="@+id/button_ChangeCamera" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@android:color/white" 
     android:text="Switch Camera" 
     android:textColor="@android:color/holo_orange_dark" 
     android:layout_marginTop="30dp" 
     android:layout_marginRight="30dp" 
     android:minWidth="100dp" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true"/> 
    <Button 
     android:id="@+id/button_capture" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="30dp" 
     android:background="@android:color/white" 
     android:text="Capture" 
     android:textColor="@android:color/holo_orange_dark" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true"/> 

</RelativeLayout> 

這裏是我的初始化:

public class TakePhoto extends Activity { 
private Camera mCamera; 
private PhotoHandler mPreview; 
private PictureCallback mPicture; 
private Button capture, switchCamera; 
private View drawView; 
private Context myContext; 
private RelativeLayout cameraPreview; 
private boolean cameraFront = false; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main_page); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
    myContext = this; 
    initialize(); 
} 

public void initialize() { 
    cameraPreview = (RelativeLayout) findViewById(R.id.camera_preview); 

    mPreview = new PhotoHandler(myContext, mCamera); 
    cameraPreview.addView(mPreview); 

    capture = (Button) findViewById(R.id.button_capture); 
    capture.setOnClickListener(captrureListener); 

    switchCamera = (Button) findViewById(R.id.button_ChangeCamera); 
    switchCamera.setOnClickListener(switchCameraListener); 
    drawView = new DrawView(myContext); 
    drawView = findViewById(R.id.camera_box); 
    drawView.bringToFront(); 

} 

//more code 

} 

drawView函數覆蓋的onDraw和實現的onTouchEvent,基本上爲完成張貼阮解決方案Binh:How to create a resizable rectangle with user touch events on Android?

可能有人幫助我失去了什麼?

感謝

回答

0
drawView = new DrawView(myContext); 
drawView = findViewById(R.id.camera_box); 

您創建的類drawView函數的一個新的對象,但在接下來的行分配不同的對象相同的變量。 DrawView現在基本失效了。相反,第二行的,你可以使用addView(drawView),但最簡單的方法是在你的layout.xml

+0

由於使用

<…package…DrawView android:id="@+id/camera_box" 

!這使得更多的意義!現在修復了它並且工作正常。 :) – Alexei