How can I make an image transparent on Android?

后端 未结 12 1513
春和景丽
春和景丽 2021-01-30 19:52

I am using a linear layout and frame layout. In the linear layout I keep an image as background and in the frame layout I keep an imageView. In that imageView I give an image.

相关标签:
12条回答
  • 2021-01-30 20:19

    Try this:

    ImageView myImage = (ImageView) findViewById(R.id.myImage);
    myImage.setAlpha(127); //value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.
    

    Note: setAlpha(int) is deprecated in favor of setAlpha(float) where 0 is fully transparent and 1 is fully opaque. Use it like: myImage.setAlpha(0.5f)

    0 讨论(0)
  • 2021-01-30 20:21

    Set transparency using setAlpha(float alpha). The below code works for me were I used an alpha value in float, 0 - 1.

    • 0: Full Transparent
    • 0.5 - 50%: Transparent
    • 1: Full Opaque

      ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView); imageView.setImageResource(mResources[position]); imageView.setAlpha(.80f);

    0 讨论(0)
  • 2021-01-30 20:22

    In XML, use:

    android:background="@android:color/transparent"
    
    0 讨论(0)
  • 2021-01-30 20:25

    On newer versions of Android (post Android 4.2 (Jelly Bean) at least), the setAlpha(int value) method is depreciated. Instead, use the setAlpha(float value) method that takes a float between 0 and 1 where 0 is complete transparency and 1 is no transparency.

    0 讨论(0)
  • 2021-01-30 20:26

    If you are in an XML file, use the following to make your imageview transparent!

     android:background="@null" 
    
    0 讨论(0)
  • 2021-01-30 20:27

    For 20% transparency, this worked for me:

    Button bu = (Button)findViewById(R.id.button1);
    bu.getBackground().setAlpha(204);
    
    0 讨论(0)
提交回复
热议问题