Android自定义viewGroup实现点击动画效果

Android自定义viewGroup实现点击动画效果,第1张

概述本文实例为大家分享了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,defStyleAttr);    Log.i("jj",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,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,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,yflag * ct,0);        childVIEw.setClickable(true);        childVIEw.setFocusable(true);      } else      // to close      {        tranAnim = new TranslateAnimation(0,xflag * cl,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,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.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,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;  }});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android自定义viewGroup实现点击动画效果全部内容,希望文章能够帮你解决Android自定义viewGroup实现点击动画效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1143403.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存