2010-04-25 100 views
294

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

<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"> 

回答

401

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

抽拉/ 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" /> 

值/ 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"> 
// 
+1

尝试用这种过于:机器人:重力=“clip_horizo​​ntal” ---它避免图像变形 – 2011-09-30 21:37:02

+2

我已经试过这一点,但看到的只是单一的瓷砖延伸到所有的屏幕:( – 2012-03-02 19:24:17

+0

如果我有一个'ScrollView'并放置一个背景重复,并且我有一个很长的列表,当'ScrollView'变得很长时,我不会遇到OutOfMemory异常的问题吗? – AndreiBogdan 2012-08-31 09:40:17

67

这里是一个纯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 /抽拉/ bg_image.png。

+4

Shader是什么? – 2012-03-01 01:15:22

+6

android.graphics.Shader – 2012-05-15 22:19:17

+0

如果我有一个'ScrollView'并且放置一个背景来重复它,并且我有一个很长的列表,那么当ScrollView变得很长时,我会不会遇到OutOfMemory异常的问题? – AndreiBogdan 2012-08-31 09:41:30

159

在drawable xml中有一个属性可以做到这一点。 机器人:TILEMODE =“重复”

浏览此网站: http://androidforbeginners.blogspot.com/2010/06/how-to-tile-background-image-in-android.html

+32

我真的不知道这是如此低的评价。牧群本能?这是平铺背景的本地实现 – 2012-04-08 13:00:08

+4

这一个像魅力一样工作。此外,这似乎是正确的方式来做到这一点。 – JCasso 2012-07-11 03:02:09

+3

我同意这应该是被接受的答案。这真的很简单,完美的作品! – 2013-02-11 06:07:48

3
// 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);*/ 
14

扩大对农夫回答,这里是用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); 
}