Android:在两种状态之间更新自定义 SurfaceView 画布

发布于 2024-12-09 04:27:37 字数 3782 浏览 0 评论 0原文

我在布局 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>

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);
    }
}
  }

在此代码中,我没有显示绘图状态 0,也没有尝试在 ButtonEnhance 上设置 onlick 侦听器。我也没有展示 onDraw 函数,因为它运行良好。我刚刚将其粘贴到此处来说明我的设置。我没有收到任何报告 logcat 信息的错误。更重要的是,我需要一些帮助来制定如何实现这个特定的案例。有什么想法吗?谢谢

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 the 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好菇凉咱不稀罕他 2024-12-16 04:27:37

尝试实现线程在画布上绘制内容,就像本教程中的那样。它可能会解决你的问题。

http://www.droidnova.com/playing -with-graphics-in-android-part-iii,176.html

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文