web-dev-qa-db-ja.com

アプリの背景画像を繰り返す方法

私は自分のアプリに背景画像を設定しましたが、背景画像は小さいので、それを繰り返して画面全体に表示したいです。私は何をすべきか?

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

さて、これが私が私のアプリに持っているものです。スクロール中に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">
//
417
yanchenko

それを行うためのdrawable xmlにはプロパティがあります。 Android:tileMode = "繰り返し"

このサイトを参照してください。 http://androidforbeginners.blogspot.com/2010/06/how-to-tile-background-image-in-Android.html

167
Laszlo Lugosi

これは、背景画像の繰り返しの純粋な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に保存する必要があります。

67
plowman

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