我在我的佈局xml中定義了自定義SurfaceView。我可以畫到畫布沒有擴展SurfaceView的關聯外部類的問題。然而,我的目標是繪製一個初始狀態(讓它的狀態爲0)到畫布上,這是一些帶有白色背景的文字,並且在按下某個事件(例如按下按鈕)之後,繪製圖像(讓它稱爲狀態1)同一幅畫布。另外,我可以將這兩種狀態都繪製到同一個畫布上,但是當發生事件時,我很難更改畫布的狀態。Android:在兩個狀態之間更新自定義Surfaceview畫布
也可能有另一種方法來做到這一點,也許在頂部創建一個覆蓋視圖,並在按鈕上按編程方式調用它。
這裏是我的細節:XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FFFFFFFF"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_height="wrap_content"
android:layout_marginLeft= "5dp"
android:layout_marginTop = "5dp"
android:layout_marginBottom = "5dp"
android:id="@+id/textNoisy"
android:layout_width="fill_parent"
android:text="@string/TextNoisy"
android:textColor="#161616"></TextView>
<com.speechenhancer.SpecGuageNoisy
android:id="@+id/SpecGuageNoisy"
android:layout_width="fill_parent"
android:layout_height="220dp"
android:layout_below="@+id/textNoisy"/>
<TextView android:layout_height="wrap_content"
android:layout_marginLeft= "5dp"
android:layout_marginBottom = "5dp"
android:id="@+id/textEnhanced"
android:layout_below="@+id/SpecGuageNoisy"
android:layout_width="wrap_content"
android:text="@string/TextEnhanced"
android:textColor="#161616"></TextView>
<Button android:id="@+id/ButtonEnhance"
android:layout_marginTop = "20dp"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:text="@string/ButtonEnhance"
android:layout_alignParentBottom="true"
android:layout_gravity="center" ></Button>
主要活動
public class SpecGuageNoisy extends SurfaceView implements SurfaceHolder.Callback {
public SpecGuageNoisy(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
holder = getHolder();
holder.addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (SpecState.getCanvasState()==0){
// Draw Initial state
}else if (SpecState.getCanvasState()==1){
spectrum = init();
canvas = getHolder().lockCanvas();
canvas.drawColor(Color.WHITE);
onDraw(canvas,spectrum, nsegs, seglen,nshift);
getHolder().unlockCanvasAndPost(canvas);
}
}
自定義SurfaceView:
public class SpecGuageNoisy extends SurfaceView implements SurfaceHolder.Callback {
public SpecGuageNoisy(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
holder = getHolder();
holder.addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (SpecState.getCanvasState()==0){
// Draw Initial state
}else if (SpecState.getCanvasState()==1){
spectrum = init();
canvas = getHolder().lockCanvas();
canvas.drawColor(Color.WHITE);
onDraw(canvas,spectrum, nsegs, seglen,nshift);
getHolder().unlockCanvasAndPost(canvas);
}
}
}
在這段代碼中我沒有表現出拉絲狀態0也沒有嘗試設置Onlick偵聽器到ButtonEnhance。我還沒有顯示onDraw函數,因爲它運行良好。我剛把它粘貼在這裏來說明我的設置。我沒有收到任何報告logcat信息的錯誤。更重要的是,我需要一些幫助來制定如何實現這種特殊情況。有任何想法嗎?謝謝
奇怪你會提到它,因爲我已經從一個多線程的方式轉換回線性的方法,因爲我認爲這可能是更容易。我已經實現了與該鏈接的着陸頁上的內容類似的內容,但仍然存在相同的問題。無法在兩種狀態之間轉換。 – digiphd
指向教程的鏈接已損壞 – Ravi