【发布时间】:2011-10-11 05:18:18
【问题描述】:
我在布局 xml 中定义了一个自定义 SurfaceView。我可以使用扩展 SurfaceView 的关联外部类在画布上绘制没有问题。但是,我的目标是在画布上绘制一个初始状态(我们称之为状态 0),它是一些带有白色背景的文本,并且在某些事件(例如按钮按下)之后,绘制图像(我们称之为状态 1)同一张画布。我可以将这两种状态分别绘制到同一个画布上,但是当事件发生时我很难改变画布的状态。
可能还有另一种方法可以做到这一点,也许在顶部创建一个覆盖视图并在按下按钮时以编程方式调用它。
这是我的详细信息: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,也没有尝试在 ButtonEnhance 上设置 onlick 侦听器。我也没有展示 onDraw 函数,因为它运行良好。我刚刚将其粘贴在这里以说明我的设置。我没有收到任何错误来报告 logcat 信息。更重要的是,我需要一些帮助来制定如何实现这种特殊情况。有任何想法吗?谢谢
【问题讨论】:
标签: android canvas surfaceview custom-view