如何从API 23+中的drawable选择器设置alpha?

时间:2016-11-03 00:12:05

标签: android android-drawable android-selector

我想将Bitmap设置为ImageButton的src,并在按下时使其变为部分透明。但是,选择器中的android:alpha属性似乎不执行任何操作

我使用@drawable/all_close

定义了StateListDrawable
<?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 -->

我将drawable设置为我的ImageButton的src:

<ImageButton
    android:src="@drawable/all_nav_close"
    android:background="@drawable/none"/>
<!-- some attributes omitted for space -->

按下时,按钮不会改变。选择器肯定在使用,因为更改state_pressed drawable会明显改变按钮:

<?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>

但是,android:alpha属性似乎对不透明度没有任何影响。该项目的构建和安装。该属性不会触发“未知属性”linter警告(和构建失败),但IDE不会建议。

这是在API 23上,使用com.android.support:appcompat-v7:25.0.0com.android.support:design:25.0.0

2 个答案:

答案 0 :(得分:1)

您尝试使用代码吗?我在我的应用程序中使用它。

GET /standards/ HTTP/1.1
Host: www.w3.org
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://www.w3.org/
Connection: keep-alive
Upgrade-Insecure-Requests: 1

但是......检查你需要什么,改变取决于你需要的,Imageview,Paint或Bitmap:

Paint paint = new Paint();
paint.setAlpha(100);
canvas.drawBitmap(bitmap, src, dst, paint);

答案 1 :(得分:0)

您好我用这种方式做了请检查, 当您进入屏幕时,请查看其工作原理

 @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);
}
退出屏幕时

 //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);
}

如果您不想使用装订,请用黄油刀替换它。

查看此example以获取更多信息。您可以了解所有内容。