android 简单的更换皮肤
更换皮肤
1.更换皮肤其实就是更换Activity的背景图片
直接上代码:
Activity:
1 public class MainActivity extends Activity implements OnClickListener { 2 3 private SkinManager skinManager; 4 private int downNums; 5 private Button btn; 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_main); 11 skinManager = new SkinManager(this); 12 skinManager.init(); 13 btn = (Button) findViewById(R.id.btn_skin); 14 btn.setOnClickListener(this); 15 } 16 17 /** 18 * 背景切换 19 */ 20 @Override 21 public boolean onTouchEvent(MotionEvent event) { 22 switch (event.getAction()) { 23 case MotionEvent.ACTION_DOWN: 24 downNums++; 25 skinManager.toggleSkin(downNums % 5); 26 break; 27 28 } 29 30 return super.onTouchEvent(event); 31 } 32 33 /** 34 * 点击后弹出主题选择框,也就是选择图片 35 */ 36 @Override 37 public void onClick(View v) { 38 39 switch (v.getId()) { 40 case R.id.btn_skin: 41 GridView grid = new GridView(this); 42 List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>(); 43 HashMap<String, Object> map = new HashMap<String, Object>(); 44 map.put("imgID", R.drawable.default_wallpaper); 45 list.add(map); 46 HashMap<String, Object> map1 = new HashMap<String, Object>(); 47 map.put("imgID", R.drawable.wallpaper_c); 48 list.add(map1); 49 SimpleAdapter adapter = new SimpleAdapter(this, list, 50 R.layout.item, new String[] { "imgID" }, 51 new int[] { R.id.image }); 52 grid.setAdapter(adapter); 53 new AlertDialog.Builder(this).setTitle("选择图片").setView(grid) 54 .create().show(); 55 break; 56 57 } 58 } 59 }
activity_main.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context=".MainActivity" > 7 8 <TextView 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="@string/hello_world" /> 12 13 <Button 14 android:id="@+id/btn_skin" 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:text="皮肤选择" /> 18 19 </LinearLayout>
item.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <ImageView 8 android:id="@+id/image" 9 android:layout_width="70dp" 10 android:layout_height="70dp" /> 11 12 </LinearLayout>
SkinManager.class
1 public class SkinManager { 2 3 private String spName = "skin_storage"; 4 5 private SharedPreferences sp; 6 7 private Activity activity; 8 9 private int drawableArray[] = { R.drawable.default_wallpaper, 10 R.drawable.wallpaper_c, R.drawable.wallpaper_d, 11 R.drawable.wallpaper_f, R.drawable.wallpaper_g }; 12 13 public SkinManager(Activity activity) { 14 this.activity = activity; 15 sp = activity.getSharedPreferences(spName, 0); 16 } 17 18 /** 19 * 保存皮肤ID 20 */ 21 private void saveImgID(int ID) { 22 String editorName = "skinID"; 23 SharedPreferences.Editor editor = sp.edit(); 24 editor.putInt(editorName, ID); 25 editor.commit(); 26 } 27 28 /** 29 * 获取当前皮肤 30 */ 31 private int getCurrentImgID() { 32 String editorName = "skinID"; 33 return sp.getInt("skinID", R.drawable.default_wallpaper); 34 35 } 36 37 public void toggleSkin(int i) { 38 try { 39 activity.getWindow() 40 .setBackgroundDrawableResource(drawableArray[i]); 41 } catch (Exception e) { 42 } 43 } 44 45 public void init() { 46 47 activity.getWindow().setBackgroundDrawableResource(drawableArray[0]); 48 } 49 50 }
效果图: