实现了简单的旋转动画


简单的实现了以上动画,代码如下:

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>






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值