Android按钮不会激活他们应该

时间:2016-06-18 09:23:15

标签: java android animation button

我正在尝试在Android中实现类似this的屏幕。我从网站上获取了相同的代码。尝试过,但它没有动画按钮。我通过运行以下代码获得this

我的 content_main.xml 包含以下代码: -

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="meetutu.juspay.com.meetutu.MainActivity"
    tools:showIn="@layout/activity_main"
    android:background="#2980B9">


    <ImageView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/meetutuImageView"
        android:src="@drawable/meetutuimage"
        android:layout_gravity="center"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <RelativeLayout
        android:id="@+id/container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/meetutuImageView"
        android:layout_centerHorizontal="true">

        <TextView
            android:id="@+id/textView1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="The joy of meeting an expert teacher"
            android:textAlignment="center"
            android:textColor="#A3E4D7"
            android:layout_centerHorizontal="true"/>
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="for a craving learner"
            android:textAlignment="center"
            android:textColor="#A3E4D7"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/textView1"/>

        <Button
            style="@style/Widget.AppCompat.Button.Colored"
            android:id="@+id/loginButton"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="LOGIN"
            android:layout_marginTop="44dp"
            android:layout_below="@+id/textView2"
            android:layout_alignLeft="@+id/textView1"
            android:layout_alignStart="@+id/textView1" />

        <Button
            style="@style/Widget.AppCompat.Button.Colored"
            android:id="@+id/signupButton"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="SIGN UP"
            android:layout_alignTop="@+id/loginButton"
            android:layout_alignRight="@+id/textView1"
            android:layout_alignEnd="@+id/textView1" />



    </RelativeLayout>


</RelativeLayout>

我的 MainActivity.java 包含以下代码:

package meetutu.juspay.com.meetutu;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.*;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private ImageView logoImage;
    private TextView textView1, textView2;
    private ViewGroup container;

    public static final int STARTUP_DELAY = 300;
    public static final int ANIM_ITEM_DURATION = 1000;
    public static final int ITEM_DELAY = 300;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);


    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {

        if(hasFocus)
            animation();
        super.onWindowFocusChanged(hasFocus);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void animation()
    {
        logoImage = (ImageView) findViewById(R.id.meetutuImageView);
        container = (ViewGroup) findViewById(R.id.container);

        ViewCompat.animate(logoImage)
                .translationY(-200)
                .setStartDelay(STARTUP_DELAY)
                .setDuration(ANIM_ITEM_DURATION)
                .setInterpolator(
                        new DecelerateInterpolator(1.2f)).start();

        for(int i = 0; i < container.getChildCount(); i++)
        {
            View v = container.getChildAt(i);
            ViewPropertyAnimatorCompat viewAnimator;

            if(!(v instanceof Button))
            {
                viewAnimator = ViewCompat.animate(v)
                        .translationY(50).alpha(1)
                        .setStartDelay((ITEM_DELAY * i) + 500)
                        .setDuration(1000);
            }
            else
            {
                viewAnimator = ViewCompat.animate(v)
                        .scaleY(1).scaleX(1)
                        .setStartDelay((ITEM_DELAY * i) + 500)
                        .setDuration(500);
            }

            viewAnimator.setInterpolator(new DecelerateInterpolator()).start();
        }
    }
}

style.xml 包含以下代码:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

我是Android的新手,所以我对动画类型及其实现方式了解不多。我想实现链接中给出的动画和平滑度。请让我知道我应该在代码中添加哪些内容以使其正常工作。

感谢您的时间!!

1 个答案:

答案 0 :(得分:0)

以下是示例: -

Image

使用这些xml并将这4个放入 drawable / anim 文件夹

zoom_in.xml

<?xml version="1.0" encoding="utf-8"?>
<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="0"
    android:fromYScale="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1"
    android:toYScale="1"
    android:startOffset="500" >
</scale>

move_up.xml

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

    <translate
        android:fromYDelta="10%p"
        android:toYDelta="0%p"
        android:duration="800"
        android:startOffset="0" />
</set>

move_down.xml

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

    <translate
        android:fromYDelta="-15%p"
        android:toYDelta="0%p"
        android:duration="500"
        android:startOffset="300"/>
</set>

move_down_slow.xml

<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

    <translate
        android:fromYDelta="-10%p"
        android:toYDelta="0%p"
        android:duration="300"
        android:startOffset="500" />
</set>

将此代码用于 Oncreate()方法..

中的动画

将其声明为全球 ......

private Animation animation,animation1,animation2,animation3,animation4;

这是代码 ......

    animation3 = new AlphaAnimation(0.0f, 1.0f);
    animation3.setFillAfter(true);
    animation3.setDuration(1200);
    mLinear.startAnimation(animation3);
    mText.setText("Meetutu");
    mText1.setText("The joy of meeting an expert teacher");
    mText2.setText("for a craving learner");
         animation = AnimationUtils.loadAnimation(Full_XML_Practise.this, R.anim.move_to_up);
    mText.startAnimation(animation);
    animation1 = AnimationUtils.loadAnimation(Full_XML_Practise.this, R.anim.move_to_down);
    mText1.startAnimation(animation1);
    animation2 = AnimationUtils.loadAnimation(Full_XML_Practise.this, R.anim.move_to_down_slow);
    mText2.startAnimation(animation2);
    animation4 = AnimationUtils.loadAnimation(this, R.anim.zoom_in);
    mButton.startAnimation(animation4);

注意: - mText,mText1,mText2为TextView,mButton为Button,mLinear为layout.xml文件中的Parent root .....

享受编码......