Android studio:View动画 旋转动画效果

View动画 旋转动画效果

先进行按钮的设置

 <Button
        android:id="@+id/bt_2"
        android:layout_marginTop="300dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />
再进行activity的配置点击事件
public class MainActivity extends AppCompatActivity {



    private RotateAnimation ra;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xuanzhuan);
        ra = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        ra.setDuration(1000);
        findViewById(R.id.bt_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.startAnimation(ra);
            }
        });
    }
}

这个是能使按钮随着自身中心点进行360度的旋转的效果
然后我们进行配置xml文件进行旋转效果

在这里插入图片描述

Resource type选择Animation , Root element 选择rotate就可以了。(还有要起名字)
写入
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"//从0度
    android:toDegrees="360"//旋转到360度
    android:duration="1000"//一秒钟的时间
    android:pivotX="50%"//两个50%是从中心旋转
    android:pivotY="50%">

</rotate>
然后我们再去写activity文件
public class MainActivity extends AppCompatActivity {



    private RotateAnimation ra;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xuanzhuan);

//        ra = new RotateAnimation(0,360,100,50);
        /*ra = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        ra.setDuration(1000);*/
        findViewById(R.id.bt_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.ra));
            }
        });
    }
}

效果一样旋转360.
原创文章 9 获赞 8 访问量 657

猜你喜欢

转载自blog.csdn.net/qq_43625843/article/details/104909543