diff --git a/res/layout/video_preference.xml b/res/layout/video_preference.xml index a6fde768c9a..5f7116dd92e 100644 --- a/res/layout/video_preference.xml +++ b/res/layout/video_preference.xml @@ -15,13 +15,22 @@ --> + + + android:layout_height="wrap_content" + android:layout_alignTop="@id/video_background" + android:layout_centerHorizontal="true" /> \ No newline at end of file diff --git a/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java b/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java index 748dc15e40d..e6a8c1e829c 100644 --- a/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java +++ b/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java @@ -30,20 +30,24 @@ import android.support.v7.preference.Preference; import android.support.v7.preference.PreferenceScreen; import android.support.v7.preference.PreferenceViewHolder; import android.view.Display; -import android.view.Gravity; +import android.view.ViewTreeObserver; +import android.view.ViewTreeObserver.OnGlobalLayoutListener; import android.view.WindowManager; +import android.widget.ImageView; import android.widget.MediaController; -import android.widget.RelativeLayout; +import android.widget.RelativeLayout.LayoutParams; import android.widget.VideoView; import com.android.internal.logging.MetricsProto.MetricsEvent; import com.android.settings.R; import com.android.settings.widget.ToggleSwitch; import com.android.settings.widget.ToggleSwitch.OnBeforeCheckedChangeListener; -public class ToggleScreenMagnificationPreferenceFragment - extends ToggleFeaturePreferenceFragment { +public class ToggleScreenMagnificationPreferenceFragment extends ToggleFeaturePreferenceFragment { protected class VideoPreference extends Preference { + private ImageView mVideoBackgroundView; + private OnGlobalLayoutListener mLayoutListener; + public VideoPreference(Context context) { super(context); } @@ -51,40 +55,19 @@ public class ToggleScreenMagnificationPreferenceFragment @Override public void onBindViewHolder(PreferenceViewHolder view) { super.onBindViewHolder(view); + Resources res = getPrefContext().getResources(); + final int backgroundAssetWidth = res.getDimensionPixelSize( + R.dimen.screen_magnification_video_background_width); + final int videoAssetWidth = res + .getDimensionPixelSize(R.dimen.screen_magnification_video_width); + final int videoAssetHeight = res + .getDimensionPixelSize(R.dimen.screen_magnification_video_height); + final int videoAssetMarginTop = res.getDimensionPixelSize( + R.dimen.screen_magnification_video_margin_top); view.setDividerAllowedAbove(false); view.setDividerAllowedBelow(false); - final RelativeLayout background = - (RelativeLayout) view.findViewById(R.id.video_background); - VideoView videoView = (VideoView) view.findViewById(R.id.video); - - // Hacky adjustment for using VideoView in recycle view and positioning - // it on the background image - Resources res = getPrefContext().getResources(); - final int backgroundWidth = res.getDimensionPixelSize( - R.dimen.screen_magnification_video_background_width); - final int backgroundHeight = res.getDimensionPixelSize( - R.dimen.screen_magnification_video_background_height); - final int videoWidth = res.getDimensionPixelSize( - R.dimen.screen_magnification_video_width); - final int videoHeight = res.getDimensionPixelSize( - R.dimen.screen_magnification_video_height); - final int videoMarginTop = res.getDimensionPixelSize( - R.dimen.screen_magnification_video_margin_top); - final int screenWidth = getScreenWidth(getPrefContext()); - - RelativeLayout.LayoutParams videoLp = new RelativeLayout.LayoutParams( - screenWidth * videoWidth / backgroundWidth, - screenWidth * videoHeight / backgroundWidth); - videoLp.setMargins(0, screenWidth * videoMarginTop / backgroundWidth, 0, 0); - videoView.setLayoutParams(videoLp); - - RelativeLayout.LayoutParams backgroundLp = new RelativeLayout.LayoutParams( - screenWidth, - screenWidth * backgroundHeight / backgroundWidth); - background.setLayoutParams(backgroundLp); - background.setBackgroundResource( - R.drawable.accessibility_screen_magnification_background); - background.setGravity(Gravity.CENTER_HORIZONTAL); + mVideoBackgroundView = (ImageView) view.findViewById(R.id.video_background); + final VideoView videoView = (VideoView) view.findViewById(R.id.video); // Loop the video. videoView.setOnPreparedListener(new OnPreparedListener() { @@ -100,7 +83,31 @@ public class ToggleScreenMagnificationPreferenceFragment R.raw.accessibility_screen_magnification))); // Make sure video controls (e.g. for pausing) are not displayed. videoView.setMediaController(null); - videoView.start(); + + // LayoutListener for adjusting the position of the VideoView on the background image. + mLayoutListener = new OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + final int backgroundViewWidth = mVideoBackgroundView.getWidth(); + + LayoutParams videoLp = (LayoutParams) videoView.getLayoutParams(); + videoLp.width = videoAssetWidth * backgroundViewWidth / backgroundAssetWidth; + videoLp.height = videoAssetHeight * backgroundViewWidth / backgroundAssetWidth; + videoLp.setMargins(0, + videoAssetMarginTop * backgroundViewWidth / backgroundAssetWidth, 0, 0); + videoView.setLayoutParams(videoLp); + videoView.invalidate(); + videoView.start(); + } + }; + + mVideoBackgroundView.getViewTreeObserver().addOnGlobalLayoutListener(mLayoutListener); + } + + @Override + protected void onPrepareForRemoval() { + mVideoBackgroundView.getViewTreeObserver() + .removeOnGlobalLayoutListener(mLayoutListener); } }