I'm trying to make a VideoView work on top of the cocos2d-x surface view. I am now able to load and play a video using the following snippets of code:
When Initializing the class:
// Create the LinearLayout that will contain our VideoView
_videoLayout = new LinearLayout( _activity );
_videoLayout.setId( VIDEO_VIEW_ID );
_videoLayout.setGravity( Gravity.CENTER );
_videoLayout.setBackgroundColor( Color.BLACK );
_videoLayout.setOrientation( LinearLayout.VERTICAL );
_videoLayout.bringToFront();
// Add the LinearLayout to the current Activity
_activity = MyGame.getCocosActivity();
_activity.addContentView( _videoLayout, new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT ) );
When playing a video:
// When the video is ready to be played, create a VideoView
VideoView videoView = new VideoView( Cocos2dxActivity.getContext() );
videoView.setLayoutParams( new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT ) );
// Add it to the _videoLayout object
LinearLayout layout = ( LinearLayout ) activity.findViewById( VIDEO_VIEW_ID );
layout.setVisibility( View.VISIBLE );
layout.addView( videoView );
// Play the video
videoView.setVideoURI( Uri.parse( path ) );
videoView.requestFocus();
videoView.start();
The above code yields the following results:
The video is played and sounds are properly heard.
The video is centered both horizontally and vertically.
Sometimes, the video is played below the current Cocos2d-x view. Playing it again will make it be played on top of it.
The video is not full screen. It only covers enough space in the screen to show the video.
What my expected results are:
The video is played on top of the cocos2d-x view, at all times.
The video takes up the whole screen, still maintaining aspect ratio, but has black borders on the sides.
I can't seem to figure out how to accomplish these two. Any help? I'm not very experienced with the Android Framework so please bear with me.
NOTE: I don't want to use XML to create layouts.
Why don't you want to use XML layouts? They are designed for this, and amke your life much easier after you get a hang of them.
Anyhow : in my case I added my VideoView on a RelativeLayout and set its CenterHorizontal and CenterVertical parameters to true. The width and height is set to wrap_content. This was done in XML, so I cannot provide you with Java equivalent.
Also try calling bringToFront() on your layout to ensure that the video is on top of CocosView.
Related
I want to implement exoplayer with exoplayer filter together? any possible ways?.
I am successfully implemented the exoplayer with texture . its working fine .
now I want set the GlFilter on top of exoplayer (texture videoview in flutter). like below
ePlayerView = new EPlayerView(this);
// set SimpleExoPlayer
ePlayerView.setSimpleExoPlayer(player);
ePlayerView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// add ePlayerView to WrapperView
((MovieWrapperView) findViewById(R.id.layout_movie_wrapper)).addView(ePlayerView);
ePlayerView.onResume();
I have to add a fragment over an Imageview, but my ImageView goes on top and covers the other views. All this is done in a RelativeLayout.
things must be done dynamically (no xml). I want that my activity has an image, which can be set DYNAMICALLY, and my fragment (added dynamically) should have transparent background, and if this is not true, i can set it to transparent at run time.
This is the code to create the image:
ImageView img=new ImageView(this);
img.setImageResource(imgID);
img.setScaleType(ImageView.ScaleType.CENTER_CROP);
((ViewGroup)findViewById(android.R.id.content)).getRootView()).addView(img);
As you can see, the image works as background, i'm using a ImageView for its scaleType.
Is there a way to do so?
SOLUTION
solved, i added the image to the wrong container.
The function below, however, has been pretty useful. It has been taken from an other answer, and lets a view to get on the back of all the others.
public static void sendViewToBack(View child) {
final ViewGroup parent = (ViewGroup)child.getParent();
if (parent!=null) {
parent.removeView(child);
parent.addView(child, 0);
}
}
I am trying to create a View of the current display so I can call the following methods to create a Bitmap:
v1.setDrawingCacheEnabled(true);
current = Bitmap.createBitmap(v1.getDrawingCache(), 1686, 136, (631-168), (343-136));
v1.setDrawingCacheEnabled(false);
To create the View I tried this line
View v1 = mCurrentUrlMask.getRootView();
I got the error "Symbol 'mCurrentUrlMask' cannot be resolved."
I'm looking to create a bitmap of a section of the screen, and thoughts you have on this issue are greatly appreciated.
1
View v1 = MainActivity.this.getWindow().getDecorView();//editted
v1.buildDrawingCache();
//your rest of the code.
// you will get a bitmap of the whole screen.
or you could use ViewSnapper
My App, (OpenGL ES 2.0) currently has 55 classes, and one activity single.
I have my own custom Scene Manager which I use to change between different Scenes (ie, different scenes would be: Main Menu, Level Select, Level 1, Level 2....Level 20, Game Over etc...)
So I set all of my AdMob stuff up in my Activity's onCreate(); method. So at the moment, the same ad runs throughout the whole app.
How it is possible to 'switch off' the ads on different Scenes?
Bear in mind that none of my classes have direct access to the Activity class itself.
Any suggestions would be appreciated as I'm finding using AdMob extremely challenging, it doesn't seem very intuitive so any AdMob experts out there, your opinions would be appreciated!
I should point out that I don't use XML, everything is done programmatically.
Thanks
Code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(TestDeviceID)
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
//Request full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Create a displayMetrics object to get pixel width and height
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
height = metrics.heightPixels;
//Work out values for resizing screen while keeping aspect ratio
width = (int) Math.min(width, height * 1.702127659574468);
height = (int) Math.min(height, width / 1.702127659574468);
//Create and set GL view (OpenGL View)
myView = new MyGLSurfaceView(MainActivity.this);
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
//Set the colour if we don't, the ad won't show (bug?)
adView.setBackgroundColor(Color.BLACK);
layout.addView(myView);
layout.addView(adView, adParams);
//Create a copy of the Bundle
if (savedInstanceState != null){
newBundle = new Bundle(savedInstanceState);
}
setContentView(layout);
}
First of all you're causing yourself a lot of pain by doing your layout programmatically (I gave a talk on this just last night). You should really consider understanding and using the XML layout.
But to answer your question, you will need to find a way for the classes that want to control whether an ad is shown or not to have some method of sending a message to the Activity. I would suggest injecting them with an interface that is implemented by an inner classes in your Activity.
The interface is going to need to look something like:
- startShowingAds - this should start requesting ads and make the AdView visible.
- stopShowingAds - this should pause ad showing and hide the AdView.
To temporarily hide the ads you just need to hide the AdView, eventually calling pause() even though I think that hidden ad views are not refreshed (you can check in the logs).
About how to reach the AdView object from your code, it depends on what you have accessible from there. If you just have the View where you are drawing a solution could be to store the AdView object in the View's tag (see setTag() and getTag() here: https://developer.android.com/reference/android/view/View.html#getTag%28int%29).
I'm a new to android programming. I'm having issues with the layout in my activity. My menu appears like this:
and I've done all the layout work directly through the source code:
enterNameTxt.setText("Enter User Name");
enterNameTxt.setY(200);
enterNameTxt.setX(-600);
userNameTxt.setY(300);
userNameTxt.setX(100);
userNameTxt.setWidth(200);
enterSpeedTxt.setText("Enter Speed");
enterSpeedTxt.setX(-500);
enterSpeedTxt.setY(100);
userSpeedTxt.setX(-400);
userSpeedTxt.setY(700);
userSpeedTxt.setWidth(200);
configButton.setWidth(400);
configButton.setText("Back to Game");
configButton.setY(1000);
and as you can see the speed option doesn't even show up on the screen. And I keep playing with setX, setY, setWidth options but it keeps getting messy.
Is it wrong to do the layout directly through the source code?
I have a two activities but only a layout xml file for one of them. Am I supposed to create another xml file in res/layout section for the menu activity?
I just don't understand when I use the source code and when I should use the layout...
Thanks in advance
Is it wrong to do the layout directly through the source code?
No it's not. But you are strongly advised to use xml layout because it gives you a visual presentation of what you are trying before runtime.
I have a two activities but only a layout xml file for one of them. Am I supposed to create another xml file in res/layout section for the menu activity?
Yes. You have to create an xml file for every activity in your app.
I just don't understand when I use the source code and when I should use the layout...
You should use xml as much as you can. You should only use Java code to set layout attributes when it has to change at runtime. E.g populating a ListView with text from an database or a web service.
Use this code in you java file
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
LinearLayout ll = new LinearLayout(this);
LinearLayout.LayoutParams layout_params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams box_params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 40);
LinearLayout.LayoutParams btn_params = new LinearLayout.LayoutParams(120, 40);
ll.setOrientation(LinearLayout.VERTICAL);
EditText et1 = new EditText(MainActivity.this);
et1.setHint("Enter User name");
EditText et2 = new EditText(MainActivity.this);
et2.setHint("Enter speed");
Button btn= new Button(MainActivity.this);
btn.setText("Back to Game");
btn.setGravity(Gravity.CENTER_HORIZONTAL);
ll.addView(et1, box_params);
ll.addView(et2, box_params);
ll.addView(btn, btn_params);
ll.setGravity(Gravity.CENTER);
rl.addView(ll,layout_params );
}
}
You will get required output but it is prefer to use xml file until you don't need dynamic changes in your UI. It is easy to maintain code and design screens using xml files and if there is no need of large dynamic changes you should use xml files. Through Xml files you can check you code on different resolution through graphical representation of code. You can create a xml file in layout folder and can link it to your activity. It is easy, time saving and provide you more accurate designs... :) Please check screen shot for dynamic creation of your required design.
Happy Coding !!!