pndsettings 界面最新win8风格,方块移动点击效果,总结。

刚开始做走了很多弯路,最开始用两个gridview配relativelayout重叠,目的是解决方块突出时与其他图层的重叠问题,后来发现gridview有一个致命
的缺陷,边界不好调,方块突出时不能越过grideview的边界。经过数次尝试后我选择了自定义boxItem组件,但还是走了弯路,自己控制焦点使用dispatchKeyEvent方法。忽略了android自己本身就有一套自动的焦点控制机制,最后恍然大悟毅然选择了android自己控制焦点利用onfocuschangelistener监控焦点变化,继而方块播放动画。
最后要分清inTouchMode和不再TouchMode中,鼠标点击马上全屏失去焦点,遥控器移动重新获取焦点。
package com.panodic.settings.view;

import java.util.HashMap;

import com.panodic.settings.ManageAppActivity;
import com.panodic.settings.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.InflateException;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.ViewParent;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.panodic.settings.about.AboutActivity;
import com.panodic.settings.display.DisplayMain;
import com.panodic.settings.net.ApSettings;
import com.panodic.settings.net.NetState;
import com.panodic.settings.net.WifiSettings;
import com.panodic.settings.net.WireSettings;
import com.panodic.settings.sound.SoundMain;
import com.panodic.settings.util.LogUtil;
import com.panodic.settings.util.NetUtil;
import com.panodic.settings.util.Util;

