How can I make an image transparent on Android?

后端 未结 12 1528
春和景丽
春和景丽 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:31

    Set an id attribute on the ImageView:

    In your code where you wish to hide the image, you'll need the following code.

    First, you'll need a reference to the ImageView:

    ImageView myImage = (ImageView) findViewById(R.id.myImage);
    

    Then, set Visibility to GONE:

    myImage.setVisibility(View.GONE);
    

    If you want to have code elsewhere that makes it visible again, just set it to Visible the same way:

    myImage.setVisibility(View.VISIBLE);
    

    If you mean "fully transparent", the above code works. If you mean "partially transparent", use the following method:

    int alphaAmount = 128; // Some value 0-255 where 0 is fully transparent and 255 is fully opaque
    myImage.setAlpha(alphaAmount);
    

提交回复
热议问题