Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. package com.app.video.ui;
  2.  
  3.  
  4. import android.content.Context;
  5. import android.os.Build;
  6. import android.view.Gravity;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.WindowManager;
  10. import android.widget.FrameLayout;
  11. import android.widget.ImageButton;
  12. import android.widget.MediaController;
  13.  
  14. import com.app.video.R;
  15.  
  16. import java.lang.reflect.Field;
  17.  
  18. /**
  19. * Custom MediaController that fixes issue with controls appearing offset on pre 4.3 devices
  20. * and shows how to add additional functionality such as fullscreen button
  21. */
  22. public class CustomMediaController extends MediaController {
  23.  
  24. public static interface OnMediaControllerInteractionListener {
  25. void onRequestFullScreen();
  26. }
  27.  
  28. Context mContext;
  29. private OnMediaControllerInteractionListener mListener;
  30.  
  31. public CustomMediaController(Context context) {
  32. super(context);
  33. mContext = context;
  34. }
  35.  
  36. public void setListener(OnMediaControllerInteractionListener listener) {
  37. mListener = listener;
  38. }
  39.  
  40. @Override
  41. public void setAnchorView(View view) {
  42. super.setAnchorView(view);
  43.  
  44. FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams(
  45. LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  46. frameParams.gravity = Gravity.RIGHT|Gravity.TOP;
  47.  
  48.  
  49. ImageButton fullscreenButton = (ImageButton) LayoutInflater.from(mContext)
  50. .inflate(R.layout.fullscreen_button, null);
  51.  
  52. fullscreenButton.setOnClickListener(new OnClickListener() {
  53. public void onClick(View v) {
  54. if(mListener != null) {
  55. mListener.onRequestFullScreen();
  56. }
  57. }
  58. });
  59.  
  60. addView(fullscreenButton, frameParams);
  61. }
  62.  
  63. @Override
  64. public void show(int timeout) {
  65. super.show(timeout);
  66. // fix pre Android 4.3 strange positioning when used in Fragments
  67. int currentapiVersion = android.os.Build.VERSION.SDK_INT;
  68. if (currentapiVersion < Build.VERSION_CODES.JELLY_BEAN_MR2) {
  69. try {
  70. Field field1 = MediaController.class.getDeclaredField("mAnchor");
  71. field1.setAccessible(true);
  72. View mAnchor = (View)field1.get(this);
  73.  
  74. Field field2 = MediaController.class.getDeclaredField("mDecor");
  75. field2.setAccessible(true);
  76. View mDecor = (View)field2.get(this);
  77.  
  78. Field field3 = MediaController.class.getDeclaredField("mDecorLayoutParams");
  79. field3.setAccessible(true);
  80. WindowManager.LayoutParams mDecorLayoutParams = (WindowManager.LayoutParams)field3.get(this);
  81.  
  82. Field field4 = MediaController.class.getDeclaredField("mWindowManager");
  83. field4.setAccessible(true);
  84. WindowManager mWindowManager = (WindowManager)field4.get(this);
  85.  
  86. // NOTE: this appears in its own Window so co-ordinates are screen co-ordinates
  87. int [] anchorPos = new int[2];
  88. mAnchor.getLocationOnScreen(anchorPos);
  89.  
  90. // we need to know the size of the controller so we can properly position it
  91. // within its space
  92. mDecor.measure(MeasureSpec.makeMeasureSpec(mAnchor.getWidth(), MeasureSpec.AT_MOST),
  93. MeasureSpec.makeMeasureSpec(mAnchor.getHeight(), MeasureSpec.AT_MOST));
  94.  
  95. mDecor.setPadding(0,0,0,0);
  96.  
  97. WindowManager.LayoutParams p = mDecorLayoutParams;
  98. p.verticalMargin = 0;
  99. p.horizontalMargin = 0;
  100. p.width = mAnchor.getWidth();
  101. p.gravity = Gravity.LEFT|Gravity.TOP;
  102. p.x = anchorPos[0];// + (mAnchor.getWidth() - p.width) / 2;
  103. p.y = anchorPos[1] + mAnchor.getHeight() - mDecor.getMeasuredHeight();
  104. mWindowManager.updateViewLayout(mDecor, mDecorLayoutParams);
  105.  
  106.  
  107. } catch (Exception e) {
  108. e.printStackTrace();
  109. }
  110. }
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement