Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 點擊下彈動畫實現

android 點擊下彈動畫實現

編輯:關於Android編程

下彈動畫的實現

下彈動畫在很多應用都有使用,比如豌豆莢中的應用介紹界面,百度手機助手的應用介紹界面等。

只要熟悉android動畫的使用接口,制作動畫並不困難。 這裡使用開源庫nineoldandroids,其實和android3.0 以上直接使用動畫接口是一樣的。

實現效果

PS, 搞了半天GIF 才能播放。原來是最大邊不能太大了。原來圖片尺寸太大,被默認轉成jpg了。現在總算能看到效果了。

\


DropDownExample.java

package com.buptfarmer.devapp;

import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorListenerAdapter;
import com.nineoldandroids.animation.ValueAnimator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

public class DropDownExample extends Activity implements OnClickListener {
    private View mHolder;
    private View mHolder2;
//    private static final int DURATION = 2000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        setContentView(R.layout.drop_down_example);
        mHolder = findViewById(R.id.holder);
        mHolder2 = findViewById(R.id.holder2);
        mHolder.setOnClickListener(this);
        mHolder2.setOnClickListener(this);
    }

    public static ValueAnimator createHeightAnimator(final View view, int start, int end) {
        ValueAnimator animator = ValueAnimator.ofInt(start, end);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int value = (Integer) valueAnimator.getAnimatedValue();

                ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                layoutParams.height = value;
                view.setLayoutParams(layoutParams);
            }
        });
//        animator.setDuration(DURATION);
        return animator;
    }

    public static void animateExpanding(final View view) {
        view.setVisibility(View.VISIBLE);

        final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(widthSpec, heightSpec);

        ValueAnimator animator = createHeightAnimator(view, 0, view.getMeasuredHeight());
        animator.start();
    }

    public static void animateCollapsing(final View view) {
        int origHeight = view.getHeight();

        ValueAnimator animator = createHeightAnimator(view, origHeight, 0);
        animator.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator animation) {
                view.setVisibility(View.GONE);
            };
        });
        animator.start();
    }

    @Override
    public void onClick(View v) {
        if (v == mHolder) {
            if (View.GONE == mHolder.findViewById(R.id.hiddenview).getVisibility()) {
                animateExpanding(mHolder.findViewById(R.id.hiddenview));
            } else {
                animateCollapsing(mHolder.findViewById(R.id.hiddenview));
            }
        } else if (v == mHolder2) {
            if (View.GONE == mHolder2.findViewById(R.id.hiddenview).getVisibility()) {
                animateExpanding(mHolder2.findViewById(R.id.hiddenview));
            } else {
                animateCollapsing(mHolder2.findViewById(R.id.hiddenview));
            }
        }
    }
}
drop_down_example.xml



    

        

            

            
        

        

            
        
    

    

        

            

            
        

        

            
            
        
    



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved