Android - 滚动活动不顺畅

时间:2015-12-31 05:05:53

标签: android

我是android新手。滚动我的活动并不顺利。当我滚动我的活动时,logcat中会显示以下警告。

  

警告

     

I / Choreographer:跳过99帧!应用程序可能在其主线程上做了太多工作。

     

I / Choreographer:跳过87帧!应用程序可能在其主线程上做了太多工作。

     

I / Choreographer:跳过94帧!应用程序可能在其主线程上做了太多工作。

我的主要活动课

public class MainActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener{
private static final float PERCENTAGE_TO_SHOW_TITLE_AT_TOOLBAR  = 0.9f;
private static final float PERCENTAGE_TO_HIDE_TITLE_DETAILS     = 0.3f;
private static final int ALPHA_ANIMATIONS_DURATION              = 200;

private boolean mIsTheTitleVisible          = false;
private boolean mIsTheTitleContainerVisible = true;

private LinearLayout mTitleContainer;
private TextView mTitle;
private AppBarLayout mAppBarLayout;
private ImageView mImageparallax;
private FrameLayout mFrameParallax;
private Toolbar mToolbar;

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


    bindActivity();

    mToolbar.setTitle("");
    mAppBarLayout.addOnOffsetChangedListener(this);

    setSupportActionBar(mToolbar);
    startAlphaAnimation(mTitle, 0, View.INVISIBLE);
    initParallaxValues();
}

private void bindActivity() {
    mToolbar        = (Toolbar) findViewById(R.id.main_toolbar);
    mTitle          = (TextView) findViewById(R.id.main_textview_title);
    mTitleContainer = (LinearLayout) findViewById(R.id.main_linearlayout_title);
    mAppBarLayout   = (AppBarLayout) findViewById(R.id.main_appbar);
    mImageparallax  = (ImageView) findViewById(R.id.main_imageview_placeholder);
    mFrameParallax  = (FrameLayout) findViewById(R.id.main_framelayout_title);
}

private void initParallaxValues() {
    CollapsingToolbarLayout.LayoutParams petDetailsLp =
            (CollapsingToolbarLayout.LayoutParams) mImageparallax.getLayoutParams();

    CollapsingToolbarLayout.LayoutParams petBackgroundLp =
            (CollapsingToolbarLayout.LayoutParams) mFrameParallax.getLayoutParams();

    petDetailsLp.setParallaxMultiplier(0.9f);
    petBackgroundLp.setParallaxMultiplier(0.3f);

    mImageparallax.setLayoutParams(petDetailsLp);
    mFrameParallax.setLayoutParams(petBackgroundLp);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int offset) {
    int maxScroll = appBarLayout.getTotalScrollRange();
    float percentage = (float) Math.abs(offset) / (float) maxScroll;

    handleAlphaOnTitle(percentage);
    handleToolbarTitleVisibility(percentage);
}

private void handleToolbarTitleVisibility(float percentage) {
    if (percentage >= PERCENTAGE_TO_SHOW_TITLE_AT_TOOLBAR) {

        if(!mIsTheTitleVisible) {
            startAlphaAnimation(mTitle, ALPHA_ANIMATIONS_DURATION, View.VISIBLE);
            mIsTheTitleVisible = true;
        }

    } else {

        if (mIsTheTitleVisible) {
            startAlphaAnimation(mTitle, ALPHA_ANIMATIONS_DURATION, View.INVISIBLE);
            mIsTheTitleVisible = false;
        }
    }
}

private void handleAlphaOnTitle(float percentage) {
    if (percentage >= PERCENTAGE_TO_HIDE_TITLE_DETAILS) {
        if(mIsTheTitleContainerVisible) {
            startAlphaAnimation(mTitleContainer, ALPHA_ANIMATIONS_DURATION, View.INVISIBLE);
            mIsTheTitleContainerVisible = false;
        }

    } else {

        if (!mIsTheTitleContainerVisible) {
            startAlphaAnimation(mTitleContainer, ALPHA_ANIMATIONS_DURATION, View.VISIBLE);
            mIsTheTitleContainerVisible = true;
        }
    }
}

public static void startAlphaAnimation (View v, long duration, int visibility) {
    AlphaAnimation alphaAnimation = (visibility == View.VISIBLE)
            ? new AlphaAnimation(0f, 1f)
            : new AlphaAnimation(1f, 0f);

    alphaAnimation.setDuration(duration);
    alphaAnimation.setFillAfter(true);
    v.startAnimation(alphaAnimation);
}
}

如何使滚动顺畅?

0 个答案:

没有答案