实现了简单的旋转动画
2015-12-30 09:38
344 查看
简单的实现了以上动画,代码如下:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Animation animation = null; private ImageView img; private Button rotate_center; private Button rotateX; private Button rotateY; private static int count=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setListener(); } private void setListener() { rotate_center.setOnClickListener(this); rotateX.setOnClickListener(this); rotateY.setOnClickListener(this); } private void initView() { img = ((ImageView) findViewById(R.id.img)); rotate_center = ((Button) findViewById(R.id.rotate_center)); rotateX = ((Button) findViewById(R.id.rotateX)); rotateY = ((Button) findViewById(R.id.rotateY)); } /* * 绕中心逆时针旋转0-180 * */ private void rotate_center_n() { Animation operatingAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_n); LinearInterpolator lin = new LinearInterpolator(); operatingAnim.setInterpolator(lin); operatingAnim.setFillAfter(true); if (operatingAnim != null) { img.startAnimation(operatingAnim); } } /* * 绕中心顺时针旋转 恢复原位置 -180-0 * */ private void rotate_center_s() { Animation operatingAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_s); LinearInterpolator lin = new LinearInterpolator(); operatingAnim.setInterpolator(lin); operatingAnim.setFillAfter(true); if (operatingAnim != null) { img.startAnimation(operatingAnim); } } private void rotate (){ animation = new RotateAnimation(0, 90, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f); animation.setDuration(3000); img.startAnimation(animation); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.rotate_center: count++; if (count%2==1){ rotate_center_n(); }else { rotate_center_s(); } break; case R.id.rotateX: startActivity(new Intent(MainActivity.this,SecondActivity.class)); break; case R.id.rotateY: RotateAnimation r=new RotateAnimation(0f,90f,50,50); rotate (); break; } } }
xml布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="150dp" android:orientation="horizontal" android:gravity="center" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_shops_down_arrow_pink" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <Button android:id="@+id/rotate_center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绕中心旋转" /> <Button android:id="@+id/rotateX" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="属性动画" /> <Button android:id="@+id/rotateY" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绕Y轴旋转" /> </LinearLayout> </LinearLayout>
逆时针旋转动画:
<set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:fromDegrees="0" android:toDegrees="-180" android:duration="1200" android:fillAfter="true" android:pivotX="50%" android:pivotY="50%" /> </set>
顺时针旋转动画:
<set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:fromDegrees="-180" android:toDegrees="0" android:duration="1200" android:fillAfter="true" android:pivotX="50%" android:pivotY="50%" /> </set>
相关文章推荐
- 适配器模式
- 黑马程序员--OC自学笔记---07内存管理、@property参数、@class、autorelease
- iOS开发中实现新闻图片的无限循环展示的方法
- Hibernate各种主键生成策略与配置详解
- Spring整合JMS(二)——三种消息监听器
- 修改屏幕顶部状态栏颜色
- SQL中 and or优先级问题
- Hibernate总结
- Play Framework 报错 java.sql.SQLException: Connection is closed!
- 注册界面设计及实现之(三)SharedPerferences实现数据暂存
- 集合
- CSS盒子,line-height,padding,margin,background,border
- SQL查询月份有多少天(根据变量)
- 为OLED屏增加GUI支持5:图片控件
- 目前计划---毕业前(2016-6)
- PHP汉字转换拼音的函数代码
- 在Windows下搭建Gitlab服务器(二)-不能访问Gitlab的web管理页面
- PHP 在源码中实现换行使得页面源码更精致美观
- C# 操作JSON字符串用于与网页中的返回数据进行互动
- PHP通过计算经纬度获取附近的数据信息