我搜索了很多但没找到任何东西。我想为布局制作背景,但不是用颜色或渐变填充它我想用纹理填充它并重复它。我尝试了这个,但它没有用。你能告诉我一个解决方案。
card_texture.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/paper_texture"
android:tileMode="repeat"
/>
card_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color = "@android:color/transparent"/>
<stroke android:width="2dp"
android:color="@color/text_color"/>
<corners
android:radius="20dp"
/>
</shape>
card_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/card_texture" >
<corners
android:radius = "20dp"/>
</item>
<item android:drawable="@drawable/card_shape" />
</layer-list>
这就是我在布局android:background="@drawable/card_background"
我得到这样的东西。但我希望纹理的角(标有红色)是透明的,并且适合黑色边框
答案 0 :(得分:3)
我称之为 res / drawable / bg_tile.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/tile"
android:tileMode="mirror"
android:dither="true"
android:antialias="true"
/>
我用它像:
android:background="@drawable/bg_tile"
很明显,我准备了一个水平和垂直镜像的瓷砖(大多数瓷砖都是这样的有趣效果)并称之为 res / drawable-a-dpi-resolution / tile.png
a-dpi-resolution是ldpi,mdpi,...,xxxhdpi
如果你想使用复合背景(即渐变和平铺位图 - 但位图必须以某种方式透明,或者它将覆盖渐变),你可以创建一个LayerList背景(以便不同的& #34;层和#34;是另一个在另一个上面的
为此,您需要添加另一个背景文件,即: res / drawable / bg_radial.xml :
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<gradient
android:startColor="#@color/bar_int_md"
android:centerColor="#@color/bar_mid_md"
android:endColor="#@color/bar_ext_md"
android:gradientRadius="480"
android:type="radial"
/>
</shape>
最后是LayerList,它们将它们组合在一起( res / drawable / bg.xml ):
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bg_radial" />
<item android:drawable="@drawable/bg_tile" />
</layer-list>
请注意,首先绘制第一个项目。下一个项目是彼此在上面绘制的,是最后一个&#34;最上面的&#34;。
所以,最后你会使用它:
android:background="@drawable/bg"
尼斯!
答案 1 :(得分:1)
使图像角落使用以下功能:
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
onCreate
中的执行此操作:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu)
View rl = (View) findViewById(R.id.root);//get the reference of the layout that you set as background
LayerDrawable layer = (LayerDrawable) rl.getBackground();
BitmapDrawable bg = (BitmapDrawable) layer.getDrawable(1);
Drawable d =new BitmapDrawable(getRoundedCornerBitmap(bg.getBitmap(),10));//pass corner radius
layer.setDrawableByLayerId(1, d);
rl.setBackgroundDrawable(d);
}