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.
相关文章推荐
- How to store an image file to SQliteDB in android
- How to load an ImageView by URL in Android
- Android How to Check if the App is in Background
- How to tile small texture image onto page as its background?
- How to Render Image Buffer in Android NDK Native Code
- How to make android app’s background image repeat.
- How to tile small texture image onto page as its background
- How to set background image of a component in GWTEXT
- How to fill the background with image in landscape in IOS? 如何使image水平铺满屏幕
- How to Render Image Buffer in Android NDK Native Code
- How to set background image to a LinearLayout using Android-Universal-Image-Loader ? #594
- How to store an image file to SQliteDB in android
- How to alignment the center position of the button in Android?
- How to install Android SDK in Ubuntu System
- QT11 How to set image with QLabel in Qt
- Android.HowToDesignPluginArchitectureInAndroidApp
- How to build intermediate classes.jar in android N
- Add Custom Background Image to UINavigationBar in iOS 5
- how to mount /system as read/write in android?
- Android中实现振动效果 (How to Imeplement Vibration Effect In Andoid)