SRoundLayout-逆时针圆形分布Layout

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;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值