六边形布局
public class SexangleViewGroup extends ViewGroup {
private static final String TAG = "yanmei";
private static final int RADIU_COUNT = 4;
private static final int PADDING = 6;
private int childRadius;// �뾶
private int childWidth;
private int childHeight;
private int mChildCount;
private int centerX, centerY;
public SexangleViewGroup(Context context) {
super(context);
Log.i(TAG, "SpecailView()");
}
public SexangleViewGroup(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Log.i(TAG, "SpecailView( , , )");
}
public SexangleViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
Log.i(TAG, "SpecailView( , )");
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int wMode = MeasureSpec.getMode(widthMeasureSpec);
int wSize = MeasureSpec.getSize(widthMeasureSpec);
int hMode = MeasureSpec.getMode(heightMeasureSpec);
int hSize = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(wSize, hSize);
centerX = wSize / 2;
centerY = hSize / 2;
childRadius = (wSize - (int)(PADDING * 2/sqrt3)) / RADIU_COUNT;// �뾶ֵ
childWidth = childRadius * 2;
childHeight = childRadius * 2;
final int count = getChildCount();
for (int index = 0; index < count; index++) {
View child = getChildAt(index);
// measure
child.measure(childWidth, childHeight);
}
if (mChildCount != count) {
mChildCount = count;
}
computerPoint(centerX, centerY, childHeight);
Log.e(TAG, "onMeasure()--childWidth=" + childWidth + ",childHeight="
+ childHeight + " wMode=" + wMode + " wSize=" + wSize
+ " hSize" + hSize + " childRadius" + childRadius
+ " childWidth" + childWidth + " childHeight" + childHeight);
Log.e(TAG, "onMeasure()--wMode=" + wMode + ",wSize=" + wSize
+ ",hMode=" + hMode + ",hSize=" + hSize);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
final int count = getChildCount();
int childLeft, childTop;
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
double centery=centers.get(i).y;
double centerx=centers.get(i).x;
childLeft = (int) (centerx - childRadius);
childTop = (int) (centery - childHeight / 2);
child.layout(childLeft, childTop, childLeft + childWidth, childTop
+ childHeight);
Log.e(TAG, "onLayout()--changed=" + changed + ",left=" + left
+ ",top=" + top + ",right=" + right + ",bottom=" + bottom
+ ",count=" + count + " childLeft=" + childLeft
+ " childTop" + childTop);
}
}
double sqrt3 = Math.sqrt(3);
private void computerPoint(double a, double b, double h) {
CircleCenteter c01 = new CircleCenteter(a, b);
CircleCenteter c04 = new CircleCenteter(a + sqrt3 * h / 2 + PADDING*2, b);// /right
CircleCenteter c09 = new CircleCenteter(a - sqrt3 * h / 2 - PADDING*2, b);// /right
CircleCenteter c02 = new CircleCenteter(a + sqrt3 * h / 4 + PADDING, b
- 3 * h / 4-PADDING*3/sqrt3);
CircleCenteter c03 = new CircleCenteter(a + 3*sqrt3 * h / 4 + PADDING*3, b
- 3 * h / 4-PADDING*3/sqrt3);
CircleCenteter c10 = new CircleCenteter(a - sqrt3 * h / 4 - PADDING, b
- 3 * h / 4-PADDING*3/sqrt3);// /right
CircleCenteter c11 = new CircleCenteter(a - 3*sqrt3 * h / 4 - PADDING*3, b
- 3 * h / 4-PADDING*3/sqrt3);// /right
CircleCenteter c05 = new CircleCenteter(a + sqrt3 * h / 4 + PADDING, b
+ 3 * h / 4+PADDING*3/sqrt3);// /false
CircleCenteter c06 = new CircleCenteter(a - sqrt3 * h / 4 - PADDING, b
+ 3 * h / 4+PADDING*3/sqrt3);// /false
CircleCenteter c07 = new CircleCenteter(a + 3*sqrt3 * h / 4 + PADDING*3, b
+ 3 * h / 4+PADDING*3/sqrt3);// /false
CircleCenteter c08 = new CircleCenteter(a - 3*sqrt3 * h / 4 - PADDING*3, b
+ 3 * h / 4+PADDING*3/sqrt3);// /false
CircleCenteter c12 = new CircleCenteter(a + sqrt3 * h / 2 + PADDING*2, b
+ (3 * h / 4+PADDING*3/sqrt3)*2);// right
CircleCenteter c13 = new CircleCenteter(a, b + (3 * h / 4+PADDING*3/sqrt3)*2);//
CircleCenteter c14 = new CircleCenteter(a - sqrt3 * h / 2 - PADDING*2, b
+ (3 * h / 4+PADDING*3/sqrt3)*2);// right
CircleCenteter c15 = new CircleCenteter(a + sqrt3 * h / 2 + PADDING*2, b
- (3 * h / 4+PADDING*3/sqrt3)*2);// right
CircleCenteter c17 = new CircleCenteter(a - sqrt3 * h / 2 - PADDING*2, b
-(3 * h / 4+PADDING*3/sqrt3)*2);// right
CircleCenteter c16 = new CircleCenteter(a, b - (3 * h / 4+PADDING*3/sqrt3)*2);//
CircleCenteter c18 = new CircleCenteter(a + sqrt3 * h / 4 + PADDING, b
+ sqrt3 * h / 3 + 2 * h);//
CircleCenteter c19 = new CircleCenteter(a - sqrt3 * h / 4 - PADDING, b
+ sqrt3 * h / 3 + 2 * h);//
centers.clear();
centers.add(c01);
centers.add(c02);
centers.add(c03);
centers.add(c04);
centers.add(c05);
centers.add(c06);
centers.add(c07);
centers.add(c08);
centers.add(c09);
centers.add(c10);
centers.add(c11);
centers.add(c12);
centers.add(c13);
centers.add(c14);
centers.add(c15);
centers.add(c16);
centers.add(c17);
centers.add(c18);
centers.add(c19);
}
public void setOnItemClick(SexangleImageViews.OnClickListener l) {
int count = getChildCount();
for (int i = 0; i < count; i++) {
((SexangleImageViews) getChildAt(i)).setOnClickListener(l);
}
}
private ArrayList<CircleCenteter> centers = new ArrayList<SexangleViewGroup.CircleCenteter>(
7);
class CircleCenteter {
double x, y;
public CircleCenteter(double x, double y) {
this.x = x;
this.y = y;
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
return false;
}
}
public class SexangleImageViews extends ImageView {
private OnSexangleImageClickListener listener;
public SexangleImageViews(Context context) {
super(context);
}
@SuppressLint("Recycle")
public SexangleImageViews(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setOnSexangleImageClick(OnSexangleImageClickListener listener ){
this.listener=listener;
}
public interface OnSexangleImageClickListener {
public void onClick(View view);
}
// @Override
// public boolean onTouchEvent(MotionEvent event) {
//
//
// switch (event.getAction()) {
//
// case MotionEvent.ACTION_UP:
// if(listener!=null){
// listener.onClick(this);
// }
//
// break;
//
// }
//
// return true;
// }
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
if(listener!=null){
listener.onClick(this);
}
return false;
}
}
