如何制作一个显示onClick环的按钮动画

时间:2016-01-19 23:09:53

标签: android animation button

我希望能够通过在转到另一个活动而不是摇晃之前显示响铃来显示所有按钮,这就是它现在所做的。请帮忙,谢谢!

main_activity布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id="@+id/drawer_layout"

    android:layout_height="match_parent">
    <FrameLayout
        android:id = "@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/scrollView2"
            android:background="@color/black"
            >




            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="910dp"
                android:orientation = "vertical"
                android:background="@mipmap/home"
                android:id = "@+id/content_fram">
                <RelativeLayout
                    android:layout_width="150sp"
                    android:layout_height="150sp"
                    android:orientation = "vertical"
                    android:background="@drawable/rounded"
                    android:id="@+id/relativeLayout2"
                    android:layout_marginLeft="@dimen/activity_horizontal_margin"
                    android:layout_marginTop="@dimen/activity_horizontal_margin"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentStart="true">
                    <Button
                        style="?android:attr/buttonStyleSmall"
                        android:layout_width="150sp"
                        android:layout_height="150sp"
                        android:id="@+id/time"
                        android:textSize="32sp"
                        android:textColor = "@color/black"
                        android:background="@mipmap/timebackground"
                        android:theme="@android:style/Animation.InputMethod"
                        android:orientation = "vertical"
                        android:layout_alignParentTop="true"
                        android:layout_alignParentStart="true" />
                </RelativeLayout>
                <RelativeLayout
                    android:layout_width="150sp"
                    android:layout_height="150sp"
                    android:background="@drawable/rounded"
                    android:id="@+id/relativeLayout3"
                    android:layout_gravity="left"
                    android:layout_above="@+id/textView2"
                    android:layout_alignParentEnd="true"
                    android:layout_marginRight="@dimen/activity_horizontal_margin"
                    android:layout_marginTop="@dimen/activity_horizontal_margin">

                    <Button
                        android:layout_width="150sp"
                        android:layout_height="150sp"
                        android:id="@+id/temperature"
                        android:theme="@android:style/Animation"
                        android:background="@mipmap/temperature"
                        android:layout_gravity="center"
                        android:orientation = "vertical"
                        android:layout_alignParentTop="true"
                        android:layout_alignParentStart="true" />

                </RelativeLayout>
                <RelativeLayout
                    android:layout_width="150sp"
                    android:layout_height="150sp"
                    android:background="@drawable/rounded"
                    android:layout_below="@+id/textView2"
                    android:layout_alignStart="@+id/textView2"
                    android:layout_marginTop="@dimen/activity_vertical_margin"
                    android:id="@+id/relativeLayout">

                    <Button
                        android:layout_width="200sp"
                        android:layout_height="180sp"
                        android:id="@+id/Weight"
                        android:theme="@android:style/Animation"
                        android:background="@mipmap/weight"
                        android:layout_alignParentStart="true" />
                </RelativeLayout>


                <TextView
                    android:layout_width="200sp"
                    android:layout_height="30sp"
                    android:text="Time"
                    android:textAllCaps="true"
                    android:id="@+id/textView2"
                    android:textSize="21sp"
                    android:gravity="center"
                    android:textColor = "@color/black"
                    android:background="@drawable/text"
                    android:layout_below="@+id/relativeLayout2"
                    android:layout_alignStart="@+id/relativeLayout2"
                    android:layout_alignEnd="@+id/relativeLayout2"
                    android:layout_marginTop="@dimen/activity_horizontal_margin" />

                <RelativeLayout
                    android:layout_width="150dp"
                    android:layout_height="150dp"
                    android:background="@drawable/rounded"
                    android:id="@+id/relativeLayout4"
                    android:layout_above="@+id/textView4"
                    android:layout_alignStart="@+id/textView3"
                    android:layout_marginTop="@dimen/activity_horizontal_margin">

                    <Button
                        android:layout_width="150dp"
                        android:layout_height="150dp"
                        android:id="@+id/button"
                        android:background="@drawable/length"/>

                </RelativeLayout>

                <TextView
                    android:layout_width="200sp"
                    android:layout_height="30sp"
                    android:text="Weight"
                    android:textAllCaps="true"
                    android:id="@+id/textView4"
                    android:textSize="21sp"
                    android:gravity="center"
                    android:textColor="@color/black"
                    android:background="@drawable/text"
                    android:layout_below="@+id/relativeLayout"
                    android:layout_alignStart="@+id/relativeLayout"
                    android:layout_alignEnd="@+id/relativeLayout"
                    android:layout_marginTop="@dimen/activity_horizontal_margin" />

                <RelativeLayout
                    android:layout_width="150dp"
                    android:layout_height="150dp"
                    android:background="@drawable/rounded"
                    android:layout_below="@+id/textView5"
                    android:layout_alignStart="@+id/textView5"
                    android:layout_marginTop="@dimen/activity_horizontal_margin"
                    android:id="@+id/relativeLayout5" >

                    <Button
                        android:layout_width="150dp"
                        android:layout_height="150dp"
                        android:background="@mipmap/money"
                        android:id="@+id/button3"
                        android:layout_centerVertical="true"
                        android:layout_alignParentStart="true" />
                </RelativeLayout>

                <TextView
                    android:layout_width="200sp"
                    android:layout_height="30sp"
                    android:text="Length"
                    android:textAllCaps="true"
                    android:id="@+id/textView5"
                    android:textSize="21sp"
                    android:gravity="center"
                    android:textColor="@color/black"
                    android:background="@drawable/text"
                    android:layout_below="@+id/relativeLayout4"
                    android:layout_alignStart="@+id/relativeLayout4"
                    android:layout_alignEnd="@+id/relativeLayout4"
                    android:layout_marginTop="@dimen/activity_horizontal_margin" />

                <RelativeLayout
                    android:layout_width="150dp"
                    android:layout_height="150dp"
                    android:background="@drawable/rounded"
                    android:layout_alignTop="@+id/relativeLayout5"
                    android:layout_alignStart="@+id/textView4"
                    android:id="@+id/relativeLayout6" >

                    <Button
                        android:layout_width="150dp"
                        android:layout_height="150dp"
                        android:background="@mipmap/metric"
                        android:id="@+id/button2"
                        android:layout_alignParentTop="true"
                        android:layout_alignParentStart="true" />

                </RelativeLayout>

                <TextView
                    android:layout_width="200sp"
                    android:layout_height="30sp"
                    android:text="Metric"
                    android:textAllCaps="true"
                    android:id="@+id/textView6"
                    android:textSize="21sp"
                    android:gravity="center"
                    android:textColor="@color/black"
                    android:background="@drawable/text"
                    android:layout_below="@+id/relativeLayout6"
                    android:layout_alignStart="@+id/relativeLayout6"
                    android:layout_alignEnd="@+id/relativeLayout6"
                    android:layout_marginTop="@dimen/activity_horizontal_margin" />

                <TextView
                    android:layout_width="200sp"
                    android:layout_height="30sp"
                    android:text="Money"
                    android:textAllCaps="true"
                    android:id="@+id/textView7"
                    android:textSize="21sp"
                    android:gravity="center"
                    android:textColor="@color/black"
                    android:background="@drawable/text"
                    android:layout_below="@+id/relativeLayout5"
                    android:layout_alignStart="@+id/relativeLayout5"
                    android:layout_alignEnd="@+id/relativeLayout5"
                    android:layout_marginTop="@dimen/activity_horizontal_margin" />


                <TextView
                    android:layout_width="202sp"
                    android:layout_height="30sp"
                    android:text="Temperature"
                    android:gravity="center"
                    android:textAllCaps="true"
                    android:id="@+id/textView3"
                    android:textSize="21sp"
                    android:textColor = "@color/black"
                    android:background="@drawable/text"
                    android:layout_above="@+id/relativeLayout"
                    android:layout_alignEnd="@+id/relativeLayout3"
                    android:layout_alignStart="@+id/relativeLayout3" />

                <RelativeLayout
                    android:layout_width="150dp"
                    android:layout_height="150dp"
                    android:background="@drawable/rounded"
                    android:id="@+id/relativeLayout7"
                    android:layout_below="@+id/textView6"
                    android:layout_alignStart="@+id/textView6"
                    android:layout_marginTop="16dp">

                    <Button
                        android:layout_width="150dp"
                        android:layout_height="150dp"
                        android:id="@+id/button4"
                        android:background="@mipmap/food"
                        android:layout_alignParentTop="true"
                        android:layout_alignParentStart="true" />
                </RelativeLayout>


                <RelativeLayout
                    android:layout_width="150dp"
                    android:layout_height="150dp"
                    android:background="@drawable/rounded"
                    android:id="@+id/relativeLayout8"
                    android:layout_alignTop="@+id/relativeLayout7"
                    android:layout_alignStart="@+id/textView7">

                    <Button
                        android:layout_width="150dp"
                        android:layout_height="150dp"
                        android:id="@+id/button6"
                        android:background="@mipmap/data"
                        android:layout_alignParentTop="true"
                        android:layout_alignParentStart="true" />
                </RelativeLayout>

                <TextView
                    android:layout_width="200sp"
                    android:layout_height="30sp"
                    android:text="Food"
                    android:textAllCaps="true"
                    android:id="@+id/textView10"
                    android:textSize="21sp"
                    android:gravity="center"
                    android:textColor="@color/black"
                    android:background="@drawable/text"
                    android:layout_alignEnd="@+id/relativeLayout7"
                    android:layout_below="@+id/relativeLayout7"
                    android:layout_alignStart="@+id/relativeLayout7"
                    android:layout_marginTop="16dp" />

                <TextView
                    android:layout_width="200sp"
                    android:layout_height="30sp"
                    android:text="Data"
                    android:textAllCaps="true"
                    android:id="@+id/textView11"
                    android:textSize="21sp"
                    android:gravity="center"
                    android:textColor="@color/black"
                    android:background="@drawable/text"
                    android:layout_alignStart="@+id/relativeLayout8"
                    android:layout_alignTop="@+id/textView10"
                    android:layout_alignEnd="@+id/relativeLayout8" />


            </RelativeLayout>


        </ScrollView>
        <fragment
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:name="course.labs.converjz.adView"
            android:id="@+id/fragment"
            tools:layout="@layout/adview"
            android:layout_gravity="center|bottom"
            android:layout_alignWithParentIfMissing="false"
            android:layout_below="@+id/textView10"
            android:layout_alignParentStart="true"
            android:layout_marginTop="16dp" />
    </FrameLayout>
    <ListView android:id="@+id/slider_list"
        android:layout_width="220dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@color/blue"
        android:dividerHeight="2dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

