如何以编程方式更改Android活动的背景图像

时间:2014-03-24 07:17:09

标签: java android android-layout sharedpreferences

我已经能够改变活动背景的颜色(参见this post)。现在要求对背景图像做同样的事情。我的意思是我可以单击一个按钮,选择一个选项并将当前活动背景图像更改为新的。

这就是我所做的:

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();

使用以下代码保存和加载更改:

//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();
}

从对话框列表中选择活动背景图像时会发生更改,但下次重新启动活动时不会保存和加载更改。

我现在不知道如何解决这个问题。你能帮忙吗?非常感谢。

3 个答案:

答案 0 :(得分:12)

Dialog中选择后设置后台时,您将获得资源ID R.drawable.pix2并检索BitmapDrawable,如下所示...

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,如下所示......

wvContent.setBackgroundColor(getSelectedItem());

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

现在,在onCreate()方法...

中设置背景drawable,如下所示
wvContent.setBackgroundColor(0);
BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(getSelectedItem());
bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
wvContent.setBackgroundDrawable(bg);

您可以按照以下步骤从SDCard更新背景...

    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);

答案 1 :(得分:4)

将此添加到您的activity.xml:

<ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/iso"
        android:id="@+id/background"
        android:scaleType="fitXY"
        />

将此添加到activity.java:

ImageView layout = (ImageView) findViewById(R.id.background);
            layout.setBackgroundResource(R.drawable.iso);

答案 2 :(得分:0)

webView.setBackgroundColor(0);
WebView.setBackgroundResource(R.drawable.yourImage);

使用上面的代码,这可能对你有帮助....