Android自定義View實現(xiàn)圓形環(huán)繞效果
之前項目中需要實現(xiàn)一個四周環(huán)繞中心圓形頭像的效果,感覺還是自定義比較方便,于是就自己封裝了一個控件去實現(xiàn)。先貼張圖顯示最終效果。
首先自定義一個View繼承自LinearLayout,通過動態(tài)添加childView的方式將子控件添加到View中。思路是先添加中間圓形頭像,然后添加周圍的小圖標(biāo)。
1.實現(xiàn)了圓形頭像的顯示,可以去參考網(wǎng)上或github上的demo,圓形頭像的外圈其實是一個View,然后再把頭像這個View蓋到這個View上。
2.計算好周圍相鄰view之間的角度,這里是要水平鋪滿,最多6個,所以相鄰之間的角度為180/ (6 - 1) = 36度。如果是360環(huán)繞,放n個圖標(biāo),則相鄰之間的角度應(yīng)該為360 / n 。
3.設(shè)置圖標(biāo)到圓心的距離r。距離要大于頭像的半徑加上圖標(biāo)的半徑。
4.確定圖標(biāo)的坐標(biāo):控件的寬為width,高為height。假設(shè)左邊第一個圖標(biāo)是起始位置。圖標(biāo)的起始角度為α= 180 - 36 * i,則它的橫坐標(biāo)為width/2 + cos(α)r,縱坐標(biāo)為height/2 - sin(α) r。
5.設(shè)置坐標(biāo)點,默認為圖標(biāo)的左上角頂點和右下點,如果想設(shè)圖標(biāo)的中心點為坐標(biāo),則左上定點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. * 添加圓形子控件實現(xiàn)時鐘環(huán)繞效果 */ 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<>(); } /** * 設(shè)置子控件到控件圓心的位置 */ 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); } } /** * 初始化環(huán)繞數(shù)量半徑 */ 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); } } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android使用CircleImageView實現(xiàn)圓形頭像的方法
- Android布局自定義Shap圓形ImageView可以單獨設(shè)置背景與圖片
- Android實現(xiàn)圓角矩形和圓形ImageView的方式
- Android開發(fā)中TextView 實現(xiàn)右上角跟隨文本動態(tài)追加圓形紅點
- Android項目實戰(zhàn)手把手教你畫圓形水波紋loadingview
- Android自定義控件之圓形、圓角ImageView
- 詳解Android中Glide與CircleImageView加載圓形圖片的問題
- Android 自定義圓形頭像CircleImageView支持加載網(wǎng)絡(luò)圖片的實現(xiàn)代碼
- Android自定義view實現(xiàn)圓形與半圓形菜單
- Android中使用CircleImageView和Cardview制作圓形頭像的方法
相關(guān)文章
Android Application存取公共數(shù)據(jù)的實例詳解
這篇文章主要介紹了Android Application存取公共數(shù)據(jù)的實例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07Android自定義HorizontalScrollView打造超強Gallery效果
這篇文章主要介紹了Android自定義HorizontalScrollView打造圖片橫向滑動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05Android開發(fā)EditText禁止輸入監(jiān)聽及InputFilter字符過濾
這篇文章主要為大家介紹了Android開發(fā)EditText禁止輸入監(jiān)聽及InputFilter字符過濾示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06實時獲取股票數(shù)據(jù)的android app應(yīng)用程序源碼分享
本文我們分享一個實時獲取股票數(shù)據(jù)的android app應(yīng)用程序源碼分享,可以作為學(xué)習(xí)使用,本文貼出部分重要代碼,需要的朋友可以參考下本文2015-09-09