java – 如何以编程方式更改Android Activity的背景图像

前端之家收集整理的这篇文章主要介绍了java – 如何以编程方式更改Android Activity的背景图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我能够改变活动背景的颜色(见 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();
}

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

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

解决方法

从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()方法中设置background 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);

猜你在找的Android相关文章