English 中文(简体)
脱硫-im化只在第二次后奏效
原标题:TranslateAnimation - animation works only after the second time

I used some code I found for a sliding panel, and basically it works but has a small issue.

小组首次开场白。

here:

    TranslateAnimation anim = null;


    m_isOpen = !m_isOpen;

    if (m_isOpen) {
        setVisibility(View.VISIBLE);
        anim = new TranslateAnimation(0.0f, 0.0f, getHeight(), 0.0f);
    } else {
        anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, getHeight());
        anim.setAnimationListener(new Animation.AnimationListener() {
              public void onAnimationEnd(Animation animation) {
                   setVisibility(View.GONE);
              }

              public void onAnimationRepeat(Animation animation) {
                   // not needed
              }

              public void onAnimationStart(Animation animation) {
                   // not needed
              }
        });

    }

    anim.setDuration(300);
    anim.setInterpolator(new AccelerateInterpolator(1.0f));
    startAnimation(anim);

为什么在我宣布小组开会时,没有人要问,但还有其他什么?

最佳回答

When are you calling this? It is probably only doing it "after the first time" because the panel has not been rendered yet when you first call it, and getHeight() is returning 0. Try waiting until getHeight() has a value and start the animation then. You can also try to hardcode the value to something just to test that my theory is right.

问题回答

我如何使该守则发挥作用:


final ViewGroup viewGroup = (ViewGroup) findViewById(R.id.panel);
viewGroup.setVisibility(View.VISIBLE);
final ViewTreeObserver treeObserver = viewGroup.getViewTreeObserver();

if (treeObserver.isAlive()) {
    final OnPreDrawListener l = new OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            treeObserver.removeOnPreDrawListener(this);
            TranslateAnimation anim = new TranslateAnimation(0.0f, 0.0f, viewGroup.getHeight(), 0.0f);
            anim.setDuration(1000);
            anim.setInterpolator(new AccelerateInterpolator(1.0f));
            viewGroup.startAnimation(anim);

            return false;
            }
        };
    treeObserver.addOnPreDrawListener(l);
}

That is it in its essence. Hope it helps.

如果只有一种观点认为你正在试图行凶,那么你就可以在XML中写一句话。

<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromYDelta="-100%"
        (or)    
android:toYDelta="-100%"  
android:duration="300"/>

This way, you don t need to use getHeight(). The Java portion then looks like this:

    final View flashTitle = findViewById(R.id.flash_title);
    Animation slideDownOnscreen = AnimationUtils.loadAnimation
            (this,  R.anim.slide_down_onscreen);
    flashTitle.setAnimation(slideDownOnscreen);
    flashTitle.setVisibility(View.VISIBLE);

或者,如果您需要同时提出若干意见,则与第一种意见一致,并将随后在<代码>可操作上发表的意见与第一种观点联系起来,从而使第一种意见的正确高度如:

    flashTitle.post(new Runnable() {
        @Override
        public void run() {
            System.out.println("NOTE    flashTitle height: "+flashTitle.getHeight());
            Animation slideDown = new TranslateAnimation(0, 0,
                    -flashTitle.getHeight(),0);
            slideDown.setDuration(300);
            flashLine.setVisibility(View.VISIBLE);
            flashLine.startAnimation(slideDown);
            //(or)flashLine.animate().translationYBy(flashTitle.getHeight()).setDuration(300);
        }
    });

You would also need to add the accelerate_interpolator as Android s default is accelerate_decelerate_interpolator.

you can Use Delay for work it

 TranslateAnimation animate1 = new TranslateAnimation(
                    0,                 // fromXDelta
                    0,                 // toXDelta
                    0,  // fromYDelta
                    binding.saveCardview.getMeasuredHeight());                // toYDelta
                animate1.setDuration(500);
                animate1.setFillAfter(true);
                binding.saveCardview.startAnimation(animate1);

                final Handler handler = new Handler(Looper.getMainLooper());
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //Do something after 100ms
                        binding.saveCardview.setVisibility(View.GONE);
                        TranslateAnimation animate2 = new TranslateAnimation(
                                0,                 // fromXDelta
                                0,                 // toXDelta
                                binding.carCardview.getHeight(),  // fromYDelta
                                0);                // toYDelta
                        animate2.setDuration(1000);
                        animate2.setFillAfter(true);
                        binding.carCardview.startAnimation(animate2);
                    }
                }, 500);




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签