Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 548
Swapnil Sonar Avatar asked Jun 03 '13 07:06

Swapnil Sonar


People also ask

How do you get the background color on a view?

To get background color of a Layout: LinearLayout lay = (LinearLayout) findViewById(R. id. lay1); ColorDrawable viewColor = (ColorDrawable) lay.

What is Surface view in android studio?

Displays a video file. Provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface and, if you like, its size; the SurfaceView takes care of placing the surface at the correct location on the screen.


2 Answers

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.

like image 65
onosendai Avatar answered Sep 28 '22 17:09

onosendai


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

android:layout_alignLeft="..."
android:layout_alignTop="..."
like image 35
Taldroid Avatar answered Sep 28 '22 18:09

Taldroid