繁体   English   中英

工具栏大小的动画启动画面

[英]Animation splash screen of toolbar resizing

EDIT2

现在一切正常,但浮动操作按钮的onclick侦听器不起作用。


编辑

好的,所以我提供了一个半解决方案,我已经从activity_main.xml中删除了AppBarLayout,现在它不再闪烁了。 唯一的事情是操作栏是完全白色的,并且不会与工具栏的颜色合并。 我该如何解决?


我已经跟随站点以实现此动画(尽管没有回收器视图):

图片

唯一的事情是我的闪烁频繁,我不知道问题可能出在哪里。 工具栏也将变得小于默认大小。 谢谢

MainActivity.java

public class MainActivity extends AppCompatActivity {
private final String PREFS_NAME = "MyPrefsFile";
private boolean doubleBackToExitPressedOnce = false;
private CoordinatorLayout main_content;
private FloatingActionButton fab;
private int mContentViewHeight;
private Toolbar mToolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.setTitle(getString(R.string.title_main_activity));
    main_content = (CoordinatorLayout) findViewById(R.id.main_content);

    fab = (FloatingActionButton) findViewById(R.id.fab);/*
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(MainActivity.this, BasicActivity.class));
        }
    });*/
    // Fake a long startup time
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            onFakeCreate();
        }

    }, 100);
}

    private void onFakeCreate() {
        setContentView(R.layout.activity_onboarding_placeholder);

        TextView titleTextView = (TextView) findViewById(R.id.text_title);
        ViewCompat.animate(titleTextView).alpha(1).start();

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.getViewTreeObserver().addOnPreDrawListener(
                new ViewTreeObserver.OnPreDrawListener() {
                    @Override
                    public boolean onPreDraw() {
                        mToolbar.getViewTreeObserver().removeOnPreDrawListener(this);
                        final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                        final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

                        mToolbar.measure(widthSpec, heightSpec);
                        mContentViewHeight = mToolbar.getHeight();
                        collapseToolbar();
                        return true;
                    }
                });
    }

    private void collapseToolbar() {
        int toolBarHeight;
        TypedValue tv = new TypedValue();
        getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
        toolBarHeight = TypedValue.complexToDimensionPixelSize(
                tv.data, getResources().getDisplayMetrics());

        ValueAnimator valueHeightAnimator = ValueAnimator
                .ofInt(mContentViewHeight, toolBarHeight);

        valueHeightAnimator.addUpdateListener(
                new ValueAnimator.AnimatorUpdateListener() {

                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        ViewGroup.LayoutParams lp = mToolbar.getLayoutParams();
                        lp.height = (Integer) animation.getAnimatedValue();
                        mToolbar.setLayoutParams(lp);
                    }
                });

        valueHeightAnimator.start();
        valueHeightAnimator.addListener(
                new AnimatorListenerAdapter() {

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);

                        // Animate fab
                        ViewCompat.animate(fab).setStartDelay(600)
                                .setDuration(400).scaleY(1).scaleX(1).start();

                    }
                });
    }
[...]

Splash_background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="opaque"
    >
    <item>
        <shape>
            <solid android:color="@color/background"/>
        </shape>
    </item>

    <item
        android:height="180dp"
        android:gravity="top">
        <shape android:shape="rectangle">
            <solid android:color="?colorPrimary"/>
        </shape>
    </item>
</layer-list>

activity_main.xml中(更新)

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    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"
    android:fitsSystemWindows="true"
    tools:context="">

        <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="156dp"
    android:background="?colorPrimary"
    android:elevation="4dp"
    app:theme="@style/ThemeOverlay.AppCompat.Light"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_add_white_24dp"
        app:fabSize="normal"/>

</android.support.design.widget.CoordinatorLayout>

Android mainifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".SplashActivity"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".BasicActivity"
            android:label="@string/title_activity_basic"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="" />
        </activity>
        <activity
            android:name=".ExtraActivity"
            android:label="@string/title_activity_extra"
            android:parentActivityName=".BasicActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="" />
        </activity>
    </application>

</manifest>

请检查project.toolbar动画中的样式,在我这边工作正常。

    <resources>
    <style name="Button" parent="Theme.AppCompat">
        <item name="colorControlHighlight">@color/indigo_500</item>
        <item name="colorButtonNormal">@color/pink_200</item>
    </style>
</resources>

还检查构建gradle

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId ""
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:cardview-v7:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
}

希望这可以帮助。:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM