您的位置:首页 > 移动开发 > Android开发

How to tile a background image in Android

2011-11-21 20:54 399 查看

How to tile a background image in Android

For one of the apps I'm working on I wanted to have a nice pixel pattern tiled behind my widgets.After a little bit of hunting around I found this tutorial, and I thought I'd clean up the lessons within and show you how.Here's the contents of my main.xml layout file,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/backrepeat"
android:gravity="center_horizontal"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>  
</LinearLayout>
which is referenced in code in the standard way like this:
main);
...// (rest of onCreate method continues here..)
Now note this line:
android:background="@drawable/backrepeat"
What's going on there?.. Glad you asked!Here's a quick screenshot of the contents of one of my drawable folders in my project:What is this
backrepeat.xml
?Well, here's the contents of that file here:
<bitmapxmlns:android="http://schemas.android.com/apk/res/android"android:src="@drawable/scale1"android:tileMode="repeat"android:dither="true" />
Can you see what's going on?Backrepeat.xml defines an instance of the BitmapDrawable class, and that class references our simple scale1.jpg, located in the drawable-hdpifolder.Simply by adding the:
<bitmapxmlns:android="http://schemas.android.com/apk/res/android"android:src="@drawable/scale1"android:tileMode="repeat"android:dither="true" />
line in bold, we are able to achieve results such as this:Easy isn't it?One thing to keep in mind is that you should have folders drawable-hdpi, drawable-mdpi & drawable-ldpi, you'll need to add this backrepeat.xml file and the relevant images to each of these to allow this functionality in high, medium and low dpi (dots per inch)screen sizes.Enjoy.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: