您的位置:首页 > 其它

实现了简单的旋转动画

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: