bitmap_xml.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:src="@drawable/beauty1"
android:tileMode="disabled" >
</bitmap>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@drawable/bitmap_xml" >
</RelativeLayout>
MainActivity.java
package com.sean.bitmapxml;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
这里值得注意的有应用性的是android:tileMode这一属性。它的四个值效果图如下:
repeat
mirror
clamp
disabled
再来解释下android:dither的作用——图像的抖动处理,当每个颜色值以低于8位表示时,对应图像做抖动处理可以实现在可显示颜色总数比较低(比如256色)时还保持较好的显示效果。
以上是用bitmap XML应用bitmap资源,下面演示了代码实现,则要应用到BitmapDrawable:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.beauty1);
BitmapDrawable bd = new BitmapDrawable(bitmap);
bd.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
bd.setDither(true);
view.setBackgroundDrawable(bd);