应用更换背景图上小例子

可以在开发过程为了让用户有更多的选择,会让用户自己选择背景图片(注意:不是主题哦!),我写了一个小例子share一下。


效果图:






原理很简单:就是SharedPreferences保存默认背景图片,所有需要设置layout都加载背景图片。


例子结构



源代码

ChangeBackgroundActivity.java

package com.harlan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 * 更换应用背景图片
 * @author Harlan
 * 2012-5-8
 */
public class ChangeBackgroundActivity extends Activity implements
		OnClickListener {
	Button btn1, btn2, btn3, btn4;
	LinearLayout llayout;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		btn1 = (Button) findViewById(R.id.button1);
		btn2 = (Button) findViewById(R.id.button2);
		btn3 = (Button) findViewById(R.id.button3);
		btn4 = (Button) findViewById(R.id.button4);
		llayout = (LinearLayout) findViewById(R.id.llayout);
		SetBackgroundImage.setBackGround(ChangeBackgroundActivity.this, llayout);
		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
		btn3.setOnClickListener(this);
		btn4.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			Intent intent = new Intent(ChangeBackgroundActivity.this,
					Activity2.class);
			startActivity(intent);
			break;
		case R.id.button2:
			SetBackgroundImage.saveBackground(ChangeBackgroundActivity.this,
					"bg1");
			SetBackgroundImage.setBackGround(ChangeBackgroundActivity.this, llayout);
			break;
		case R.id.button3:
			SetBackgroundImage.saveBackground(ChangeBackgroundActivity.this,
					"bg2");
			SetBackgroundImage.setBackGround(ChangeBackgroundActivity.this, llayout);
			break;
		case R.id.button4:
			SetBackgroundImage.saveBackground(ChangeBackgroundActivity.this,
					"bg3");
			SetBackgroundImage.setBackGround(ChangeBackgroundActivity.this, llayout);
			break;

		}

	}
}


SetBackgroundImage.java

package com.harlan;

import android.app.Activity;
import android.content.SharedPreferences;
import android.view.View;

/**
 * 设置默认背景和更换背景
 * @author Harlan
 * 2012-5-7
 */
public class SetBackgroundImage {
	
	public static void changeBackgroundImage(int i) {

	}
	
	/**
	 * 设置背景图片
	 * @param activity
	 * @param view 在这里传的是LinearLayout
	 */
	public static void setBackGround(Activity activity, View view) {
		String bg = getBG(activity);
		if (bg != null) {
			if ("bg1".equals(bg)) {
				view.setBackgroundResource(R.drawable.bg1);
			}
			if ("bg2".equals(bg)) {
				view.setBackgroundResource(R.drawable.bg2);
			}
			if ("bg3".equals(bg)) {
				view.setBackgroundResource(R.drawable.bg3);
			}
		}else{
			saveBackground(activity,"bg1");
			view.setBackgroundResource(R.drawable.bg3);
		}

	}
	
	/**
	 * 更改默认背景图片
	 * @param activity
	 * @param imageTag	
	 */
	public static void saveBackground(Activity activity, String imageTag) {
		SharedPreferences preferences = activity.getSharedPreferences("bg",
				Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
		editor.putString("background", imageTag);
		editor.commit();

	}
	
	/**
	 * 得到默认图片标识
	 * @param activity
	 * @return 
	 */
	private static String getBG(Activity activity) {
		SharedPreferences preferences = activity.getSharedPreferences("bg",Activity.MODE_PRIVATE);
		String bg = preferences.getString("background", null);
		return bg;

	}
}


Activity2.java 没什么内容


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/llayout" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" 
        />


    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="跳转下一个Activity看效果" />


    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="背景一" />


    <Button
        android:id="@+id/button3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="背景二" />


    <Button
        android:id="@+id/button4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="背景三" />

</LinearLayout>

o分源代码下载: http://download.csdn.net/detail/mark_dev/4284540


Over。


ps:偶只是按照我自己想的写的方法,如果有什么好的方法记得分享也学习一下。如果兴趣的可以加载我们的QQ群(213167832)




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值