使用自定义Animation,实现VIEw的左右摇摆效果,如图所示:
代码很简单,直接上源码
activity_maini.xml布局文件:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="#ffffff" androID:gravity="center" androID:orIEntation="vertical"> <!--图片--> <ImageVIEw androID:ID="@+ID/iv_dial" androID:layout_wIDth="200dp" androID:layout_height="200dp" androID:src="@drawable/img"/> <!--控制按钮--> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_gravity="bottom" androID:gravity="center" androID:orIEntation="horizontal"> <button androID:ID="@+ID/btn_start" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="开始"/> <button androID:ID="@+ID/btn_end" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="结束"/> </linearLayout></linearLayout>
也可以用其它的VIEw控件替代ImageVIEw,都是可以实现摇摆效果的
主界面MainActivity
/** * 主界面 * Created by zhuwentao on 2016-08-08. */public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener{ /** 表盘图片 */ private ImageVIEw mDialiv; /** 开始按钮 */ private button mStartBtn; /** 结束按钮 */ private button mEndBtn; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); initUI(); initListener(); } /** * 初始化UI */ private voID initUI() { mDialiv = (ImageVIEw) findVIEwByID(R.ID.iv_dial); mStartBtn = (button) findVIEwByID(R.ID.btn_start); mEndBtn = (button) findVIEwByID(R.ID.btn_end); } /** * 初始化监听 */ private voID initListener() { mStartBtn.setonClickListener(this); mEndBtn.setonClickListener(this); } @OverrIDe public voID onClick(VIEw v) { switch (v.getID()) { case R.ID.btn_start: showAnimation(); break; case R.ID.btn_end: mDialiv.clearanimation(); break; } } /** * 设置动画 */ private voID showAnimation() { // 获取自定义动画实例 CustomrotateAnim rotateAnim = CustomrotateAnim.getCustomrotateAnim(); // 一次动画执行1秒 rotateAnim.setDuration(1000); // 设置为循环播放 rotateAnim.setRepeatCount(-1); // 设置为匀速 rotateAnim.setInterpolator(new linearInterpolator()); // 开始播放动画 mDialiv.startAnimation(rotateAnim); }}
setRepeatCount()设置的是重复播放动画的次数,-1是为了让它循环播放,setRepeatCount(0)代表的是执行一次,setRepeatCount(1)代表重复1次,即动画执行2次。
setInterpolator()方法是设置插值器,用来指定动画的效果,这里使用系统提供的linearInterpolator()匀速变化效果。
自定义的CustomrotateAnim动画需要继承Animation,这里只要实现它的initialize()和applytransformation()方法就好
/** * 左右摇摆动画 * Created by zhuwentao on 2016-08-08. */public class CustomrotateAnim extends Animation { /** 控件宽 */ private int mWIDth; /** 控件高 */ private int mHeight; /** 实例 */ private static CustomrotateAnim rotateAnim; /** * 获取动画实例 * @return 实例 */ public static CustomrotateAnim getCustomrotateAnim() { if (null == rotateAnim) { rotateAnim = new CustomrotateAnim(); } return rotateAnim; } @OverrIDe public voID initialize(int wIDth,int height,int parentWIDth,int parentHeight) { this.mWIDth = wIDth; this.mHeight = height; super.initialize(wIDth,height,parentWIDth,parentHeight); } @OverrIDe protected voID applytransformation(float interpolatedTime,transformation t) { // 左右摇摆 t.getMatrix().setRotate((float)(Math.sin(interpolatedTime*Math.PI*2)*50),mWIDth/2,mHeight/2); super.applytransformation(interpolatedTime,t); }}
initialize(int wIDth,int parentHeight)中,wIDth和height代表指定播放动画的VIEw空间宽高,parentWIDth和parentHeight代表该VIEw控件所在的父控件宽高。
我们需要使用当前VIEw的宽高来确定摇摆的旋转点,所以在initialize中获取VIEw控件的宽高。
applytransformation()方法是动画具体的实现方法,在系统绘制动画时会反复调用这个方法,每调用一次applytransformation()方法,其中的interpolatedTime参数都会改变一次,值从0到1递增,当interpolatedTime的值为1时则动画结束。
transformatio类是一个变换的矩阵,通过改变该矩阵就可以实现各种复杂的效果。
复写这个方法,在里面就可以实现我们自定义的动画效果了。
以上是内存溢出为你收集整理的Android自定义Animation实现View摇摆效果全部内容,希望文章能够帮你解决Android自定义Animation实现View摇摆效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)