Android自定义viewGroup实现点击动画效果
作者:大城小小爱 发布时间:2022-09-01 00:27:07
标签:Android,viewGroup,点击动画
本文实例为大家分享了viewGroup实现点击动画效果展示的具体代码,供大家参考,具体内容如下
public class MyCustomView extends ViewGroup implements View.OnClickListener {
private OnMenuItemClickListener mMenuItemClickListener;
/**
* 点击子菜单项的回调接口
*/
public interface OnMenuItemClickListener {
void onClick(View view, int pos);
}
public void setOnMenuItemClickListener(
OnMenuItemClickListener mMenuItemClickListener) {
this.mMenuItemClickListener = mMenuItemClickListener;
}
public enum Status {
OPEN, CLOSE
}
private int mRadius;
/**
* 菜单的状态
*/
private Status mCurrentStatus = Status.CLOSE;
/**
* 菜单的主按钮
*/
private View mCButton;
public MyCustomView(Context context) {//通过new对象来调用
this(context, null);
Log.i("jj", "super(context)");
}
public MyCustomView(Context context, AttributeSet attrs) {//在布局中使用时调用
this(context, attrs, 0);
Log.i("jj", "super(context, attrs)");
}
public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Log.i("jj", "super(context, attrs, defStyleAttr)");
mRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
Log.i("jj", "mRadius1: " + mRadius);
// 获取自定义属性的值
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyleAttr, 0);
mRadius = (int) a.getDimension(R.styleable.MyCustomView_radius, TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100,
getResources().getDisplayMetrics()));
Log.i("jj", "mRadius: " + mRadius);
a.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
for (int i = 0; i < count; i++) {
// 测量child
measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (changed) {
layoutCButton();
int count = getChildCount();
for (int i = 0; i < count - 1; i++) {
View child = getChildAt(i + 1);
child.setVisibility(View.GONE);
int cl = (int) (mRadius * Math.sin(Math.PI / 2 / (count - 2)
* i));
int ct = (int) (mRadius * Math.cos(Math.PI / 2 / (count - 2)
* i));
int cWidth = child.getMeasuredWidth();
int cHeight = child.getMeasuredHeight();
ct = getMeasuredHeight() - cHeight - ct;
cl = getMeasuredWidth() - cWidth - cl;
child.layout(cl, ct, cl + cWidth, ct + cHeight);
}
}
}
private void layoutCButton() {
mCButton = getChildAt(0);
mCButton.setOnClickListener(this);
int width = mCButton.getMeasuredWidth();
int height = mCButton.getMeasuredHeight();
int l = getMeasuredWidth() - width;
int t = getMeasuredHeight() - height;
mCButton.layout(l, t, l + width, t + width);
}
@Override
public void onClick(View v) {
rotateCButton(v, 0f, 360f, 300);
toggleMenu(300);
}
private void rotateCButton(View v, float start, float end, int duration) {
RotateAnimation anim = new RotateAnimation(start, end,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
anim.setDuration(duration);
anim.setFillAfter(true);
v.startAnimation(anim);
}
/**
* 切换菜单
*/
public void toggleMenu(int duration) {
// 为menuItem添加平移动画和旋转动画
int count = getChildCount();
for (int i = 0; i < count - 1; i++) {
final View childView = getChildAt(i + 1);
childView.setVisibility(View.VISIBLE);
int cl = (int) (mRadius * Math.sin(Math.PI / 2 / (count - 2) * i));
int ct = (int) (mRadius * Math.cos(Math.PI / 2 / (count - 2) * i));
AnimationSet animset = new AnimationSet(true);
Animation tranAnim = null;
int xflag = 1;
int yflag = 1;
// to open
if (mCurrentStatus == Status.CLOSE) {
tranAnim = new TranslateAnimation(xflag * cl, 0, yflag * ct, 0);
childView.setClickable(true);
childView.setFocusable(true);
} else
// to close
{
tranAnim = new TranslateAnimation(0, xflag * cl, 0, yflag * ct);
childView.setClickable(false);
childView.setFocusable(false);
}
tranAnim.setFillAfter(true);
tranAnim.setDuration(duration);
tranAnim.setStartOffset((i * 100) / count);
tranAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if (mCurrentStatus == Status.CLOSE) {
childView.setVisibility(View.GONE);
}
}
});
// 旋转动画
RotateAnimation rotateAnim = new RotateAnimation(0, 720,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnim.setDuration(duration);
rotateAnim.setFillAfter(true);
animset.addAnimation(rotateAnim);
animset.addAnimation(tranAnim);
childView.startAnimation(animset);
final int pos = i + 1;
childView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mMenuItemClickListener != null)
mMenuItemClickListener.onClick(childView, pos);
menuItemAnim(pos - 1);
changeStatus();
}
});
}
// 切换菜单状态
changeStatus();
}
/**
* 添加menuItem的点击动画
*
* @param
*/
private void menuItemAnim(int pos) {
for (int i = 0; i < getChildCount() - 1; i++) {
View childView = getChildAt(i + 1);
if (i == pos) {
childView.startAnimation(scaleBigAnim(300));
} else {
childView.startAnimation(scaleSmallAnim(300));
}
childView.setClickable(false);
childView.setFocusable(false);
}
}
private Animation scaleSmallAnim(int duration) {
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnim = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
AlphaAnimation alphaAnim = new AlphaAnimation(1f, 0.0f);
animationSet.addAnimation(scaleAnim);
animationSet.addAnimation(alphaAnim);
animationSet.setDuration(duration);
animationSet.setFillAfter(true);
return animationSet;
}
/**
* 为当前点击的Item设置变大和透明度降低的动画
*
* @param duration
* @return
*/
private Animation scaleBigAnim(int duration) {
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnim = new ScaleAnimation(1.0f, 4.0f, 1.0f, 4.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
AlphaAnimation alphaAnim = new AlphaAnimation(1f, 0.0f);
animationSet.addAnimation(scaleAnim);
animationSet.addAnimation(alphaAnim);
animationSet.setDuration(duration);
animationSet.setFillAfter(true);
return animationSet;
}
public boolean isOpen() {
return mCurrentStatus == Status.OPEN;
}
/**
* 切换菜单状态
*/
private void changeStatus() {
mCurrentStatus = (mCurrentStatus == Status.CLOSE ? Status.OPEN
: Status.CLOSE);
}
}
attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="radius" format="dimension" />
<declare-styleable name="MyCustomView">
<attr name="radius" />
</declare-styleable>
</resources>
菜单布局文件:
<com.admom.mycanvas.view.MyCustomView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:hyman="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/id_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
hyman:radius="160dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/composer_button">
<ImageView
android:id="@+id/id_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/composer_icn_plus" />
</RelativeLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/composer_music"
android:tag="Music" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/composer_place"
android:tag="Place" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/composer_sleep"
android:tag="Sleep" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/composer_thought"
android:tag="Sun" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/composer_with"
android:tag="People" />
</com.admom.mycanvas.view.MyCustomView>
主界面布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<include layout="@layout/menu_right_bottom"/>
</RelativeLayout>
在主程序直接使用:
mMenu = (MyCustomView) findViewById(R.id.id_menu);
mMenu.setOnMenuItemClickListener(new MyCustomView.OnMenuItemClickListener() {
@Override
public void onClick(View view, int pos) {
Toast.makeText(MainActivity.this, pos + ":" + view.getTag(), Toast.LENGTH_SHORT).show();
}
});
mMenu.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(mMenu.isOpen()){
mMenu.toggleMenu(300);
}
return false;
}
});
来源:http://blog.csdn.net/Scorpio_gao/article/details/53153788
0
投稿
猜你喜欢
- 前言本文主要介绍下Spring事务中的传播行为。事务传播行为是Spring框架独有的事务增强特性,他不属于的事务实际提供方数据库行为。这是S
- 要求:1.配置文件的namespace名称空间指定为接口的全类名2.配置文件中的id唯一标识与接口中的方法对应(返回值类型对应,方法名对应,
- Android的布局管理器本身就是个UI组件,所有的布局管理器都是ViewGroup的子类,而ViewGroup是View的子类,所以布局管
- 这篇文章主要介绍了SpringCloud断路器Hystrix原理及用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参
- Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建
- 本文实例为大家分享了Java实现扑克牌程序的具体代码,供大家参考,具体内容如下思路:在实现之前,先要想好步骤,思路清晰才不会出错。要实现一副
- SpringBoot打jar包遇到的xml文件丢失在pom.xml的build标签中添加如下内容指定资源路径<resources>
- int n;int &m = n;在C++中,多了一个C语言没有的引用声明符&,如上,m就是n的引用,简单的说m就是n的别名
- 简介Spring Security,这是一种基于 Spring AOP 和 Servlet 过滤器的安全框架。它提供全面的安全性解决方案,同
- 本文实例讲述了Java链表(Linked List)基本原理与实现方法。分享给大家供大家参考,具体如下:在分析链表之前,我们先来对之前的动态
- 目录前言Hello World1.可以在 Spring Initializr上面添加,也可以手动在 pom.xml中添加如下代码∶2. 编写
- 我们知道,值类型的变量是在堆栈上分配内存的,而引用类型包括System.Object的对象是在堆上分配内存的,基于这一特点,当值类型被类型转
- 提示:建议一定要看后面的@RequestBody的核心逻辑源码以及六个重要结论!本文前半部分的内容都是一些基本知识常识,可选择性跳过。声明:
- 前言本文章主要从spring security安全认证登录内部调用流程来流程分析登录过程。一、登录时序图时序原图二、配置与代码1.引入库po
- springboot默认读取的配置文件名字是:“application.properties”和&a
- package com.java.db;import java.lang.reflect.Constructor;import java.l
- 1、导入资源2、JSP代码<div class="page-container">  
- 一、场景描述接《Java设计模式(一)工厂模式》工厂模式有一缺点,就是破坏了类的封闭性原则。例如,如果需要增加Word文件的数据采集,此时按
- 本文实例为大家分享了java实现随机数生成器的具体代码,供大家参考,具体内容如下自己编的随机数生成器,比较简陋,功能也单一,当作练手。App
- 本文分为俩部分:第一部分介绍如何给Eclipse安装Spring Boot开发插件spring tool suite(简称STS);第二部分