【问题标题】:How to make an app's background image repeat如何使应用程序的背景图像重复
【发布时间】:2011-02-12 00:14:24
【问题描述】:

我在我的应用程序中设置了背景图片,但背景图片很小,我希望它可以重复并填满整个屏幕。我该怎么办?

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

【问题讨论】:

    标签: android image layout


    【解决方案1】:

    好的,这就是我的应用程序中的内容。它包括防止ListViews 在滚动时变黑的技巧。

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

    【讨论】:

    • 也试试这个:android:gravity="clip_horizo​​ntal" ---它可以避免图像变形
    • 我试过这个,但只看到一个瓷砖拉伸到整个屏幕:(
    • 如果我有一个ScrollView 并在其上放置一个重复的背景并且我有一个很长的列表,当ScrollView 变得很长时,我不会遇到 OutOfMemory 异常的问题吗?
    • 要记住的一点是,您应该有文件夹 drawable-hdpi、drawable-mdpi 和 drawable-ldpi,您需要将此 backrepeat.xml 文件和相关图像添加到每个文件夹这些是为了在高、中、低 dpi(每英寸点数)屏幕尺寸中实现此功能。
    • @sabertababaeyazdi 您只需要这些文件夹中的图像。 XML 可以放在drawable(没有-*dpi)文件夹中。
    【解决方案2】:

    drawable xml 中有一个属性可以做到这一点。 android:tileMode="重复"

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

    【讨论】:

    • 我真的不知道这个评分怎么这么低。从众本能?这是平铺背景的原生实现
    • 这个工作就像一个魅力。这似乎也是正确的做法。
    • 我同意这应该是公认的答案。它真的很简单,而且效果很好!
    • +1 文章中错误提及的只有一件事必须更正: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。您只需将引用的可绘制对象放在所有密度桶中。引用的 XML drawable 可以放在drawable 文件夹中,就足够了。
    • 这就是你所谓的专业解释
    【解决方案3】:

    这是一个纯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 中。

    【讨论】:

    • 如果我有一个ScrollView 并在其上放置一个重复的背景,并且我有一个很长的列表,当ScrollView 变得很长时,我不会遇到 OutOfMemory 异常的问题吗?
    • 为什么这不再起作用了?折旧意味着这些命令不应再使用,因为它们可能会在将来的某个时间退役。在 API 19 中,这仍然像@plowman 建议的那样工作。此外,不推荐使用 BitmapDrawable,而仅不推荐使用它的一些方法。我已经编辑了上面的代码,所以我们不必使用不推荐使用的方法。
    【解决方案4】:

    扩展 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);
    }
    

    【讨论】:

      【解决方案5】:
      // 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);*/
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-18
        • 2019-03-31
        • 2012-07-14
        • 2020-01-07
        • 2018-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多