星空网 > 软件开发 > 操作系统

自定义控件——安卓旋转动画

                                       自定义控件——安卓旋转动画images/loading.gif' data-original="http://images2015.cnblogs.com/blog/781624/201512/781624-20151216130420693-1125998581.png" />

RotateAnimation (float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 
参数说明: 
float fromDegrees:旋转的开始角度。 
float toDegrees:旋转的结束角度。 
int pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。 
float pivotXValue:X坐标的伸缩值。 
int pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。 
float pivotYValue:Y坐标的伸缩值。

 

//animation.setRepeatCount(int repeatCount);//设置重复次数 
//animation.setFillAfter(boolean);//动画执行完后是否停留在执行完的状态 
//animation.setStartOffset(long startOffset);//执行前的等待时间

package com.org.demo.youku;import com.org.wangfeng.R;import android.app.Activity;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageView;import android.widget.RelativeLayout;public class MainActivity extends Activity implements OnClickListener {  private ImageView iv_home, iv_menu;  private RelativeLayout level1, level2, level3;  private boolean isShowLevel2 = true;// 是否显示2级菜单  private boolean isShowLevel3 = true;// 是否显示3级菜单  private boolean isShowMenu = true;// 是否显示整个菜单,包括1级,2级,3级的菜单  @Override  protected void onCreate(Bundle savedInstanceState) {    // TODO Auto-generated method stub    super.onCreate(savedInstanceState);    setContentView(R.layout.mainyouku);    iv_home = (ImageView) findViewById(R.id.iv_home);    iv_menu = (ImageView) findViewById(R.id.iv_menu);    level1 = (RelativeLayout) findViewById(R.id.level1);    level2 = (RelativeLayout) findViewById(R.id.level2);    level3 = (RelativeLayout) findViewById(R.id.level3);    iv_home.setOnClickListener(this);    iv_menu.setOnClickListener(this);  }  @Override  public void onClick(View arg0) {    // TODO Auto-generated method stub    switch (arg0.getId()) {    case R.id.iv_home:      if (AnimUtil.animCount != 0) {        // 说明有动画在执行        return;      }      if (isShowLevel2) {        // 需要隐藏        int startOffset = 0;        if (isShowLevel3) {          AnimUtil.closeMenu(level3, startOffset);          startOffset += 200;          isShowLevel3 = false;        }        AnimUtil.closeMenu(level2, startOffset);      } else {        // 需要显示        AnimUtil.showMenu(level2, 0);      }      isShowLevel2 = !isShowLevel2;      break;    case R.id.iv_menu:      if (AnimUtil.animCount != 0) {        // 说明有动画在执行        return;      }      if (isShowLevel3) {        // 隐藏3级菜单        AnimUtil.closeMenu(level3, 0);      } else {        // 显示3级菜单        AnimUtil.showMenu(level3, 0);      }      isShowLevel3 = !isShowLevel3;      break;    default:      break;    }  }  @Override  public boolean onKeyDown(int keyCode, KeyEvent event) {    if (keyCode == KeyEvent.KEYCODE_MENU) {      if (isShowMenu) {        // 需要关闭所有菜单        int startOffset = 0;        if (isShowLevel3) {          AnimUtil.closeMenu(level3, startOffset);          isShowLevel3 = false;          startOffset += 200;        }        if (isShowLevel2) {          AnimUtil.closeMenu(level2, startOffset);          isShowLevel2 = false;          startOffset += 200;        }        AnimUtil.closeMenu(level1, startOffset);      } else {        // 需要显示所有菜单        AnimUtil.showMenu(level1, 0);        AnimUtil.showMenu(level2, 200);        isShowLevel2 = true;        AnimUtil.showMenu(level3, 400);        isShowLevel3 = true;      }      isShowMenu = !isShowMenu;      return true;    }    return super.onKeyDown(keyCode, event);  }}

package com.org.demo.youku;import android.view.animation.Animation;import android.view.animation.RotateAnimation;import android.view.animation.Animation.AnimationListener;import android.widget.RelativeLayout;public class AnimUtil {  public static int animCount = 0;// 记录当前执行的动画数量  public static void closeMenu(RelativeLayout rl, int startOffset) {    for (int i = 0; i < rl.getChildCount(); i++) {      rl.getChildAt(i).setEnabled(false);    }    // pivotXValue: 0-1    RotateAnimation animation = new RotateAnimation(0, -180,        RotateAnimation.RELATIVE_TO_SELF, 0.5f,        RotateAnimation.RELATIVE_TO_SELF, 1);    animation.setDuration(500);    animation.setFillAfter(true);// 动画结束后保持当时的状态    animation.setStartOffset(startOffset);    animation.setAnimationListener(new MyAnimationListener());    rl.startAnimation(animation);  }  public static void showMenu(RelativeLayout rl, int startOffset) {    for (int i = 0; i < rl.getChildCount(); i++) {      rl.getChildAt(i).setEnabled(true);    }    RotateAnimation animation = new RotateAnimation(-180, 0,        RotateAnimation.RELATIVE_TO_SELF, 0.5f,        RotateAnimation.RELATIVE_TO_SELF, 1);    animation.setDuration(500);    animation.setFillAfter(true);// 动画结束后保持当时的状态    animation.setStartOffset(startOffset);    animation.setAnimationListener(new MyAnimationListener());    rl.startAnimation(animation);  }  static class MyAnimationListener implements AnimationListener {    @Override    public void onAnimationStart(Animation animation) {      animCount++;    }    @Override    public void onAnimationEnd(Animation animation) {      animCount--;    }    @Override    public void onAnimationRepeat(Animation animation) {    }  }}

<?  android:layout_width="match_parent"  android:layout_height="match_parent" >  <!-- -->  <RelativeLayout    android:layout_width="100dp"    android:id="@+id/level1"    android:layout_height="50dp"    android:layout_alignParentBottom="true"    android:layout_centerHorizontal="true"    android:background="@drawable/level1" >    <ImageView      android:id="@+id/iv_home"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_centerInParent="true"      android:background="@drawable/icon_home"      android:contentDescription="@null" />  </RelativeLayout>  <RelativeLayout    android:id="@+id/level2"    android:layout_width="180dp"    android:layout_height="90dp"    android:layout_alignParentBottom="true"    android:layout_centerHorizontal="true"    android:background="@drawable/level2" >    <ImageView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignParentBottom="true"      android:layout_marginBottom="10dp"      android:layout_marginLeft="10dp"      android:background="@drawable/icon_search"      android:contentDescription="@null" />    <ImageView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignParentBottom="true"      android:layout_alignParentRight="true"      android:layout_marginBottom="10dp"      android:layout_marginRight="10dp"      android:background="@drawable/icon_myyouku"      android:contentDescription="@null" />    <ImageView      android:id="@+id/iv_menu"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_centerHorizontal="true"      android:layout_marginTop="5dp"      android:background="@drawable/icon_menu"      android:contentDescription="@null" />  </RelativeLayout>  <RelativeLayout    android:id="@+id/level3"    android:layout_width="280dp"    android:layout_height="142dp"    android:layout_alignParentBottom="true"    android:layout_centerHorizontal="true"    android:background="@drawable/level3" >    <ImageView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignParentBottom="true"      android:layout_marginBottom="15dp"      android:layout_marginLeft="12dp"      android:background="@drawable/channel1"      android:contentDescription="@null" />    <ImageView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignParentBottom="true"      android:layout_alignParentRight="true"      android:layout_marginBottom="15dp"      android:layout_marginRight="12dp"      android:background="@drawable/channel5"      android:contentDescription="@null" />    <ImageView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignParentBottom="true"      android:layout_marginBottom="55dp"      android:layout_marginLeft="32dp"      android:background="@drawable/channel2"      android:contentDescription="@null" />    <ImageView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignParentBottom="true"      android:layout_alignParentRight="true"      android:layout_marginBottom="55dp"      android:layout_marginRight="32dp"      android:background="@drawable/channel6"      android:contentDescription="@null" />    <ImageView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignParentBottom="true"      android:layout_marginBottom="85dp"      android:layout_marginLeft="62dp"      android:background="@drawable/channel3"      android:contentDescription="@null" />    <ImageView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignParentBottom="true"      android:layout_alignParentRight="true"      android:layout_marginBottom="85dp"      android:layout_marginRight="62dp"      android:background="@drawable/channel7"      android:contentDescription="@null" />    <ImageView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_centerHorizontal="true"      android:layout_marginTop="5dp"      android:background="@drawable/channel4"      android:contentDescription="@null" />  </RelativeLayout></RelativeLayout>

 




原标题:自定义控件——安卓旋转动画

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

好消息!Lazada在线客服服务时间延长至21点:https://www.goluckyvip.com/news/4989.html
2019年物流行业细分领域人才需求及薪酬指南!:https://www.goluckyvip.com/news/499.html
亚马逊将在德国市场加码!新建8个仓库,招聘3000名员工!:https://www.goluckyvip.com/news/4990.html
亚马逊自配送订单"预付费退货标签计划"解读:https://www.goluckyvip.com/news/4991.html
Shopee大促如何引流,Shopee大促“三件套”收好!:https://www.goluckyvip.com/news/4992.html
TikTok 在美国的平均观看时长超过了 YouTube:https://www.goluckyvip.com/news/4993.html
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
中国有哪些著名的酒店品牌。:https://www.vstour.cn/a/411242.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流