How can I set transparency of MediaController back

2020-07-27 03:35发布

问题:

I want to set the transparency of MediaController background and not controls.

I tried using mediaController.setAlpha(0.6f), but it is applying transparency to controls as well like this

回答1:

Found a tricky solution, that worked for me. The idea is to set background color with desired transparency level only for the specific background layout. This will not have an impact on controls or other MediaController views:

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            public void onPrepared(MediaPlayer mediaPlayer) {
                // get media controller background layout
                LinearLayout viewGroupLevel1 = (LinearLayout)  mediaController.getChildAt(0);   
                //Set your color with desired transparency here:
                viewGroupLevel1.setBackgroundColor(getResources().getColor(R.color.transparent));
            }
        });


回答2:

try setting the background color with a transparent color instead of alpha.

For example, with color hex #99CC00:

In your colors.xml:

<resources>
  <color name="NoTransparent">#FF99CC00</color>
  <color name="AlmostTransparent">#4499CC00</color>
  <color name="FullTransparent">#0099CC00</color>
</resources>

Then in your activity:

mediaController.setBackgroundColor(getResources().getColor(R.color.AlmostTransparent));

Or in your layout file:

<MediaController
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/media_controller"
    android:background="@color/AlmostTransparent" />