更改Android中每个屏幕的图像背景

时间:2015-01-29 15:36:10

标签: java android android-studio drawable setbackground

我还是新的android,所以请尽可能小心解释,最好用示例代码。我正在研究事实查找器应用程序,它只是在单击按钮时显示一个新事实。每次显示新事实时,弹出相同的屏幕,但颜色不同。我这样做是通过将数据中的事实存储在数组中,然后将颜色数存储在数组中,然后随机匹配它们。如果弹出特定的(不随机颜色)事实,如何将背景更改为自定义图像。我知道设置第一个图像的背景我们可以简单地写android:background =“@ drawable /(name_of_image)”我将如何去做这个,这是我到目前为止编写的代码:不同的颜色存储在一个名为ColorWheel的新类

ColorWheel

package com.example.kharl.funfacts;

import android.graphics.Color;

import java.util.Random;

/**
 * Created by Kharl on 1/27/2015.
 */
public class ColorWheel {
    //Member variable (propoerties about the object)
    public String[] mColors = {
            "#39add1", // light blue
            "#3079ab", // dark blue
            "#c25975", // mauve
            "#e15258", // red
            "#f9845b", // orange
            "#838cc7", // lavender
            "#7d669e", // purple
            "#53bbb4", // aqua
            "#51b46d", // green
            "#e0ab18", // mustard
            "#637a91", // dark gray
            "#f092b0", // pink
            "#b7c0c7"  // light gray
    };


    String color="";
    //Method (abilities:things the object can do)
    public int getColor(){

        //Randomly select a fact
        Random randomGenerator =new Random(); // construct a new random generator
        int randomNumber =randomGenerator.nextInt(mColors.length);
        color = mColors[randomNumber];
        int colorAsInt = Color.parseColor(color);

        return colorAsInt;






    }
}

FunFactsActivity.Java

public class FunFactsActivity extends Activity {

        public static final String TAG =FunFactsActivity.class.getSimpleName();

        private FactBook mFactBook = new FactBook();
        private ColorWheel mColorWheel= new ColorWheel();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_fun_facts);
            // Declare our view variables and assign them the views from the layout file
           final TextView factLabel = (TextView) findViewById(R.id.factTextView);
            final Button showFactButton= (Button) findViewById(R.id.showFactButton);
            final RelativeLayout relativelayout =(RelativeLayout) findViewById(R.id.relativeLayout);
            View.OnClickListener listener= new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String fact = mFactBook.getFact();


                    //Update the label with our dynamic fact
                    factLabel.setText(fact);
                   int color = mColorWheel.getColor();
                    relativelayout.setBackgroundColor(color);
                    showFactButton.setTextColor(color);


                }
            };
            showFactButton.setOnClickListener(listener);

    //Toast.makeText(this,"YAY! our activity was created",Toast.LENGTH_LONG).show();
    Log.d(TAG, "We are Logging from the oncreate method");
        }
    }

概况

package com.example.kharl.funfacts;

import java.util.Random;

/**
 * Created by Kharl on 1/26/2015.
 */
public class FactBook {
    //Member variable (propoerties about the object)
    public String[] mFacts = {
            "Ants stretch when they wake up in the morning.",
            "Ostriches can run faster than horses.",
            "Olympic gold medals are actually made mostly of silver.",
            "You are born with 300 bones; by the time you are an adult you will have 206.",
            "It takes about 8 minutes for light from the Sun to reach Earth.",
            "Some bamboo plants can grow almost a meter in just one day.",
            "The state of Florida is bigger than England.",
            "Some penguins can leap 2-3 meters out of the water.",
            "On average, it takes 66 days to form a new habit.",
            "Mammoths still walked the earth when the Great Pyramid was being built." };


    String fact="";
    //Method (abilities:things the object can do)
    public String getFact(){

        //Randomly select a fact
        Random randomGenerator =new Random(); // construct a new random generator
        int randomNumber =randomGenerator.nextInt(mFacts.length);
        fact = mFacts[randomNumber];

    return fact;
    }
}

1 个答案:

答案 0 :(得分:1)

将ImageView添加到layout.xml文件

<ImageView
    android:id="@+id/myimage"
    android:layout_width="100dp"
    android:layout_height="118dp"
    android:layout_marginRight="8dp"
    android:maxHeight="100dp"
    android:maxWidth="100dp"
    android:scaleType="centerCrop"
    android:src="@drawable/name_of_image" />

然后当你需要改变图像时

Bitmap bmp = BitmapFactory.decodeFile(imgPath); //* you can use decode stream if it is from a URL
ImageView img = (ImageView) layout.findViewById(R.id.myimage);
 img.setImageBitmap(bmp);