Android Animation(三)

之前编写的一些动画,翻转,平移都是各自独立的,下面我们编写一个混合的,并且我也同时编写了利用xml和java的代码的方案。


跟之前一样,首先在res下创建一个anim文件夹,创建一个xml文件


1.animone.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:shareInterpolator="true">

    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:startOffset="500"
        android:duration="500"/>

    <translate
        android:fromXDelta="0%"
        android:toXDelta="100%"
        android:fromYDelta="0%"
        android:toYDelta="100%"
        android:duration="2000"/>

</set>


2.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="sc.animationthird.MainActivity">

    <ImageView
        android:id="@+id/image_view"
        android:src="@mipmap/ic_launcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"/>

    <Button
        android:layout_marginTop="100dp"
        android:id="@+id/btn_start_xml"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="启动动画xml"
        android:layout_gravity="center_horizontal"/>

    <Button
        android:id="@+id/btn_start_java"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="启动动画java"
        android:layout_gravity="center_horizontal"/>

</LinearLayout>


3.MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;
    private Button btnStartXml;
    private Button btnStartJava;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.image_view);
        btnStartXml = (Button) findViewById(R.id.btn_start_xml);
        btnStartJava = (Button) findViewById(R.id.btn_start_java);

        btnStartXml.setOnClickListener(new XmlListener());
        btnStartJava.setOnClickListener(new JavaListener());
    }

    class XmlListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.animone);
            imageView.startAnimation(animation);
        }
    }

    class JavaListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            AnimationSet animationSet = new AnimationSet(true);
            ScaleAnimation scaleAnimation = new ScaleAnimation(0,0.1f,0,0.1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
            RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
            rotateAnimation.setDuration(1000);

            animationSet.addAnimation(rotateAnimation);
            animationSet.addAnimation(scaleAnimation);
            imageView.startAnimation(animationSet);
        }
    }

}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值