Java 如何以编程方式更改 Android Activity 的背景图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22603153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to programmatically change the background image of an Android Activity
提问by Niamh Doyle
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.
我已经能够更改活动背景的颜色(请参阅此帖子)。现在的要求是对背景图像做同样的事情。我的意思是我可以单击一个按钮,选择一个选项并将当前的活动背景图像更改为新的。
Here is what I have done:
这是我所做的:
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();
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.
我现在不知道如何解决这个问题。你能帮忙吗?非常感谢。
采纳答案by Hamid Shatu
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...
当您在选择后设置背景时,Dialog
您将获得资源 IDR.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;
But in onCreate()
method you are just passing the resource id as below...
但是在onCreate()
方法中,您只是传递资源 ID 如下...
wvContent.setBackgroundColor(getSelectedItem());
where, getSelectedItem()
returns an int
value which is a resource id.
其中,getSelectedItem()
返回一个int
作为资源 ID的值。
Now, set background drawable as follows in onCreate()
method...
现在,在onCreate()
方法中按如下方式设置背景可绘制...
wvContent.setBackgroundColor(0);
BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(getSelectedItem());
bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
wvContent.setBackgroundDrawable(bg);
You can update background from SDCard as follows...
您可以按如下方式从 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);
回答by mohammad rababah
add this to your activity.xml:
将此添加到您的activity.xml:
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/iso"
android:id="@+id/background"
android:scaleType="fitXY"
/>
add this to activity.java:
将此添加到activity.java:
ImageView layout = (ImageView) findViewById(R.id.background);
layout.setBackgroundResource(R.drawable.iso);
回答by prakash
webView.setBackgroundColor(0);
WebView.setBackgroundResource(R.drawable.yourImage);
use this above code, may this help you....
使用上面的代码,这可以帮助你......