简体   繁体   中英

How to add view as background to the surfaceView?

Hi I am currently working on Game which contains VIEW of visualization of audio frequency effect in background of surfaceView.

The surfaceView contains actual game play.

I posting some code snippets :-

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 are created as follows -

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..
}

My VisualizerView running well so how I can add this as background to SurfaceView (Actual running Game).I am stuck on the problem that "How to add VIEW as background of surfaceView?" Or any another better solution for that.. Thx in advance..

One possibility you could have a look at is to retrieve the View's drawing cache Bitmap and add the Bitmap to the SurfaceView , drawn behind your game contents.

Retrieve the Bitmap :

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

Draw the Bitmap in SurfaceView's onDraw(...) :

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

I'm not sure this is a great idea in terms of performance if you have to retrieve the bitmap every frame of your game but it could be worth exploring.

try to add them using LinearLayout, and make them on top of each other using:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM