如何使android应用程序的背景图像重复

我已经在我的应用程序中设置了一个背景图像,但背景图像很小,我希望它被重复,并填写整个屏幕。 我该怎么办?

<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/bg" android:tileMode="repeat"> 

好的,这是我的应用程序。 它包括一个黑客来防止ListView在滚动时变黑。

drawable / app_background.xml

 <?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/actual_pattern_image" android:tileMode="repeat" /> 

values / styles.xml

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="app_theme" parent="android:Theme"> <item name="android:windowBackground">@drawable/app_background</item> <item name="android:listViewStyle">@style/TransparentListView</item> <item name="android:expandableListViewStyle">@style/TransparentExpandableListView</item> </style> <style name="TransparentListView" parent="@android:style/Widget.ListView"> <item name="android:cacheColorHint">@android:color/transparent</item> </style> <style name="TransparentExpandableListView" parent="@android:style/Widget.ExpandableListView"> <item name="android:cacheColorHint">@android:color/transparent</item> </style> </resources> 

AndroidManifest.xml

 // <application android:theme="@style/app_theme"> // 

在可绘制的XML中有一个属性来做到这一点。 机器人:TILEMODE = “回头客”

看到这个网站: http : //androidforbeginners.blogspot.com/2010/06/how-to-tile-background-image-in-android.html

这里是一个纯Java实现的背景图片重复:

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.bg_image); BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp); bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); LinearLayout layout = new LinearLayout(this); layout.setBackgroundDrawable(bitmapDrawable); } 

在这种情况下,我们的背景图像必须存储在res / drawable / bg_image.png中。

在plowman的答案上扩展,这里是用java改变背景图像的不被弃用的版本。

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.texture); BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(),bmp); bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); setBackground(bitmapDrawable); } 
 // Prepared By Muhammad Mubashir. // 26, August, 2011. // Chnage Back Ground Image of Activity. package com.ChangeBg_01; import com.ChangeBg_01.R; import android.R.color; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.ImageView; import android.widget.TextView; public class ChangeBg_01Activity extends Activity { TextView tv; int[] arr = new int[2]; int i=0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView)findViewById(R.id.tv); arr[0] = R.drawable.icon1; arr[1] = R.drawable.icon; // Load a background for the current screen from a drawable resource //getWindow().setBackgroundDrawableResource(R.drawable.icon1) ; final Handler handler=new Handler(); final Runnable r = new Runnable() { public void run() { //tv.append("Hello World"); if(i== 2){ i=0; } getWindow().setBackgroundDrawableResource(arr[i]); handler.postDelayed(this, 1000); i++; } }; handler.postDelayed(r, 1000); Thread thread = new Thread() { @Override public void run() { try { while(true) { if(i== 2){ //finish(); i=0; } sleep(1000); handler.post(r); //i++; } } catch (InterruptedException e) { e.printStackTrace(); } } }; } } /*android:background="#FFFFFF"*/ /* ImageView imageView = (ImageView) findViewById(R.layout.main); imageView.setImageResource(R.drawable.icon);*/ // Now get a handle to any View contained // within the main layout you are using /* View someView = (View)findViewById(R.layout.main); // Find the root view View root = someView.getRootView();*/ // Set the color /*root.setBackgroundColor(color.darker_gray);*/