Reputation: 648
I want to set a Bitmap as an ImageButton's src, and have it become partially transparent while pressed. However, the android:alpha
attribute in the selector doesn't seem to do anything.
I've defined a StateListDrawable using my @drawable/all_close
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/all_close" android:alpha="0.5" />
<item android:drawable="@drawable/all_close" android:alpha="1.0" />
</selector>
<!-- have also tried alpha as 0 to 255, with the same result -->
I've set that drawable as my ImageButton's src:
<ImageButton
android:src="@drawable/all_nav_close"
android:background="@drawable/none"/>
<!-- some attributes omitted for space -->
When pressed, the button doesn't change. The selector is definitely being used, as changing the state_pressed
drawable will visibly change the button:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/all_add" android:alpha="0.5" />
<item android:drawable="@drawable/all_close" android:alpha="1.0" />
</selector>
However, the android:alpha
attribute doesn't seem to have any effect on opacity. The project builds and installs. The attribute doesn't trigger the "Unknown attribute" linter warning (and build failure), but it's not suggested by the IDE.
This is on API 23, using com.android.support:appcompat-v7:25.0.0
and com.android.support:design:25.0.0
.
Upvotes: 4
Views: 1849
Reputation: 727
The alpha attribute must be set on the <bitmap>
element not on the <item>
element. So in your case, instead of:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/all_close" android:alpha="0.5" />
<item android:drawable="@drawable/all_close" android:alpha="1.0" />
</selector>
Do it this way:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<bitmap android:src="@drawable/all_close" android:alpha="0.5"/>
</item>
<item>
<bitmap android:src="@drawable/all_close" android:alpha="1.0"/>
</item>
</selector>
Or, as you have alpha attribute set to 1.0 (which is the default value), this would also work:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<bitmap android:src="@drawable/all_close" android:alpha="0.5"/>
</item>
<item android:drawable="@drawable/all_close"/>
</selector>
Upvotes: 0
Reputation: 4220
hi i have done this using this way please check, When you enter the screen see how it work
@Override
public void centerScene(ImageView sharedElement) {
binding.sharedImage.setTranslationY(0);
setSharedImageRadius(binding.sharedImage, 0);
binding.deviceImage.setAlpha(1.0f);
binding.deviceText.setAlpha(1.0f);
binding.deviceImage.setAlpha(1.0f);
binding.deviceImage.setScaleX(1.0f);
}
when you exit the screen
//position goes from -1.0 to 0.0
@Override
public void exitScene(ImageView sharedElement, float position) {
binding.sharedImage.setTranslationY(
getResources().getDimension(R.dimen.tutorial_shared_element_translate_y)
* position);
setSharedImageRadius(binding.sharedImage, -position);
binding.deviceText.setAlpha(1 + position);
binding.deviceImage.setAlpha(1 + position);
binding.deviceImage.setScaleX(1 - position); // stretch
}
@Override
public void notInScene() {
binding.deviceImage.setAlpha(0.0f);
binding.deviceText.setAlpha(0.0f);
}
If you don't want to use binding replace it with butter knife.
Checkout this example for more information.You can understand all things.
Upvotes: 0
Reputation: 5600
Do you try with code? I use this in my apps.
Paint paint = new Paint();
paint.setAlpha(100);
canvas.drawBitmap(bitmap, src, dst, paint);
But... check what you need and change depends what you need, Imageview,Paint or Bitmap:
ImageView.setAlpha().
BitmapDrawable.setAlpha().
Upvotes: 1