Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 實現一鍵切換應用主題顏色(二)

Android 實現一鍵切換應用主題顏色(二)

編輯:關於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);
	}

}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved