一、Bitmap File:可以使用.png, .jpg, or .gif文件,路径:res/drawable/filename.png (.png, .jpg, or .gif)
一般使用,res/drawable/myimage.png,
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/myimage" />
或者代码中使用
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.myimage);
二、XML Bitmap:在res目录下创建xml文件:res/drawable/filename.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@[package:]drawable/drawable_resource"
android:antialias=["true" | "false"]
android:dither=["true" | "false"]
android:filter=["true" | "false"]
android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
"fill_vertical" | "center_horizontal" | "fill_horizontal" |
"center" | "fill" | "clip_vertical" | "clip_horizontal"]
android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />
参数名 | 作用 | 备注 |
antialias | 设置是否使用抗锯齿功能。 | |
dither | 设定是否使用图像抖动处理,会使绘制出来的图片颜色更加平滑和饱满,图像更加清晰。 | |
filter | 启用或禁用位图过滤,过滤时会使图像收缩或拉伸显示更加平滑。 | |
gravity | 设置位置。 | |
tileMode | 定义平铺模式: disabled:不使用平铺。 clamp:复制边缘色彩。 repeat:重复图片显示。 mirror:镜像显示。 |
三、代码方式:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
BitmapDrawable bd = new BitmapDrawable(bitmap);
bd.setTileModeXY(TileMode.REPEAT , TileMode.REPEAT );
bd.setDither(true);
view.setBackgroundDrawable(bd);