Android自定义VIew实现卫星菜单效果浅析
作者:wolf艺术 发布时间:2022-09-23 22:44:43
一 概述:
最近一直致力于Android自定义VIew的学习,主要在看《android群英传》,还有CSDN博客鸿洋大神和wing大神的一些文章,写的很详细,自己心血来潮,学着写了个实现了类似卫星效果的一个自定义的View,分享到博客上,望各位指点一二。写的比较粗糙,见谅。(因为是在Linux系统下写的,效果图我直接用手机拍的,难看,大家讲究下就看个效果,勿喷)。
先来看个效果图,有点不忍直视:
自定义VIew准备:
(1)创建继承自View的类;
(2)重写构造函数;
(3)定义属性。
(4)重写onMeasure(),onLayout()方法。
好了,废话不说了,准备上菜。
二 相关实现
首先是自定义的View,重写构造函数,我这里是直接继承的VIewGroup,贴上代码:
public MoonView(Context context) {
this(context,null);
}
public MoonView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public MoonView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
这里需要读取自定义的属性,所以调用含三个参数的构造函数。
自定义的属性,我这里知定义了两个,一个是菜单弧形的半径,还有个是菜单在屏幕的位置,这里可以设置在左上角,左下角,右上角,右下角。代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MoonAttrs">
<attr name="mRadius" format="integer"></attr><!--菜单圆形半径-->
<attr name="mPosition"><!--卫星菜单屏幕所在位置-->
<enum name="leftTop" value="-2"></enum><!--左上角-->
<enum name="leftBottom" value="-1"></enum><!--左下角-->
<enum name="rightTop" value="-3"></enum><!--右上角-->
<enum name="rightBottom" value="-4"></enum><!--右下角-->
</attr>
</declare-styleable>
</resources>
然后在布局文件里面引用自定义的View,配置属性:
<?xml version="1.0" encoding="utf-8"?>
<com.example.liujibin.testmyview3.myView.MoonView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.example.liujibin.testmyview3"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
custom:mRadius="400"
custom:mPosition="rightBottom"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/sapi_icon_add_account"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/sapi_icon_add_account"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/sapi_icon_add_account"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/sapi_icon_add_account"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/sapi_icon_add_account"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/sapi_icon_add_account"/>
</com.example.liujibin.testmyview3.myView.MoonView>
最后我们需要在自定义的View类中的构造函数里,获取相关的属性值:
public MoonView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//获取相关属性
TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MoonAttrs,
defStyleAttr,0);
mRaius = ta.getInt(R.styleable.MoonAttrs_mRadius,500);
position = ta.getInt(R.styleable.MoonAttrs_mPosition,-1);
}
做完以上的准备工作,我们就可以对组件进行测量,布局。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
count = getChildCount()-1;
angle = 90/(count-1);
int count = getChildCount();
for(int i =0;i< count;i++){
measureChild(getChildAt(i),widthMeasureSpec,heightMeasureSpec);
}
}
count获取按钮的数量,有一个是中心点,不参与计算,angle是每个按钮离基准线的角度,这里以90度为准,固定在这个范围里面均匀分配。
首先先把中心点固定好位置:
@Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
if(isChanged){
layoutBottom();
}
}
private void layoutBottom(){
View view = getChildAt(0);
switch (position){
case -1:
btml = 0;
btmt = getMeasuredHeight() - view.getMeasuredHeight();
btmr = view.getMeasuredWidth();
btmb = getMeasuredHeight();
break;
case -2:
btml = 0;
btmt = 0;
btmr = view.getMeasuredWidth();
btmb = view.getMeasuredHeight();
break;
case -3:
btml = getMeasuredWidth() - view.getMeasuredWidth();
btmt = 0;
btmr = getMeasuredWidth();
btmb = view.getMeasuredHeight();
break;
case -4:
btml = getMeasuredWidth() - view.getMeasuredWidth();
btmt = getMeasuredHeight() - view.getMeasuredHeight();
btmr = getMeasuredWidth();
btmb = getMeasuredHeight();
break;
}
btmWidth = view.getMeasuredWidth();
btmHeight = view.getMeasuredHeight();
view.setOnClickListener(this);
view.layout(btml,btmt,btmr,btmb);
}
position的值看属性就明白了,对中心点进行固定位置。并且注册点击事件。
现在开始给剩下的按钮布局,并隐藏按钮:
@Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
if(isChanged){
layoutBottom();
int count = getChildCount();
for(int k = 0;k < count - 1;k++){
View view = getChildAt(k+1);
int childWidth = view.getMeasuredWidth();
int childHeight = view.getMeasuredHeight();
int childX = (int)(mRaius*(Math.sin(angle*(k)*Math.PI/180)));
int childY = (int)(mRaius*(Math.cos(angle*(k)*Math.PI/180)));
int left = 0;
int top = 0;
int right = 0;
int bottom = 0;
switch(position){
case -1:
left = childX+btmWidth/2-childWidth/2;
top =getMeasuredHeight() - (childY+childHeight/2+btmHeight/2);
right = childX+btmWidth/2+childWidth/2;
bottom =getMeasuredHeight() - (childY + btmHeight/2) + childHeight/2;
break;
case -2:
left = childX+btmWidth/2-childWidth/2;
top =childY-childHeight/2+btmHeight/2;
right = childX+btmWidth/2+childWidth/2;
bottom = childY + btmHeight/2 + childHeight/2;
break;
case -3:
left = getMeasuredWidth() - (childX+btmWidth/2+childWidth/2);
top =childY-childHeight/2+btmHeight/2;
right = getMeasuredWidth() - (childX+btmWidth/2+childWidth/2)+childWidth;
bottom = childY + btmHeight/2 + childHeight/2;
break;
case -4:
left = getMeasuredWidth() - (childX+btmWidth/2+childWidth/2);
top =getMeasuredHeight() - (childY+childHeight/2+btmHeight/2);
right = getMeasuredWidth() - (childX+btmWidth/2+childWidth/2)+childWidth;
bottom =getMeasuredHeight() - (childY + btmHeight/2) + childHeight/2;
break;
}
view.layout(left,top,right,bottom);
view.setVisibility(View.GONE);
}
}
}
现在我们实现点击事件:
@Override
public void onClick(View view) {
if(isChanged){
int count = getChildCount();
for(int i = 0;i < count - 1;i++){
View childView = getChildAt(i+1);
int childX = (int)(mRaius*(Math.sin(angle*(i)*Math.PI/180)));
int childY = (int)(mRaius*(Math.cos(angle*(i)*Math.PI/180)));
int childWidth = view.getMeasuredWidth();
int childHeight = view.getMeasuredHeight();
int left = 0;
int top = 0;
TranslateAnimation ta = null;
switch(position){
case -1:
left = childX+btmWidth/2-childWidth/2;
top =getMeasuredHeight() - (childY+childHeight/2+btmHeight/2);
ta = new TranslateAnimation(-(left+childView.getMeasuredWidth()),0,getMeasuredHeight()-top,0);
break;
case -2:
left = childX+btmWidth/2-childWidth/2;
top =childY-childHeight/2+btmHeight/2;
ta = new TranslateAnimation(-(left+childView.getMeasuredWidth()),0,-top,0);
break;
case -3:
left = getMeasuredWidth() - (childX+btmWidth/2+childWidth/2);
top =childY-childHeight/2+btmHeight/2;
ta = new TranslateAnimation(getMeasuredWidth()-(left+childView.getMeasuredWidth()),0,-top,0);
break;
case -4:
left = getMeasuredWidth() - (childX+btmWidth/2+childWidth/2);
top =getMeasuredHeight() - (childY+childHeight/2+btmHeight/2);
ta = new TranslateAnimation(getMeasuredWidth()-(left+childView.getMeasuredWidth()),0,getMeasuredHeight()-top,0);
break;
}
ta.setDuration(500);
childView.setAnimation(ta);
childView.setVisibility(View.VISIBLE);
}
isChanged = false;
}else{
int count = getChildCount();
for(int i = 0;i < count - 1;i++){
View childView = getChildAt(i+1);
int childX = (int)(mRaius*(Math.sin(angle*(i)*Math.PI/180)));
int childY = (int)(mRaius*(Math.cos(angle*(i)*Math.PI/180)));
int childWidth = view.getMeasuredWidth();
int childHeight = view.getMeasuredHeight();
int left = 0;
int top = 0;
TranslateAnimation ta = null;
switch(position){
case -1:
left = childX+btmWidth/2-childWidth/2;
top =getMeasuredHeight() - (childY+childHeight/2+btmHeight/2);
ta = new TranslateAnimation(0,-(left+childView.getMeasuredWidth()),0,getMeasuredHeight()-top);
break;
case -2:
left = childX+btmWidth/2-childWidth/2;
top =childY-childHeight/2+btmHeight/2;
ta = new TranslateAnimation(0,-(left+childView.getMeasuredWidth()),0,-top);
break;
case -3:
left = getMeasuredWidth() - (childX+btmWidth/2+childWidth/2);
top =childY-childHeight/2+btmHeight/2;
ta = new TranslateAnimation(0,getMeasuredWidth()-(left+childView.getMeasuredWidth()),0,-top);
break;
case -4:
left = getMeasuredWidth() - (childX+btmWidth/2+childWidth/2);
top =getMeasuredHeight() - (childY+childHeight/2+btmHeight/2);
ta = new TranslateAnimation(0,getMeasuredWidth()-(left+childView.getMeasuredWidth()),0,getMeasuredHeight()-top);
break;
}
ta.setDuration(500);
childView.setAnimation(ta);
childView.setVisibility(View.GONE);
}
isChanged = true;
}
}
设置点击显示以及隐藏,并且带飘动的动画效果。
四个角落效果如下:
以上所述是小编给大家介绍的Android自定义VIew实现卫星菜单效果浅析网站的支持!
来源:http://blog.csdn.net/wolf6699/article/details/53065752


