代码很简单,直接上源码
activity_maini.xml布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns: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"><!--图片--><ImageViewandroid:id="@+id/iv_dial"android:layout_width="200dp"android:layout_height="200dp"android:src="@drawable/img"/><!--控制按钮--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="bottom"android:gravity="center"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_start"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="开始"/><Buttonandroid:id="@+id/btn_end"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="结束"/></LinearLayout></LinearLayout>也可以用其它的View控件替代ImageView,都是可以实现摇摆效果的
/** * 主界面 * Created by zhuwentao on 2016-08-08. */public class MainActivity extends AppCompatActivity implements View.OnClickListener{/** 表盘图片 */private ImageView mDialIv;/** 开始按钮 */private Button mStartBtn;/** 结束按钮 */private Button mEndBtn;@Overrideprotected 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);}@Overridepublic 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次。
/** * 左右摇摆动画 * 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;}@Overridepublic void initialize(int width, int height, int parentWidth, int parentHeight) {this.mWidth = width;this.mHeight = height;super.initialize(width, height, parentWidth, parentHeight);}@Overrideprotected 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控件所在的父控件宽高。