a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

Android自定義View實現圓形環繞效果

2019-10-21 21:26:02
字體:
來源:轉載
供稿:網友

之前項目中需要實現一個四周環繞中心圓形頭像的效果,感覺還是自定義比較方便,于是就自己封裝了一個控件去實現。先貼張圖顯示最終效果。

Android,自定義View,圓形環繞

首先自定義一個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);   }  } }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 成人免费视频网 | 99久久久久国产精品免费 | 中文一区二区 | 久久久精品影院 | 国产成人精品午夜视频' | 中文字幕在线不卡 | 91精品久久久久久久 | 青青青免费视频在线 | 日韩欧美一区二区视频 | 好大好爽快点深一点陶软 | 精品在线免费播放 | 男女视频在线 | 黄a在线| 中文字幕在线免费视频 | 中文字幕在线一区 | 男女网站在线观看 | 天天操天天插天天干 | 国产在线a | 国产激情偷乱视频一区二区三区 | 欧美日韩中文在线观看 | 欧美日韩在线精品 | 黑料社区av在线 | 亚洲大胆人体视频 | 超碰97国产精品人人cao | 亚洲一区二区三区四区五区午夜 | 欧美一级一区 | 久草视频网址 | 久久久高清| 久久精品99国产精品亚洲最刺激 | 91色在线 | 亚洲一区二区中文 | 黄色网址av | 欧美日韩视频在线第一区 | 在线色网址 | 亚洲一区久久 | 在线观看国产视频 | 国产精品免费一区二区三区四区 | 久久成人综合网 | 日韩一区二区在线观看 | 午夜一区二区三区 | 在线播放黄 |