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


猜你喜欢
- 由于经常用到文件处理,便自己封装了下 分享给大家。 包含写入文本 批量删除文件 下载文件 。--可直接使用/// <summary&g
- 大家好,这是 [C#.NET 拾遗补漏] 系列的第 07 篇文章。在 C# 中,大多数方法都是通过 return 语句立即把程序的控制权交回
- 概要:流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性
- 到底什么是反射呢???反射的核心就是JVM在运行时才动态加载类或调用方法,访问属性,它不需要事先(写代码的时候或编译期)知道运行对象是谁。每
- 一、什么是网关限流:在微服务架构中,网关层可以屏蔽外部服务直接对内部服务进行调用,对内部服务起到隔离保护的作用,网关限流,顾名思义,就是通过
- 这篇文章主要介绍了Java方法参数传递机制原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可
- 概述源码就是能够被用来执行,生成机器能够识别的代码,通过开源源码,可以引用其功能。重要性1、mybatis中的sql执行,不仅要知道返回的结
- AsyncTask是Android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度
- 本文实例为大家分享了java实现点击按钮事件弹出子窗口的具体代码,供大家参考,具体内容如下要求:1、在父窗口中添加一个按钮2、点击按钮弹出子
- 一、使用线程的理由1、可以使用线程将代码同其他代码隔离,提高应用程序的可靠性。2、可以使用线程来简化编码。3、可以使用线程来实现并发执行。二
- 0引言随着万维网的发展和大数据时代的到来,每天都有大量的数字化信息在生产、存储、传递和转化,如何从大量的信息中以一定的方式找到满足自己需求的
- 本文实例讲述了C#实现日期格式转换的公共方法类。分享给大家供大家参考,具体如下:这里演示了C#中一些日期格式的转换。创建公共方法类(Util
- 苹果上的UI基本上都是这个效果,然而Android机上的顶部状态栏总是和app的主题颜色不搭。还好如今的api19以上的版本,我们也能做出这
- 项目比较大有时候会比较卡,虽然有GC自动清理机制,但是还是有不尽人意的地方。所以尝试在项目启动文件中,手动写了一个定时器,定时清理内存,加快
- 前言之前写过 Mybatis Plus 的基本配置和使用。Mybatis-Plus 看这一篇就够了当初在进行查询时,虽然没有写硬SQL进行查
- “Java is still not dead—and people are starting to figure that out.”本教
- 概述什么是动态编程?动态编程解决什么问题?Java中如何使用?什么原理?如何改进?(需要我们一起探索,由于自己也是比较菜,一般深入不到这个程
- @RequestBody部分属性丢失问题描述JavaBean实现public class VerifyNewFriendApplyReq i
- 先给大家展示下效果图:1、验证码生成类:import java.util.Random;import java.awt.imag
- 前言前段时间因为工作的需要用到Spring事件,翻翻文档将功能实现了,但是存在少许理解不畅的地方,今天有空来梳理梳理。需求背景叶子同学在新入