Android 实现一键切换应用主题颜色(二)

    接着上一篇继续来看看如何实现应用的一些图标也能够跟随应用的主题颜色切换而改变图标的颜色。比如应用首页的一些固定的图标,或者是单选框以及复选框的图标,还有一些功能按钮的图标等等。先看一个效果图:




    在上图可以看到当切换相应的应用主题颜色时,可以看到下面所选中的图标也会立即跟随应用的主题颜色进行切换。实现这个功能其实也很简单,不需要把每个颜色的图标都让设计去准备一套,只需要一套白色颜色图标就可以了。然后同样也是使用ColorFilter颜色滤镜的子类LightingColorFilter来处理。下面贴上相关的代码。


1、自定义ColorImageView

import android.content.Context;
import android.graphics.LightingColorFilter;
import android.util.AttributeSet;
import android.widget.ImageView;
import com.qiulong.changedmotivecolortest.mode.ColorManager;
import com.qiulong.changedmotivecolortest.mode.OnColorChangedListener;

public class ColorImageView extends ImageView implements OnColorChangedListener {
	private int color;
	private boolean isColorEnabled = true;

	public ColorImageView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	public ColorImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public ColorImageView(Context context) {
		super(context);
	}

	@Override
	public void onColorChanged(int color) {
		if (this.color == color) {
			return;
		}
		this.color = color;
		if (isColorEnabled) {
			setColorFilter(new LightingColorFilter(color, 1));
		}
	}

	public void setColorFilterEnabled(boolean isColorEnabled) {
		if (this.isColorEnabled == isColorEnabled) {
			return;
		}
		this.isColorEnabled = isColorEnabled;
		if (isColorEnabled) {
			setColorFilter(new LightingColorFilter(color, 1));
		} else {
			setColorFilter(null);
		}
	}

	@Override
	protected void onAttachedToWindow() {
		ColorManager.getInstance().addListener(this);
		super.onAttachedToWindow();
	}

	@Override
	protected void onDetachedFromWindow() {
		ColorManager.getInstance().removeListener(this);
		super.onDetachedFromWindow();
	}
}

2、应用主题Activity类:MotiveActivity

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import com.qiulong.changedmotivecolortest.R;
import com.qiulong.changedmotivecolortest.mode.ColorManager;
import com.qiulong.changedmotivecolortest.view.ColorImageView;

public class MotiveActivity extends Activity implements OnClickListener {

	private final int[] layouts = { R.id.skin_01, R.id.skin_02, R.id.skin_03,
			R.id.skin_04, R.id.skin_05 };
	private ColorImageView mSelected, mChecked, mDelete;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_motive);
		initView();
	}

	private void initView() {
		findViewById(R.id.motive_back).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						finish();
					}
				});
		int colorArr[] = ColorManager.getInstance().getSkinColor(this);
		for (int i = 0; i < layouts.length; i++) {
			View view = findViewById(layouts[i]);
			View color = view.findViewById(R.id.motive_item_color);
			View selected = view.findViewById(R.id.motive_item_selected);
			color.setBackgroundColor(colorArr[i]);
			if (colorArr[i] == MyApplication.mPreference.getSkinColorValue()) {
				selected.setVisibility(View.VISIBLE);
			}
			color.setOnClickListener(new OnSkinColorClickListener(i));
		}

		mSelected = (ColorImageView) findViewById(R.id.motive_selected);
		mChecked = (ColorImageView) findViewById(R.id.motive_checked);
		mDelete = (ColorImageView) findViewById(R.id.motive_delete);
		setColorImageViewEnabled(mSelected,
				MyApplication.mPreference.getSelectedEnabled());
		setColorImageViewEnabled(mChecked,
				MyApplication.mPreference.getCheckedEnabled());
		setColorImageViewEnabled(mDelete,
				MyApplication.mPreference.getDeleteEnabled());
		mSelected.setOnClickListener(this);
		mChecked.setOnClickListener(this);
		mDelete.setOnClickListener(this);
	}

	class OnSkinColorClickListener implements OnClickListener {

		private int position;

		public OnSkinColorClickListener(int position) {
			this.position = position;
		}

		@Override
		public void onClick(View v) {
			for (int i = 0; i < layouts.length; i++) {
				View view = findViewById(layouts[i]);
				View selected = view.findViewById(R.id.motive_item_selected);
				selected.setVisibility(i == position ? View.VISIBLE : View.GONE);
				ColorManager.getInstance().setSkinColor(MotiveActivity.this,
						position);
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.motive_selected:
			boolean isSelected = mSelected.isSelected() ? false : true;
			setColorImageViewEnabled(mSelected, isSelected);
			MyApplication.mPreference.setSelectedEnabled(isSelected);
			break;
		case R.id.motive_checked:
			boolean isChecked = mChecked.isSelected() ? false : true;
			setColorImageViewEnabled(mChecked, isChecked);
			MyApplication.mPreference.setCheckedEnabled(isChecked);
			break;
		case R.id.motive_delete:
			boolean isDelete = mDelete.isSelected() ? false : true;
			setColorImageViewEnabled(mDelete, isDelete);
			MyApplication.mPreference.setDeleteEnabled(isDelete);
			break;
		}
	}

	private void setColorImageViewEnabled(ColorImageView imageView,
			boolean isEnabled) {
		imageView.setSelected(isEnabled);
		imageView.setColorFilterEnabled(isEnabled);
	}

}


以上内容仅供参考和学习,如有不妥之处欢迎指正和讨论,谢谢!

下载源码请戳这里:http://download.csdn.net/detail/baidu_23478311/9515416

你可能感兴趣的:(Android开发)