Bitmap XML和BitmapDrawable应用实例

来源:互联网 发布:sniffer捕获别人的数据 编辑:程序博客网 时间:2024/05/29 03:10

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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}

这里值得注意的有应用性的是android:tileMode这一属性。它的四个值效果图如下:

repeatmirrorclampdisabled

再来解释下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);



原创粉丝点击