Android: Update custom surfaceview canvas between two states
I have a custom SurfaceView defined in my layout xml. I can draw to the canvas no problem with an associated external class which extended SurfaceView. However, my aim is to draw an initial state (lets call it state 0) to the canvas which is some text with a white background, and after some event such as a button press an image is drawn (lets call it state 1) on the same canvas. Separately I can draw both of these states to the same canvas, however I am having difficulty in changing the state of the canvas when an event occurs.
There might also be another method to do this, perhaps creating an overlaid view on top and programatically calling it on the button press.
Here are my particulars: 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>
Main Activity
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);
}
}
Custom 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);
}
}
}
In this code I have not show drawing state 0, nor an attempt to set an onlick listener on to the ButtonEnhance. I haven't shown th开发者_运维百科e onDraw function either, as it works well. I have just pasted it here to illustrate my setup. I am not getting any error's to report logcat information. It's more that I need some help in formulating how to achieve this particular case. Any idea's? Thanks
Try mplement thread to draw things on canvas, something like in this tutorial. It might solve your problem.
http://www.droidnova.com/playing-with-graphics-in-android-part-iii,176.html
精彩评论