Android自定义Animation实现View摇摆效果

所属分类: 软件编程 / Android 阅读数: 45
收藏 0 赞 0 分享

使用自定义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 height, int parentWidth, int parentHeight)中,width和height代表指定播放动画的View空间宽高,parentWidth和parentHeight代表该View控件所在的父控件宽高。
我们需要使用当前View的宽高来确定摇摆的旋转点,所以在initialize中获取View控件的宽高。

applyTransformation()方法是动画具体的实现方法,在系统绘制动画时会反复调用这个方法,每调用一次applyTransformation()方法,其中的interpolatedTime参数都会改变一次,值从0到1递增,当interpolatedTime的值为1时则动画结束。
Transformatio类是一个变换的矩阵,通过改变该矩阵就可以实现各种复杂的效果。
复写这个方法,在里面就可以实现我们自定义的动画效果了。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多