当前位置: 移动技术网 > 移动技术>移动开发>Android > Android自定义View实现圆形环绕效果

Android自定义View实现圆形环绕效果

2019年07月29日  | 移动技术网移动技术  | 我要评论

之前项目中需要实现一个四周环绕中心圆形头像的效果,感觉还是自定义比较方便,于是就自己封装了一个控件去实现。先贴张图显示最终效果。

首先自定义一个view继承自linearlayout,通过动态添加childview的方式将子控件添加到view中。思路是先添加中间圆形头像,然后添加周围的小图标。

1.实现了圆形头像的显示,可以去参考网上或github上的demo,圆形头像的外圈其实是一个view,然后再把头像这个view盖到这个view上。

2.计算好周围相邻view之间的角度,这里是要水平铺满,最多6个,所以相邻之间的角度为180/ (6 - 1) = 36度。如果是360环绕,放n个图标,则相邻之间的角度应该为360 / n 。

3.设置图标到圆心的距离r。距离要大于头像的半径加上图标的半径。

4.确定图标的坐标:控件的宽为width,高为height。假设左边第一个图标是起始位置。图标的起始角度为α= 180 - 36 * i,则它的横坐标为width/2 + cos(α)r,纵坐标为height/2 - sin(α) r。

5.设置坐标点,默认为图标的左上角顶点和右下点,如果想设图标的中心点为坐标,则左上定点x、y分别减去width/2和height/2,右下角分别加上width/2、height/2。

下面附上主要代码:

package com.ihaveu.iuzuan.cardgame.widget;

import android.content.context;
import android.support.annotation.nullable;
import android.util.attributeset;
import android.view.layoutinflater;
import android.view.view;
import android.widget.linearlayout;

import com.ihaveu.iuzuan.cardgame.r;
import com.ihaveu.iuzuan.cardgame.util.measureutil;

import java.util.arraylist;
import java.util.list;

/**
 * created by zhouhui on 17-6-8.
 * 添加圆形子控件实现时钟环绕效果
 */

public class circleimagelayout extends linearlayout{

 private double mangle = 0;//初始角度
 private int mx, my;//子控件位置
 private int mwidth, mheight;//控件长宽
 private int mradius;//子控件距离控件圆心位置
 private int mcount;
 private list<circleimageview> mcircleimageviewlist;
 private circleimageview mcircleimageview;

 public circleimagelayout(context context) {
  this(context, null);
 }

 public circleimagelayout(context context, @nullable attributeset attrs) {
  this(context, attrs, 0);
 }

 public circleimagelayout(context context, @nullable attributeset attrs, int defstyleattr) {
  super(context, attrs, defstyleattr);
  mcircleimageviewlist = new arraylist<>();
 }

 /**
  * 设置子控件到控件圆心的位置
  */
 public void setradius(int radius) {
  mradius = radius;
 }

 @override
 protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
  super.onmeasure(widthmeasurespec, heightmeasurespec);
  mwidth = getmeasuredwidth();
  mheight = getmeasuredheight();
 }

 @override
 protected void onlayout(boolean changed, int left, int top, int right, int bottom) {
  super.onlayout(changed, left, top, right, bottom);
  initdraw();
 }

 public void initdraw() {
  mcount = getchildcount();
  for (int i = 0; i < mcount; i++) {
   view child = getchildat(i);
   child.getwidth();
   child.getheight();
   if (i == 0) {
    mx = mwidth / 2;
    my = mheight / 2;
   } else {
    mangle = 180 - 180 / (mcount - 1) * (i - 1);
    mx = (int) (mwidth / 2 + math.cos(math.toradians(mangle)) * mradius);
    my = (int) (mheight / 2 - math.sin(math.toradians(mangle)) * mradius);
   }
   child.layout(mx - child.getwidth() / 2, my - child.getheight() / 2, mx + child.getwidth() / 2, my + child.getheight() / 2);
  }
 }

 /**
  * 初始化环绕数量半径
  */
 public void init(int count, int radius) {
  mradius = radius;
  for (int i = 0; i < count + 1; i++) {
   circleimageview imageview = new circleimageview(getcontext());
   if (i == 0) {
    //i为0时为圆型头像
    view view = layoutinflater.from(getcontext()).inflate(r.layout.layout_header, null, true);
    mcircleimageview = (circleimageview) view.findviewbyid(r.id.iv_header);
    addview(view);
   } else {
    addview(imageview, measureutil.dip2px(15), measureutil.dip2px(15));
    mcircleimageviewlist.add(imageview);
   }
  }
 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网