【问题标题】:How to add view as background to the surfaceView?如何将视图作为背景添加到surfaceView?
【发布时间】:2013-05-29 07:25:23
【问题描述】:

您好,我目前正在开发包含 SurfaceView 背景中音频效果可视化视图的游戏。

The surfaceView contains actual game play.

我发布了一些代码 sn-ps :-

main.xml

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_margin="5dp"
    android:layout_weight="1"
    android:background="#000" >

    <co.ardor.visualizer.VisualizerView
        android:id="@+id/visualizerView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </co.ardor.visualizer.VisualizerView>
</FrameLayout>

Visualizer view 创建如下 -

public class VisualizerView extends View
{
  private static final String TAG = "VisualizerView";

  private byte[] mBytes;
  private byte[] mFFTBytes;
  private Rect mRect = new Rect();
  private Visualizer mVisualizer;
  private Set<Renderer> mRenderers; // type of Renderer class

  private Paint mFlashPaint = new Paint(); // for flash paint
  private Paint mFadePaint = new Paint(); // for fade paint

  public VisualizerView(Context context, AttributeSet attrs, int defStyle)
  {
    super(context, attrs);
    init();
 }

  public VisualizerView(Context context, AttributeSet attrs)
 {
    this(context, attrs, 0);
 }

  public VisualizerView(Context context)
  {
   this(context, null, 0);
  } 

  // Some code regarding to the audio visualization..
}

我的 VisualizerView 运行良好,所以我如何将其作为背景添加到 SurfaceView(实际运行的游戏)。我被困在 "How to add VIEW as background of surfaceView?" 或任何其他更好的解决方案的问题上。 提前谢谢..

【问题讨论】:

  • 视图不能添加为背景。您必须将其设置为 Activity 的内容视图

标签: android view background surfaceview


【解决方案1】:

您可以查看的一种可能性是检索View's 绘图缓存Bitmap 并将Bitmap 添加到SurfaceView,绘制在您的游戏内容后面。

检索Bitmap:

myVisualizerView.setDrawingCacheEnabled(true);
mVisualizerBitmap = Bitmap.createBitmap(myVisualizerView.getDrawingCache());
myVisualizerView.setDrawingCacheEnabled(false);

在 SurfaceView 的onDraw(...) 中绘制Bitmap

canvas.drawBitmap(mVisualizerBitmap, //other params);

如果您必须在游戏的每一帧中检索位图,我不确定这在性能方面是否是一个好主意,但它可能值得探索。

【讨论】:

【解决方案2】:

尝试使用 LinearLayout 添加它们, 并使用:

android:layout_alignLeft="..."
android:layout_alignTop="..."

【讨论】:

    猜你喜欢
    • 2016-10-23
    • 2019-02-21
    • 2015-08-14
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    • 2012-05-12
    相关资源
    最近更新 更多