<?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>
/**
* 主界面
* 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);
}
}
/**
* 左右摇摆动画
* 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);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有