我有一个不透明的图案(10% 的不透明度),需要与一些颜色组合使用。
能否将背景颜色设置为图片?
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#FF00ffff" />
</shape>
</item>
<item>
<bitmap
android:src="@drawable/your_tiled_drawable"
android:tileMode="repeat" />
</item>
</layer-list>
将它保存在res/drawables
目录中,并将其用作背景。
不幸的是,如果您的应用程序更加复杂,有时候瓷砖模式会不明原因地被取消设置。如果您遇到这种情况,就需要在Java中重新添加瓷砖模式。
我通常仍然在xml中设置它,然后做一些像这样的事情:
View v = findViewById(R.id.my_view);
LayerDrawable lld = (LayerDrawable) v.getBackground();
BitmapDrawable tiled = (BitmapDrawable) lld.getDrawable(1);
tiled.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
可以的。
你可以使用 ImageView
。将其背景颜色设置为半透明图像,然后将半透明图像设置为其源。
如果你想对一个布局进行此操作,则需要扩展该布局,并在重写的 onDraw() 方法中绘制背景颜色并与半透明图像混合。
Paint p = new Paint();
p.setXfermode(new PorterDuffXfermode(Mode.MULTIPLY));