6*3自定义Cell布局

package com.eken.wondertv.app;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.FocusFinder;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import com.wmt.wondertv.common.WorkContext;

public class FixedGridLayout extends ViewGroup {
	private final static String TAG = "FixedGridLayout";
	public final static int FIXGRID_COLUMN = 6;
	public final static int FIXGRID_ROW = 3;
	public final static int CELL_WIDE = 168;

	private WorkContext mWorkContext;
	private boolean mOccupied[][] = new boolean[FIXGRID_ROW][FIXGRID_COLUMN];

	private View firstFocusView;
	private View lastFocusView;

	public FixedGridLayout(Context context) {
		super(context);
		init();
	}

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

	@Override
	protected void dispatchDraw(Canvas canvas) {
		// TODO Auto-generated method stub

		// 获取布局控件宽高
		int width = getWidth();
		int height = getHeight();
		// 创建画笔
		Paint mPaint = new Paint();
		// 设置画笔的各个属性
		mPaint.setColor(Color.BLUE);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(10);
		mPaint.setAntiAlias(true);

		// 创建矩形框
		Rect mRect = new Rect(0, 0, width, height);
		// 绘制边框
		canvas.drawRect(mRect, mPaint);

		// 最后必须调用父类的方法
		super.dispatchDraw(canvas);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// 记录ViewGroup中Child的总个数
		int count = getChildCount();

		// 设置子空间Child的宽高
		for (int i = 0; i < count; i++) {
			View childView = getChildAt(i);
			final CellLayoutParams lp = (CellLayoutParams) childView.getLayoutParams();
			/*
			 * This is called to find out how big a view should be. The parent
			 * supplies constraint information in the width and height
			 * parameters. The actual mesurement work of a view is performed in
			 * onMeasure(int, int), called by this method. Therefore, only
			 * onMeasure(int, int) can and must be overriden by subclasses.
			 */
			childView.measure(lp.xColumn*CELL_WIDE, lp.yRow*CELL_WIDE);
		}

		// 设置容器控件所占区域大小
		// 注意setMeasuredDimension和resolveSize的用法
		setMeasuredDimension(resolveSize(CELL_WIDE * FIXGRID_COLUMN, widthMeasureSpec),
				resolveSize(CELL_WIDE * FIXGRID_ROW, heightMeasureSpec));
		// setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);

		// 不需要调用父类的方法
		//super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		int count = getChildCount();
		for (int i = 0; i < count; i++) {
			final View child = getChildAt(i);
			final CellLayoutParams lp = (CellLayoutParams) child.getLayoutParams();
			
			int left = getPaddingLeft() + lp.positionX*CELL_WIDE;
			int top = getPaddingTop() + lp.positionY*CELL_WIDE;
			
			
			final int right = left + lp.xColumn*CELL_WIDE;
			final int bottom = top + lp.yRow*CELL_WIDE;
			child.layout(left, top, right, bottom);
//			Log.e(TAG, "width: "+child.getWidth()+" height: "+child.getHeight());
//			if (child == this.getFocusedChild() ) {
//				final int width = (int) (lp.xColumn * CELL_WIDE * 1.1);
//				final int height = (int) (lp.yRow * CELL_WIDE * 1.1);
//				left =(int) (left - (lp.xColumn * CELL_WIDE*0.1) / 2);
//				top = (int) (top - (lp.yRow * CELL_WIDE*0.1) / 2);
//				
//				child.layout(left , top , left + width , top
//						+ height);
//
//			}
		}
	}

	public void setWorkContext(WorkContext mWorkContext) {
		this.mWorkContext = mWorkContext;
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		return super.dispatchKeyEvent(event) || executeKeyEvent(event);
	}

	private boolean executeKeyEvent(KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN) {

			View currentFocused = findFocus();
			if (currentFocused == this)
				currentFocused = null;
			final int keyCode = event.getKeyCode();
			if (KeyEvent.KEYCODE_DPAD_LEFT == keyCode
					|| KeyEvent.KEYCODE_DPAD_RIGHT == keyCode) {
				final View nextFocused = getChildTopParent(FocusFinder
						.getInstance()
						.findNextFocus(
								this,
								currentFocused,
								keyCode == KeyEvent.KEYCODE_DPAD_LEFT ? View.FOCUS_LEFT
										: View.FOCUS_RIGHT));
				switch (keyCode) {
				case KeyEvent.KEYCODE_DPAD_LEFT:
					if (nextFocused == null) {
						firstFocusView = currentFocused;
						mWorkContext.mHandler.removeMessages(View.FOCUS_LEFT);
						mWorkContext.mHandler.sendEmptyMessage(View.FOCUS_LEFT);
						return true;
					}
					break;
				case KeyEvent.KEYCODE_DPAD_RIGHT:
					if (nextFocused == null) {
						lastFocusView = currentFocused;
						mWorkContext.mHandler.removeMessages(View.FOCUS_RIGHT);
						mWorkContext.mHandler
								.sendEmptyMessage(View.FOCUS_RIGHT);
						return true;
					}
					break;
				}
			}

		}
		return false;
	}

	public View getFirstFocusView() {
		return firstFocusView != null ? firstFocusView : null;
	}

	public View getLastFocusView() {
		return lastFocusView != null ? lastFocusView : null;
	}

	public boolean insertView(View view,int positionX,int positionY,int xColumn,int yRow) {
		if(positionX>=0&&positionY>=0
				&&positionX<FIXGRID_COLUMN&&positionY<FIXGRID_ROW
				&&xColumn>0&&yRow>0
				&&(positionX + xColumn <= FIXGRID_COLUMN)
				&& (positionY + yRow <= FIXGRID_ROW)){
			//这个逻辑判断是添加的组件必须要网格之内
			int count =0;
			for(int i=0;i<yRow;i++){
				for(int j=0;j<xColumn;j++)
					if(!mOccupied[positionY+i][positionX+j])
						count++;
			}
			if(count == xColumn*yRow){
				for(int i=0;i<yRow;i++){
					for(int j=0;j<xColumn;j++){
						mOccupied[positionY+i][positionX+j] = true;
						//itemList.add(itemView);
					}
				}
				CellLayoutParams cellLayout = new CellLayoutParams(positionX, positionY, xColumn, yRow);
				this.addView(view,cellLayout);
				return true;
			}
		}
		return false;
	}
//-----------------------------------------------------

	private View getChildTopParent(View child) {
		// get the outer View but not the PanelGroup
		while (child != null && child.getParent() != null
				&& child.getParent() != this
				&& View.class.isAssignableFrom(child.getParent().getClass())) {
			child = (View) child.getParent();
		}
		return child;
	}
	private void init(){
		for(int i = 0;i<FIXGRID_ROW;i++)
			for(int j=0;j<FIXGRID_COLUMN;j++)
				mOccupied[i][j] = false;
	}
	public static class CellLayoutParams extends ViewGroup.MarginLayoutParams {
		public int positionX = 0;
		public int positionY = 0;
		public int xColumn = 1;
		public int yRow = 1;

		public CellLayoutParams(int positionX,int positionY,int xColumn,int yRow) {
			super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			this.positionX = positionX;
			this.positionY = positionY;
			this.xColumn = xColumn;
			this.yRow = yRow;
		}

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值