I'm trying to put a video as background in my application. However, when I put the video, the size of it, very strange, rises over time and is not top. Anyone know how I can fix this? Leave it in a legal proportion, just that it is cut like sides ... something of the kind
My code:
Activity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/home_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<icon.com.videobackground.VideoBackground
android:id="@+id/introVideoView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:hint="Enter Email"
android:padding="10dp"
android:gravity="center"
android:background="@null" />
<EditText
android:id="@+id/pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/edit"
android:hint="Enter Password"
android:padding="10dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:background="@null" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:textColor="@android:color/holo_orange_dark"
android:padding="10dp"
android:layout_marginTop="5dp"
android:textSize="25sp"
android:background="@android:color/transparent"
android:layout_below="@+id/pass"/>
</RelativeLayout>
</RelativeLayout>
VideoBackground.java
public class VideoBackground extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "INTRO_VIDEO_CALLBACK";
private MediaPlayer mp;
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public VideoBackground(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
public VideoBackground(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public VideoBackground(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public VideoBackground(Context context) {
super(context);
init();
}
private void init() {
mp = new MediaPlayer();
getHolder().addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.video_bgg); // your intro video file placed in raw folder named as intro.mp4
try {
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
afd.getDeclaredLength());
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
android.view.ViewGroup.LayoutParams lp = getLayoutParams();
int screenHeight = getHeight();
int screenWidth = getWidth();
// this plays in full screen video
lp.height = screenHeight;
lp.width = screenWidth;
setLayoutParams(lp);
mp.setDisplay(getHolder());
mp.setLooping(false);
mp.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mp.stop();
}
}