我尝试以编程方式为此XML drawable(hexgrid.xml)设置透明度:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/hexgrid1"
android:tileMode="repeat" />
使用它的RelativeLayout的一部分:
<RelativeLayout
android:id="@+id/boardf"
android:background="@drawable/hexgrid"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
这是代码:
boardf = (RelativeLayout) findViewById(R.id.boardf);
Drawable background = boardf.getBackground();
background.setAlpha(Math.round(255*hexgridtrans/100f));
在我的手机上(API 19)它运行正常,但在我的平板电脑(API 15)上可悲的是它没有。
hexgridtrans
的值为25(并且64作为setAlpha(int alpha)
方法的参数传递),这就是我得到的:
在左侧,alpha设置为25%并按预期工作,而在右侧 - 相同的代码和相同的值 - 或多或少为6%。
我在这里做错了什么?
我知道Drawable.mutate()
,但在此图之前我没有使用任何可绘制的东西,尽管如此,我已经尝试了它并且它没有&#39;这里有所不同。
Drawable.setAlpha(int alpha)
在两个设备上正常工作,所以我怀疑这个问题可能与使用XML drawable有关。