Android 自定义View,圆形随机选择列表

来源:互联网 发布:翻译软件哪个好 编辑:程序博客网 时间:2024/05/17 23:45

Android 自定义View,圆形随机选择列表

第一次写博客,做了很长时间的android,好多的东西都是从网上拔下来,这次由于工作需要,从网上没拔下来控件,就只能自己做了。。

虽说之前,从网上看过自定义控件怎么写,比如onMesure测量,之后onLayout布局,最后再onDraw等等,但实际自己写的时候,发现并没有什么用。

比如自定义View分好多种,可以继承View,可以继承ViewGroup,可以继承Linearlayout,等等所谓不学不知道啊,自己研究一番刷新了我对自定义控件的认知。

在此我就介绍一下自定义ViewGroup,因为我这个控件是使用ViewGroup写的。

上图



第一次写博客这不知道怎么弄啊,大概给大家讲一下,中间是个可以转的圆形图片,周围是随机顺序生成的文字,透明度由0变1,大小随机可点击,点击变色。

下面不废话了直接讲怎么做的。

首先继承ViewGroup,在这要讲一下,一般继承View,onLayout用的不会多,大多都是用的onDraw,继承ViewGroup用的onLayout较多,一般不会用到onDraw,之后会给大家讲一个我自己ViewGroup用onDraw的例子哈。

继承ViewGroup后,会重写三个构造方法,主要是为了实现自定义属性,这里没用上就不给大家细讲了。之后就是onMeasure。

重写onMeasure要调用 measureChildren()方法,只要是为了计算出所有子View的宽高,之后就是setMeasuredDimension()测量保存onLayout的高度,通俗的讲就是这是设置自定义view的宽高的。

@Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {        int cCount = getChildCount();        int screenWidth = getWidth();        int screenHeight = getHeight();        //圆心坐标        outr = screenWidth / 2;        int cl = 0, ct = 0, cr = 0, cb = 0;        for (int i = 0; i < cCount; i++) {            View childAt = getChildAt(i);            int cWidth = childAt.getMeasuredWidth();            int cHeight = childAt.getMeasuredHeight();            if (i == 0) {                cl = screenWidth / 2 - cWidth / 2;                ct = screenHeight / 2 - cHeight / 2;                cr = screenWidth / 2 + cWidth / 2;                cb = screenHeight / 2 + cHeight / 2;                mRects.add(new Rect(cl, ct, cr, cb));                childAt.layout(cl, ct, cr, cb);            } else if (i == 1) {                cl = screenWidth / 2 + getChildAt(0).getMeasuredWidth() / 2 - cWidth;                ct = screenHeight / 2 - getChildAt(0).getMeasuredHeight() / 2;                cr = screenWidth / 2 + getChildAt(0).getMeasuredWidth() / 2;                cb = screenHeight / 2 + cHeight - getChildAt(0).getMeasuredHeight() / 2;                mRects.add(new Rect(cl, ct, cr, cb));                childAt.layout(cl, ct, cr, cb);            } else {/*这个是第一个View的时候,展示在中间的*/                setLocationView(screenWidth, screenHeight, childAt, cWidth, cHeight);            }        }    }

之后是重点onLayout,我们这个需求是后台穿来多少个文字就展示多少个,在此我们先本地定一共有15个文字,之后再onLayout里生成这些文字,首先我们要先生成一个中间的圆,我没有直接生成圆,直接用的一个TextView,因为有人给切图吗。。

我就讲一下怎么确定这个中心圆的位置,Rect这个方法大家查阅过都应该知道四个参数传的是左上右下,但实际上有可能不太清楚,以我的理解通俗的讲一下,就是两个点的坐标,android是以左上点为0,0坐标,Rect的左上就是一个矩形的左上点坐标,右下,就是一个矩形的右下点坐标,基本android所有的控件都是矩形,所有这个大多都适用,之后我们看看怎么确定中间圆的位置的。

首先确定圆心,圆心位置是屏幕宽高的一半getWidth/2,getHight/2就是圆心坐标,那怎么确定圆的宽度和高度呢,就是确定包裹圆的正方形,这就好算了,正方形的左上就是,圆心减去圆的半径,右下就是圆心加上圆的半径,之后传入Rect就确定了中间大圆的位置。

接下来是剩下的文字随机位置了。

 private void setLocationView(int screenWidth, int screenHeight, View childAt, int cWidth, int cHeight) {        int cl;        int ct;        int cr;        int cb;        /*这里随机的是结束点*/        int randomWidth = mRandom.nextInt(screenWidth);        int randomHeight = mRandom.nextInt(screenHeight);        cl = randomWidth - cWidth;        ct = randomHeight - cHeight;        cr = randomWidth;        cb = randomHeight;        int distanceX = Math.abs(outr - randomWidth);        int distanceY = Math.abs(outr - randomHeight);        //点击位置与圆心的直线距离        int distanceZ = (int) Math.sqrt(Math.pow(distanceX, 2) + Math.pow(distanceY, 2));        if (cl < 0 || ct < 0 || distanceZ > outr) {            /*这里应该重新获取点重新计算*/            setLocationView(screenWidth, screenHeight, childAt, cWidth, cHeight);            return;        }        /*这里判断的是是否重合*/        Rect currentRect = new Rect(cl, ct, cr, cb);        for (int i = 0; i < mRects.size(); i++) {            Rect rect = mRects.get(i);            if (Rect.intersects(rect, currentRect)) {                setLocationView(screenWidth, screenHeight, childAt, cWidth, cHeight);                return;            }        }        childAt.layout(cl, ct, cr, cb);        mRects.add(new Rect(cl, ct, cr, cb));    }

剩下的文字位置的左上点都是用随机数生成的,这需要讲一下,生成的随机数是Textview的右下点,再减去测量的宽高就是TextView的左上点,之后传入Rect,(在这里我是自己想的或许有更好的方法,请大神们赐教啊),我在这里把所有的TextView的Rect都存了起来,然后用递归和intersects来判断是否重叠,重叠就不添加不重叠就添加这个TextView。

以上就是所有的主要逻辑,看着是不是不是很多,自己理解自定义控件后,想做啥就做啥。。

萌新写的不好大家勿怪哈。

还有个自定义拖动控件,等下次给大家讲。

我还不知道怎么把源码弄上去,要是有需要的加我qq啊,754178954

谢谢大家

原创粉丝点击