在res文件夹下,创建一个名为“ anim”的新文件夹来存储动画资源,并将其放在该文件夹中。
shakeanimation.xml
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="100" android:fromDegrees="-15" android:pivotX="50%" android:pivotY="50%" android:repeatCount="infinite" android:repeatMode="reverse" android:toDegrees="15" />
创建一个名为Landing的空白活动
activity_landing.xml
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imgBell" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@mipmap/ic_notifications_white_48dp"/> </RelativeLayout>
以及在Landing.java上对imageview进行动画处理的方法
Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mContext=this; setContentView(R.layout.activity_landing); AnimateBell(); } public void AnimateBell() { Animation shake = AnimationUtils.loadAnimation(mContext, R.anim.shakeanimation); ImageView imgBell= (ImageView) findViewById(R.id.imgBell); imgBell.setImageResource(R.mipmap.ic_notifications_active_white_48dp); imgBell.setAnimation(shake); }