猜你喜欢
- 在Activity之间传递数据还可以利用一些技巧,不管windows还是Linux操作系统,都会支持一种叫剪切板的技术,也就是某一个程序将一
- 本文实例讲述了Java Swing实现JTable检测单元格数据变更事件的方法。分享给大家供大家参考,具体如下:在JTable的初级教程中往
- 本文实例为大家分享了Android仿大众点评星星评分控件的具体代码,供大家参考,具体内容如下话不多说,直接上代码,这里采用的是自定Viewp
- 比如在窗体中显示时间:错误思路一:我在窗体结构函数中写入一个死循环,每隔一秒显示一次当前时间public Form6() &n
- 这个破碎动画,是一种类似小米系统删除应用时的 * 破碎效果的动画。效果图展示先来看下是怎样的动效,要是感觉不是理想的学习目标,就跳过,避免浪费
- 1.servlet:定义:接口2.配置servlet:public class HelloServlet extends HttpServl
- 绪论转眼间,2016伴随着互联网寒冬和帝都的雾霾马上就过去了,不知道大家今年一整年过得怎么样?最近票圈被各个城市的雾霾刷屏,内心难免会动荡,
- 一、简述首先,Java 8引入了java.time.LocalDate来表示一个没有时间的日期。其次,使用Java 8版本,还需要更新jav
- PagerBottomTabStrip 是一个基本按谷歌Material Design规范完成的安卓底部导航栏控件官方设计规范:https:
- 一、问题描述平时我们在完成某些数据的入库后,发布了一个事件,此时使用的是@EventListener,然后在这个事件中,又去对刚才入库的数据
- Android setButtonDrawable()的兼容问题解决办法setButtonDrawable()的兼容问题API1
- WebService是一种传统的SOA技术架构,它不依赖于任何的编程语言,也不依赖于任何的技术平台,可以直接基于HTTP协议实现网络应用间的
- 1. 启用HTTPS修改配置application.ymlserver:# port: 80 port: 443 s
- 在开发中我们经常需要把我们的应用设置为全屏,有两种方法,一中是在代码中设置,另一种方法是在配置文件里改!一、在代码中设置:package c
- 使用Scroller实现绚丽的ListView左右滑动删除Item效果这里来给大家带来使用Scroller的小例子,同时也能用来帮助初步解除
- 问题:最近在项目中遇到,不同客户机安装不同Office版本,在导出Excel时,发生错误。找不到Excel Com组件,错误信息如下。&nb
- 经典分布式事务,是相对互联网中的柔性分布式事务而言,其特性为ACID原则,包括原子性(Atomictiy)、一致性(Consistency)
- 前言:反射(Reflection)是.NET提供给开发者的一个强大工具,尽管作为.NET框架的使用者,很多时候不会用到反射。但在一些情况下,
- 前文本章是关于Java流程控制语句的最全汇总,本篇为汇总上篇。流程是人们生活中不可或缺的一部分,它表示人们每天都在按照一定的流程做事。比如出
- 本文通过C#程序代码展示如何给PDF文档添加可视化数字签名和不可见数字签名。可视化数字签名,即在PDF文档中的指定页面位置添加签名,包含相关