GL音乐播放器--代码实现--实现换肤功能

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u011747781/article/details/19407403

下面我要讲的便是如何实现换肤功能。众所周知,现在的手机应用大部分都支持这个功能,可是如何换肤呢?介绍这方面的博客倒还真不太多,所以我希望看完我的这篇博客会对大家有所启发,有所帮助,我就特别开心了。

首先是效果图:

 

首先是皮肤设置的主要java代码:

package com.genius.demo;

import com.genius.adapter.ImageAdapter;
import com.genius.widget.Settings;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

/**
 * @author Google_acmer
 *
 */

public class SkinSettingActivity extends SettingActivity{
	private GridView gv_skin;			//网格视图
	private ImageAdapter adapter;		//图片适配器
	private Settings mSetting;			//设置引用
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.skinsetting_layout);		
		resultCode = 2;
		mSetting = new Settings(this, true);	
		SkinSettingActivity.this.getWindow().setBackgroundDrawableResource(Settings.SKIN_RESOURCES[6]);
		adapter = new ImageAdapter(this, mSetting.getCurrentSkinId());
		gv_skin = (GridView) findViewById(R.id.gv_skin);
		gv_skin.setAdapter(adapter);
		gv_skin.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				//更新GridView
				adapter.setCurrentId(position);
				//更新背景图片
				SkinSettingActivity.this.getWindow().setBackgroundDrawableResource(Settings.SKIN_RESOURCES[position]);
				//保存数据
				mSetting.setCurrentSkinResId(position);
				SharedPreferences preferences;
				 preferences = getSharedPreferences("data", 0);
				preferences.edit().putInt("background_id", position).commit();
				//Toast.makeText(SkinSettingActivity.this, "位置"+preferences.getInt("id", 0), Toast.LENGTH_SHORT);
				
			}
		});
	}
	
}

 

package com.genius.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class SettingActivity extends Activity {
	public int resultCode = -1;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK) {
			if(resultCode != -1) {
				setResult(resultCode);
			}
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
	
	
	
}


接下来需要在MusicPlayActivity里调用

Intent intent = new Intent(MusicPlayActivity.this,SkinSettingActivity.class);
					startActivityForResult(intent, 1);

注意要复写回调函数,这里如果不太懂可以参考文章http://blog.csdn.net/google_acmer/article/details/19409899,写得非常详细,我也是从那里受到启发的。

//得到改变后皮肤的数据
    @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1 && requestCode == 1) {
			Settings setting = new Settings(this, false);
			this.getWindow().setBackgroundDrawableResource(setting.getCurrentSkinResId());
		}
	}

下面是皮肤的资源模块:


 

package com.genius.widget;

import com.genius.demo.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;



public class Settings {
	/**
	 * SD卡下载歌曲目录
	 */
	public static final String DOWNLOAD_MUSIC_DIRECTORY = "/Music/download_music/";
	/**
	 * SD卡下载歌词目录
	 */
	public static final String DOWNLOAD_LYRIC_DIRECTORY = "/Music/download_lyric/";
	/**
	 * SD卡下载专辑图片目录
	 * */
	public static final String DOWNLOAD_ALBUM_DIRECTORY="/Music/download_album/";
	/**
	 * SD卡下载歌手图片目录
	 * */
	public static final String DOWNLOAD_ARTIST_DIRECTORY="/Music/download_artist/";
	/**
	 * 系统设置的保存的文件名
	 */
	public static final String PREFERENCE_NAME = "com.wwj.music.settings";
	public static final String KEY_SKINID = "skin_id";
	private SharedPreferences settingPreferences;	
	
	/**
	 * 皮肤资源ID数组
	 */
	public static final int[] SKIN_RESOURCES = {
		R.drawable.main_bg01, R.drawable.main_bg02,
		R.drawable.main_bg03, R.drawable.main_bg04,
		R.drawable.main_bg05, R.drawable.main_bg06,
		R.drawable.bg_playback
	};
	
	public Settings(Context context, boolean isWrite) {
		settingPreferences = context.getSharedPreferences(PREFERENCE_NAME,
				isWrite ? Context.MODE_WORLD_READABLE
						: Context.MODE_WORLD_WRITEABLE);
	}
	
	/**
	 * 获取设置数据
	 * @param key
	 * @return
	 */
	public String getValue(String key) {
		return settingPreferences.getString(key, null);
	}
	/**
	 * 获取皮肤资源ID
	 * @return
	 */
	public int getCurrentSkinResId() {
		int skinIndex = settingPreferences.getInt(KEY_SKINID, 0);
		if(skinIndex >= SKIN_RESOURCES.length) {
			skinIndex = 0;
		}
		return SKIN_RESOURCES[skinIndex];
	}
	
	/**
	 * 获取皮肤Id
	 * @return
	 */
	public int getCurrentSkinId() {
		int skinIndex = settingPreferences.getInt(KEY_SKINID, 0);
		if(skinIndex >= SKIN_RESOURCES.hashCode()) {
			skinIndex = 0;
		}
		return skinIndex;
	}
	
	/**
	 * 设置皮肤资源ID
	 * @param skinIndex
	 */
	public void setCurrentSkinResId(int skinIndex) {
		Editor it = settingPreferences.edit();
		it.putInt(KEY_SKINID, skinIndex);
		it.commit();
	}
	
	/**
	 * 设置键值
	 * @param key
	 * @param value
	 */
	public void setValue(String key, String value) {
		Editor it = settingPreferences.edit();
		it.putString(key, value);
		it.commit();
	}
}


最后需要注意几点:

1、用SharedPreferences别忘了声明,如果还不知道什么是SharedPreferences,请看看http://blog.csdn.net/google_acmer/article/details/19408413,我想你就会秒懂了。

2、由于设置背景是用setBackgroundDrawableResource方法,所以你在布局文件里的background里不能设置任何值,否则就没办法改变了。如果你想有个初始的皮肤,那就在开始的时候先用setBackgroundDrawableResource方法设置一下就行了,下面是我的代码

 int position=preferences.getInt("background_id", 0);//改变后的皮肤ID
Settings mSetting = new Settings(this, true);
		this.getWindow().setBackgroundDrawableResource(Settings.SKIN_RESOURCES[position]);//初始化壁纸

如果这些你都按我的步骤做了,我想这个功能你就实现了。


 

 

展开阅读全文

没有更多推荐了,返回首页