public class NetItem extends RelativeLayout implements OnClickListener,
		OnFocusChangeListener {

	// private static final int COUNT = 8;
//	private static int LEN_X = 2;
//	private static int LEN_Y = 3;
	// private static View[][] mChildren = new View[LEN_X][LEN_Y];
	// private static int mX;
	// private static int mY;
	private static View mPreView = null;
	// private static int SELECT_FIRST_TIME = 200;
	private static int mSize;
	private static PatchItem mPatch;
	private static Activity mActivity = null;
	private TextView mTitle;
	private TextView mSummery;
	private ImageView mImage;
	private Animation mTurnHuge = null;
	private Animation mTurnBig = null;
	private Animation mTurnNormal = null;
	private Context mContext = null;
//	private Handler mHandler = new Handler();
	private View mChild;
	private HashMap<View, Integer> mItem = new HashMap<View, Integer>();

	// private HashMap<View, Position> mItemMap = new HashMap<View, Position>();

	public NetItem(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView(context, attrs);
	}

	public NetItem(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public void setActivity(Activity acitivity) {
		mActivity = acitivity;
	}

	public void setPatch(PatchItem patch) {
		mPatch = patch;
		LogUtil.d("patch==" + mPatch);
	}

	public void setTitleValue(String title) {
		mTitle.setText(title);
	}

	public String getTitleValue() {
		return mTitle.getText().toString();
	}

	public void setSummeryValue(String summery) {
		mSummery.setText(summery);
	}

	public void setIcoSrc(int resId) {
		mImage.setImageResource(resId);
	}

	// public boolean isMyChild(View child) {
	// if (child == this) {
	// return true;
	// } else {
	// ViewParent vp = child.getParent();
	// while (vp != null) {
	// if (vp == this) {
	// return true;
	// }
	// vp = vp.getParent();
	// }
	// return false;
	// }
	// }

	protected void initView(Context context, AttributeSet attrs) {
		mContext = context;
		mChild = ((LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.disk_box_item, this, true);
		mTitle = (TextView) mChild.findViewById(R.id.box_name);
		mSummery = (TextView) mChild.findViewById(R.id.box_summery);
		mImage = (ImageView) mChild.findViewById(R.id.box_ico);
		if (mTitle == null || mSummery == null) {
			throw new InflateException("do you miss some widgets in EditItem?");
		}
		mChild.setOnFocusChangeListener(this);

		final TypedArray ta = context.obtainStyledAttributes(attrs,
				R.styleable.BoxItem);
		int title = ta.getResourceId(R.styleable.BoxItem_name, 0);
		int summery = ta.getResourceId(R.styleable.BoxItem_summery, 0);
		int ico = ta.getResourceId(R.styleable.BoxItem_ico, 0);
		ta.recycle();
		mTitle.setText(title);
		mSummery.setText(summery);
		mImage.setImageResource(ico);

		addChild(mChild);

		mChild.setOnClickListener(this);
		loadAnimation();
	}

	public static void initNetItem(PatchItem patch,Activity activity){
		mSize = 0;
		mPreView=null;
		mPatch=patch;
		mActivity = activity;
	}
	private void setPatchFocusable() {
		LogUtil.d(" set focs patch==" + mPatch);
		if (!mPatch.isPatchFocusable()) {
			mPatch.setFocusable(true);
		}
	}

	public void onClick(View v) {
		int position = mItem.get(v);
		setPatchFocusable();
		// Position pos = mItemMap.get(v);
		// NetUtil.x = pos.x;
		// NetUtil.y = pos.y;
		// LogUtil.d("prePos.x=" + pos.x + " prePos.y=" + pos.y);
		playAnim(v, false);
		// toBig(v);
		switch (position) {
		case 0:
			Util.startActivity(mActivity, WifiSettings.class);
			break;
		case 1: // display module
			Util.startActivity(mActivity, WireSettings.class);
			break;
		case 2:
			Util.startActivity(mActivity, ApSettings.class);
			break;
		case 3:
			break;
		case 4:
			Util.startActivity(mActivity, NetState.class);
			break;
		case 5:
			break;
		default:
			break;
		}
	}

	private void addChild(final View child) {
		// if (mY == LEN_Y) {
		// mX++;
		// if (mX == LEN_X) {
		// mX = 0;
		// }
		// mY = 0;
		// }
		// // LogUtil.d("add map mx ==" + mX + " mY==" + mY + child);
		// mItemMap.put(child, new Position(mX, mY));
		// mChildren[mX][mY++] = child;
//		 if (mSize >= COUNT) {
//		 mSize = 0;
//		 }
		mItem.put(child, mSize++);
	}

	// public boolean dispatchKeyEvent(KeyEvent event) {
	// if (event.getAction() == KeyEvent.ACTION_UP) {
	// return super.dispatchKeyEvent(event);
	// }
	// LogUtil.d("event=" + event);
	// if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
	// // boolean result = moveDown(true);
	// // LogUtil.d("down==" + result);
	// // return result;
	// // return moveDown(true);
	// } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
	//
	// // boolean result = moveDown(false);
	// // LogUtil.d("up==" + result);
	// // return result;
	// // return moveDown(false);
	// } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
	//
	// // boolean result = moveRight(false);
	// // LogUtil.d("left==" + result);
	// // return result;
	// // return moveRight(false);
	// } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
	//
	// // boolean result = moveRight(true);
	// // LogUtil.d("right== " + result);
	// // return result;
	// // return moveRight(true);
	// }
	// return super.dispatchKeyEvent(event);
	// }

	// private boolean moveRight(boolean right) {
	// int currX = NetUtil.x;
	// int currY = NetUtil.y;
	// // LogUtil.d("currx=" + currX + "  currY=" + currY);
	// if (right) {
	// if (currY + 1 < LEN_Y) {
	// // playAnim(mChildren[currX][currY + 1], false);
	// LogUtil.d("focus right:"
	// + mChildren[currX][currY + 1].requestFocus());
	// // mChildren[currX][currY + 1].requestFocus();
	// NetUtil.x = currX;
	// NetUtil.y = currY + 1;
	// return true;
	// }
	// return false;
	//
	// } else {
	// if (currY - 1 >= 0) {
	// // playAnim(mChildren[currX][currY - 1], false);
	// mChildren[currX][currY - 1].requestFocus();
	// NetUtil.x = currX;
	// NetUtil.y = currY - 1;
	// return true;
	// }
	// return false;
	// }
	// }

	public void resume() {
		mPatch.setOnFocusChangeListener(this);
		// mHandler.postDelayed(new Runnable() {
		// public void run() {
		// // playAnim(mChildren[NetUtil.x][NetUtil.y], true);
		// // mChildren[NetUtil.x][NetUtil.y].requestFocus();
		// // LogUtil.d("resume x=" + NetUtil.x + "  y=" + NetUtil.y);
		// mChild.requestFocus();
		// }
		// }, 100);
		// NetUtil.resetDeskItemPosition();
	}

	// private boolean moveDown(boolean down) {
	// int currX = NetUtil.x;
	// int currY = NetUtil.y;
	// if (down) {
	// if (currX + 1 < LEN_X) {
	// // playAnim(mChildren[currX + 1][currY], false);
	// mChildren[currX + 1][currY].requestFocus();
	// NetUtil.x = currX + 1;
	// NetUtil.y = currY;
	// return true;
	// }
	// // LogUtil.d("currx+1=" + (currX + 1) + "  mX=" + mX);
	// return false;
	//
	// } else {
	// if (currX - 1 >= 0) {
	// // playAnim(mChildren[currX - 1][currY], false);
	// mChildren[currX - 1][currY].requestFocus();
	// NetUtil.x = currX - 1;
	// NetUtil.y = currY;
	// return true;
	// }
	// return false;
	// }
	// }

	private void playAnim(View v, boolean stopAnim) {
		mTurnHuge.setFillAfter(false);
		mTurnBig.setFillAfter(false);
		mTurnNormal.setFillAfter(false);
		loadAnimation();
		// LogUtil.d("mPreView==" + mPreView + " v=" + v + " isfocsed="
		// + v.isFocused());
		if (mPreView != null) {
			// LogUtil.d("preview start animation-----------");
			mPreView.startAnimation(mTurnNormal);
			mTurnNormal.setFillAfter(true);
		}
		if (!stopAnim) {
			v.bringToFront();
			v.startAnimation(mTurnHuge);
			v.startAnimation(mTurnBig);
			mTurnBig.setFillAfter(true);
			mPreView = v;
		} else {
			mPreView = null;
		}
	}

	private void loadAnimation() {
		mTurnHuge = AnimationUtils.loadAnimation(mContext, R.anim.turn_huge);
		mTurnBig = AnimationUtils.loadAnimation(mContext, R.anim.trun_big);
		mTurnNormal = AnimationUtils
				.loadAnimation(mContext, R.anim.trun_normal);
	}

	class Position {
		int x;
		int y;

		public Position() {
		}

		public Position(int x, int y) {
			this.x = x;
			this.y = y;
		}
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		LogUtil.d("focus=" + hasFocus + " v=" + v);
		if (hasFocus && v instanceof NetItem) {
			setPatchFocusable();
			playAnim(v, false);
		}
		if (hasFocus && v instanceof Button) {
			LogUtil.d("Button");
			playAnim(v, true);
		}

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值