MainActivity.class:

 final Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
        final Button Metric = (Button) findViewById(R.id.button2);
        final RelativeLayout r = (RelativeLayout) findViewById(R.id.relativeLayout6);
        Metric.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {


                r.startAnimation(shake);
                Intent i = new Intent(MainActivity.this, Metric.class);
                startActivity(i);
                finish();

            }
        });
        final Button Time = (Button) findViewById(R.id.time);
        final RelativeLayout r2 = (RelativeLayout) findViewById(R.id.relativeLayout2);
        Time.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                r2.startAnimation(shake);
                Intent i = new Intent(MainActivity.this, Time.class);
                startActivity(i);
                finish();


            }
        });
        final Button Temperature = (Button) findViewById(R.id.temperature);
        final RelativeLayout r3 = (RelativeLayout) findViewById(R.id.relativeLayout3);
        Temperature.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                r3.startAnimation(shake);
                Intent i = new Intent(MainActivity.this, Temperature.class);
                startActivity(i);
                finish();


            }
        });
        final Button Weight = (Button) findViewById(R.id.Weight);
        final RelativeLayout r4 = (RelativeLayout) findViewById(R.id.relativeLayout);
        Weight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                r4.startAnimation(shake);
                Intent i = new Intent(MainActivity.this, Weight.class);
                startActivity(i);
                finish();


            }
        });
        final Button Length = (Button) findViewById(R.id.button);
        final RelativeLayout r5 = (RelativeLayout) findViewById(R.id.relativeLayout4);
        Length.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                r5.startAnimation(shake);
                Intent i = new Intent(MainActivity.this, Length.class);
                startActivity(i);
                finish();

            }
        });
        final Button Money = (Button) findViewById(R.id.button3);
        final RelativeLayout r6 = (RelativeLayout) findViewById(R.id.relativeLayout5);
        Money.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                r6.startAnimation(shake);
                Intent i = new Intent(MainActivity.this, Money.class);
                startActivity(i);
                finish();
            }
        });
        final Button Food = (Button) findViewById(R.id.button4);
        final RelativeLayout r7 = (RelativeLayout) findViewById(R.id.relativeLayout7);
        Food.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                r7.startAnimation(shake);
                Intent i = new Intent(MainActivity.this, Food.class);
                startActivity(i);
                finish();
            }
        });
        final Button Data = (Button) findViewById(R.id.button6);
        final RelativeLayout r8 = (RelativeLayout) findViewById(R.id.relativeLayout8);
        Data.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                r8.startAnimation(shake);
                Intent i = new Intent(MainActivity.this, Data.class);
                startActivity(i);
                finish();
            }
        });

1 个答案:

答案 0 :(得分:1)

您可以使用Animation.AnimationListener:

shake.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
    Intent i = new Intent(MainActivity.this, Metric.class);
    startActivity(i);
    finish();
    }

    @Override
    public void onAnimationEnd(Animation animation) {

    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});

希望我能正确理解你的问题,这会有所帮助。
由于你的动画,你可以只使用脉冲:

ObjectAnimator pulsingAnimation = ObjectAnimator.ofPropertyValuesHolder(mYourCustomButton,
    PropertyValuesHolder.ofFloat("scaleX", 1.2f),
    PropertyValuesHolder.ofFloat("scaleY", 1.2f));

pulsingAnimation.setRepeatCount(ObjectAnimator.INFINITE);
pulsingAnimation.setRepeatMode(ObjectAnimator.REVERSE);

pulsingAnimation.start();