package com.g.s.view;
import java.net.InterfaceAddress;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
/**
*
* @author GaoSai
* github:
* 圆形非滚动菜单栏,模仿自:http://blog.csdn.net/lmj623565791/article/details/43131133
* 只是完成了位置的摆放,背景图片和点击时间没有设置
*/
public class SRoundMenu extends ViewGroup{
/**
* 当前圆菜单的半径
*/
private int radious;
/**
* 各子元素所占的比例
*/
private final double RATIO_ITEM_DIMENSION = 1.0 / 4;
private final int DEFAULT_BACKGROUND = Color.WHITE;
public SRoundMenu( Context cxt ) {
super( cxt );
setWillNotDraw( false );
}
public SRoundMenu( Context cxt, AttributeSet attrs ) {
super( cxt, attrs );
setWillNotDraw( false );
}
public SRoundMenu( Context cxt, AttributeSet attrs, int style ) {
super( cxt, attrs, style );
setWillNotDraw( false );
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//先确定自身尺寸
int resWidth, resHeight;
resWidth = resHeight = 0;
int width = MeasureSpec.getSize( widthMeasureSpec ),
height = MeasureSpec.getSize( heightMeasureSpec ),
widthMode = MeasureSpec.getMode( widthMeasureSpec ),
heightMode = MeasureSpec.getMode( heightMeasureSpec );
if( widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY ){
resWidth = getSuggestedMinimumWidth();
resWidth = resWidth == 0 ? getScreenWidth() : resWidth;
resHeight = getSuggestedMinimumHeight();
resHeight = resHeight == 0 ? getScreenHeight() : resHeight;
}else{
resWidth = resHeight = Math.min( width , height );
}
setMeasuredDimension( resWidth , resHeight );
//确定孩子尺寸
radious = Math.min( resHeight , resWidth ) / 2;
int childWidth = (int) (radious * RATIO_ITEM_DIMENSION);
int childMeasureSpec = MeasureSpec.makeMeasureSpec( childWidth , MeasureSpec.EXACTLY );
int childNum = getChildCount();
for( int i=0; i<childNum; ++i ){
View v = getChildAt( i );
if( v.getVisibility() == GONE ){
continue;
}
v.measure(childMeasureSpec, childMeasureSpec);
}
}
/**
* 获取屏幕宽
* @return 屏幕宽度
*/
@SuppressLint("NewApi")
private int getScreenWidth() {
WindowManager wm = (WindowManager) getContext().getSystemService( Context.WINDOW_SERVICE );
DisplayMetrics metrics = new DisplayMetrics();
wm.getDefaultDisplay().getRealMetrics( metrics );
return metrics.widthPixels;
}
/**
* 获取屏幕高
* @return 屏幕高
*/
@SuppressLint("NewApi")
private int getScreenHeight(){
WindowManager wm = (WindowManager) getContext().getSystemService( Context.WINDOW_SERVICE );
DisplayMetrics metrics = new DisplayMetrics();
wm.getDefaultDisplay().getRealMetrics( metrics );
return metrics.heightPixels;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int curAngle = 0;
int cNum = getChildCount();
if( cNum == 0 ) return ;
int preAngle = 360 / cNum;
int cWidth = (int) (radious * RATIO_ITEM_DIMENSION);
int cradious = (int) (radious * RATIO_ITEM_DIMENSION * 2.5);
int width = getMeasuredWidth(),
height = getMeasuredHeight();
for( int i=0; i<cNum; ++i ){
View v = getChildAt( i );
if( v.getVisibility() == GONE ){
continue;
}
int px = (int) (cradious * Math.cos( Math.toRadians( curAngle ) ));
int py = (int) (cradious * Math.sin( Math.toRadians( curAngle ) ));
px = width / 2 + px;
py = height / 2 - py;
int left = (int) (px - cWidth / 2);
int top = (int) (py - cWidth / 2);
v.layout( left , top, left + cWidth, top + cWidth);
curAngle += preAngle;
}
}
}
SRoundLayout-逆时针圆形分布Layout
最新推荐文章于 2021-03-20 14:32:21 发布