How to add view as background for surfaceView?

Hi, I'm currently working on a game that contains a visualization of the sound frequency effect in the background of a surfaceView.

The surfaceView contains actual game play.

I post 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 works well, as I can add this as a SurfaceView background (Actual running Game). I am stuck in the problem that "How to add VIEW as background of surfaceView?" or any other best solution for this. thanks in advance..

+6
source share
2 answers

One of the features you might want to pay attention to is to extract View's Bitmap cache image and add Bitmap to SurfaceView , drawn behind the contents of your game.

Extract Bitmap :

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

Draw Bitmap in SurfaceView onDraw(...) :

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

I'm not sure if this is a great idea in terms of performance if you need to get a bitmap in every frame of your game, but it might be worth exploring.

+7
source

try adding them with LinearLayout, and make them on top of each other using:

 android:layout_alignLeft="..." android:layout_alignTop="..." 
+1
source

Source: https://habr.com/ru/post/946382/


All Articles