如何以编程方式更改一个Android活动的背景图像 [英] How to programmatically change the background image of an Android Activity

查看:253
本文介绍了如何以编程方式更改一个Android活动的背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够改变一个活动的背景颜色(参见<一个href=\"http://stackoverflow.com/questions/22574678/how-to-change-background-colour-of-an-android-activity\">this帖子)。现在一个要求是做与背景图像相同的。我的意思是我可以点击一个按钮,选择一个选项和当前活动的背景图像更改为新的。

I have been able to change colour of an activity background (see this post). Now a requirement is to do the same with background image. I mean I can click a button, select an option and change the current Activity background image to the new one.

下面是我做了什么:

private SharedPreferences prefs;    
private static final String SELECTED_ITEM = "SelectedItem"; 
private Editor sharedPrefEditor;

btnchangeColor = (ImageButton) findViewById(R.id.btnchangeColor);
btnchangeColor.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    final CharSequence[] items={getString(R.string.default),getString(R.string.pix1), getString(R.string.pix2))};
    AlertDialog.Builder builder = new AlertDialog.Builder(
            ContentView.this);

    builder.setTitle((getResources().getString(R.string.color_switch)));
    builder.setPositiveButton((R.string.ok), new DialogInterface.OnClickListener() { 

        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    builder.setSingleChoiceItems(items, getSelectedItem(), new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {                
            wvContent = (WebView) findViewById(R.id.wvContent);             
            int bg_color=0;

            if(getString(R.string.default).equals(items[which]))
            {                   
                wvContent.setBackgroundColor(0);
                BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.default);
                bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
                wvContent.setBackgroundDrawable(bg);                    
                bg_color=R.drawable.default; 
            }
            else if(getString(R.string.pix1).equals(items[which]))
            {
                wvContent.setBackgroundColor(0);
                BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.pix1);
                bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
                wvContent.setBackgroundDrawable(bg);                    
                bg_color=R.drawable.pix1;
                }
            else if(getString(R.string.pix2).equals(items[which]))
            {
                wvContent.setBackgroundColor(0);
                BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.pix2);
                bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
                wvContent.setBackgroundDrawable(bg);                    
                bg_color=R.drawable.pix2;                   
                }               
            saveSelectedItem(bg_color);
        }
    });
    builder.show();

保存更改并使用以下code加载:

Changes are saved and loaded using the following code:

//OnCreate
wvContent = (WebView) findViewById(R.id.wvContent); 
wvContent.setBackgroundColor(getSelectedItem());
...
private int getSelectedItem() {
    if (prefs == null) {
        prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
    }
    return prefs.getInt(SELECTED_ITEM, -1);
}

private void saveSelectedItem(int which) {
    if (prefs == null) {
        prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
    }
    sharedPrefEditor = prefs.edit();
    sharedPrefEditor.putInt(SELECTED_ITEM, which);
    sharedPrefEditor.commit();
}

该活动的背景图像不会当它从对话框列表中选择,但变化不会被保存,加载时的活动重新启动下一次改变。

The Activity background image does change when it is selected from the Dialog list, BUT the change is not saved and loaded next time when the activity is relaunched.

我不知道现在该怎么解决这个问题。可以请你帮忙吗?非常感谢。

I have no idea now how to solve this problem. Can you please help? Many thanks.

推荐答案

当您从对话框中选择那么你所得到的资源ID之后设置背景 R.drawable.pix2 和检索 BitmapDrawable 如下...

When you are setting background after selecting from Dialog then you are getting the resource id R.drawable.pix2 and retrieving the BitmapDrawable as follows...

wvContent.setBackgroundColor(0);
BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.pix2);
bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
wvContent.setBackgroundDrawable(bg);                    
bg_color=R.drawable.pix2;

的onCreate()方法,你只是过客资源ID如下...

But in onCreate() method you are just passing the resource id as below...

wvContent.setBackgroundColor(getSelectedItem());

其中, getSelectedItem()返回 INT 值,它是一个资源ID。

where, getSelectedItem() returns an int value which is a resource id.

现在,设置背景绘制在的onCreate如下()方法...

Now, set background drawable as follows in onCreate() method...

wvContent.setBackgroundColor(0);
BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(getSelectedItem());
bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
wvContent.setBackgroundDrawable(bg);

您可以从SD卡更新的背景如下...

You can update background from SDCard as follows...

    String pathName = Environment.getExternalStorageDirectory().getPath() + "/folder/" + "image.jpg";
    Resources res = getResources(pathName);
    Bitmap bitmap = BitmapFactory.decodeFile(pathName);
    BitmapDrawable backgroundDrawable = new BitmapDrawable(res, bitmap);
    wvContent.setBackgroundDrawable(backgroundDrawable);

这篇关于如何以编程方式更改一个Android活动的背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