android:编写属性动画程序(旋转,缩放,淡出淡入)

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.yanhsama.ex4_13.MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="属性动画演示"
            android:id="@+id/textView"
            android:textSize="28sp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal" />

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_gravity="center_horizontal">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="旋转"
                android:id="@+id/button1"
                android:layout_gravity="bottom"
                android:textSize="18sp"
                android:layout_below="@+id/textView"
                android:layout_alignParentStart="true" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="淡入淡出"
                android:id="@+id/button2"
                android:layout_gravity="bottom"
                android:textSize="18sp"
                android:layout_below="@+id/button1"
                android:layout_alignParentLeft="true" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="缩放"
                android:id="@+id/button3"
                android:layout_gravity="bottom"
                android:textSize="18sp"
                android:layout_below="@+id/button2"
                android:layout_alignParentLeft="true" />

        </LinearLayout>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/imageView"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:scaleType="centerCrop"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/bn" />
    </LinearLayout>

</RelativeLayout>

MainActivity.java

package com.example.yanhsama.ex4_13;

import android.support.v7.app.AppCompatActivity;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    Button rotateButton,alphaButton,scaleButton;
    ImageView img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img = (ImageView)findViewById(R.id.imageView);
        rotateButton = (Button)findViewById(R.id.button1);
        alphaButton = (Button)findViewById(R.id.button2);
        scaleButton = (Button)findViewById(R.id.button3);
        rotateButton.setOnClickListener(new mClick());
        alphaButton.setOnClickListener(new mClick());
        scaleButton.setOnClickListener(new mClick());
    }

    public class mClick implements View.OnClickListener
    {
        @Override
        public void onClick(View v) {
            if(v == rotateButton) {
                ObjectAnimator animator = ObjectAnimator.ofFloat(img, "rotation", 0.0F, 360.0F);
                animator.setDuration(1000);
                animator.start();
            }
            else if(v == alphaButton){
                ObjectAnimator animator = ObjectAnimator.ofFloat(img, "alpha",1.0F, 0.0F, 1.0F);
                animator.setDuration(3000);
                animator.start();
            }
            else if(v == scaleButton){
                ObjectAnimator animator = ObjectAnimator.ofFloat(img, "ScaleY", 1.0F, 0.5F, 1.0F);
                animator.setDuration(5000);
                animator.start();
            }
        }
    }

}

运行效果图示

发布了91 篇原创文章 · 获赞 25 · 访问量 3万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览