如何在动画通知查看幻灯片是推动内容视图下 [英] How to animate a slide in notification view that pushes the content view down

查看:80
本文介绍了如何在动画通知查看幻灯片是推动内容视图下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在屏幕上两种观点

1坐在屏幕的顶部和直接坐在它下面

我需要绿色的视图滑出前 - ,使蓝色的观点占据整个屏幕,结果

这就是我要做的:

- 问题是,当动画完成后,蓝色的看法只是跳起来 - 我希望它缓和与消失的绿色观点,我如何做到这一点

slide_in_animation.xml

 < XML版本=1.0编码=UTF-8&GT?;

<设置的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    <翻译
            机器人:时间=1000
            机器人:fromYDelta = -  100%
            机器人:toYDelta =0%/>
< /集>
 

slide_out_animation.xml

 < XML版本=1.0编码=UTF-8&GT?;

<设置的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    <翻译
            机器人:时间=1000
            机器人:fromYDelta =0%
            机器人:toYDelta = -  100%/>
< /集>
 

MainActivity.java

  slideInAnimation = AnimationUtils.loadAnimation(mActivity,R.anim.slide_in_animation);
slideOutAnimation = AnimationUtils.loadAnimation(mActivity,R.anim.slide_out_animation);


    slideInAnimation.setAnimationListener(新AnimationListener(){

        @覆盖
        公共无效onAnimationStart(动画动画){
            mGreenView.setVisibility(View.VISIBLE);

        }

        @覆盖
        公共无效onAnimationRepeat(动画动画){
            // TODO自动生成方法存根

        }

        @覆盖
        公共无效onAnimationEnd(动画动画){

        }
    });

    slideOutAnimation.setAnimationListener(新AnimationListener(){
        @覆盖
        公共无效onAnimationStart(动画动画){


        }

        @覆盖
        公共无效onAnimationEnd(动画动画){
            mGreenView.setVisibility(View.GONE);

        }

        @覆盖
        公共无效onAnimationRepeat(动画动画){
            //要改变的实现方法正文中使用文件|设置
            // |文件模板。
        }
    });
 

解决方案

这为我工作

首先,你必须导入该库: http://nineoldandroids.com/

进口是通过导入现有的Andr​​oid项目到工作区完成,之后右键单击您Poject - >属性 - > Android系统。在这里,你会看到一个库部分,单击Add按钮,添加nineoldandroids库。

首先,这里是用于该工作布局的xml:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:ID =@ + ID / parentLayout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
工具:上下文=MainActivity。>

<的FrameLayout
    机器人:ID =@ + ID /的FrameLayout
    机器人:layout_width =match_parent
    机器人:layout_height =0dp
    机器人:layout_weight =1>

    <的ListView
        机器人:ID =@ + ID / ListView1的
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:背景=@机器人:彩色/透明
        机器人:分隔=@机器人:彩色/透明
        机器人:fastScrollEnabled =假
        机器人:listSelector =@机器人:彩色/透明
        机器人:滚动条=垂直
        机器人:smoothScrollbar =真/>

    <查看
        机器人:ID =@ + ID /绿景
        机器人:layout_width =match_parent
        机器人:layout_height =150dp
        机器人:后台=#FF00FF00
        机器人:阿尔法=0/>
< /的FrameLayout>

<的LinearLayout
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=横向>

    <按钮
        机器人:ID =@ + ID /动画
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:的onClick =clickHandler事件
        机器人:文本=动画/>

    <按钮
        机器人:ID =@ + ID /关
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:的onClick =clickHandler事件
        机器人:文本=关闭/>
< / LinearLayout中>
 

请注意:无论是ListView和绿色景观可以是任何类型的布局与任何类型的内容

和下一个概念活动的证据。

 公共类TestActivity延伸活动{

私人查看绿景;
私人的ListView ListView的;
私人诠释greenHeight;

私人布尔isShowingBox;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_test);

    //动画视图
    绿景=(查看)findViewById(R.id.greenView);
    ListView的=(的ListView)findViewById(R.id.listView1);


    // Instanciating一个数组列表(你不需要做这个,你已经拥有你的)
    ArrayList的<字符串> your_array_list =新的ArrayList<字符串>();
    your_array_list.add(1);
    your_array_list.add(2);
    your_array_list.add(3);
    your_array_list.add(4);
    your_array_list.add(5);
    your_array_list.add(6);
    your_array_list.add(7);
    your_array_list.add(8);
    your_array_list.add(9);
    your_array_list.add(10);
    your_array_list.add(11);
    your_array_list.add(12);
    your_array_list.add(13);
    your_array_list.add(14);
    your_array_list.add(15);
    //这是阵列适配器,它需要活动的背景下作为第一//参数列表视图作为第二个参数的类型和数组作为第三个参数
    ArrayAdapter<字符串> arrayAdapter =
    新的ArrayAdapter<字符串>(这一点,android.R.layout.simple_list_item_1,your_array_list);
    listView.setAdapter(arrayAdapter);

    最后的LinearLayout布局=(的LinearLayout)findViewById(R.id.parentLayout);

       最终ViewTreeObserver VTO = layout.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(新OnGlobalLayoutListener(){
            @覆盖
            公共无效onGlobalLayout(){

                如果(Build.VERSION.SDK_INT< 16){
                    。layout.getViewTreeObserver()removeGlobalOnLayoutListener(本);
                } 其他 {
                    。layout.getViewTreeObserver()removeOnGlobalLayoutListener(本);
                }

                greenHeight = greenView.getHeight();

            }
        });
}

公共无效clickHandler事件(视图v){
    如果(isShowingBox){
        isShowingBox = FALSE;
        滑出(1500,0);
    } 其他 {
        isShowingBox = TRUE;
        的slideIn(1500,0);
    }
}

私人无效的slideIn(INT持续时间,诠释延迟){
    AnimatorSet组=新AnimatorSet();
    set.playTogether(
        //从离屏中屏动画
        ObjectAnimator.ofFloat(绿景translationY,-greenHeight,0),
        ObjectAnimator.ofFloat(ListView中,translationY,0,greenHeight)
        ObjectAnimator.ofFloat(绿景,阿尔法,0,​​0.25f,1)
        //添加其他动画,如果你想
    );
    set.setStartDelay(延迟);
    set.setDuration(持续时间)。开始();
}

私人无效滑出(INT持续时间,诠释延迟){
    AnimatorSet组=新AnimatorSet();
    set.playTogether(
        //从屏幕和出动画
        ObjectAnimator.ofFloat(绿景,translationY,0,-greenHeight)
        ObjectAnimator.ofFloat(ListView中,translationY,greenHeight,0),
        ObjectAnimator.ofFloat(绿景,阿尔法,1,1,1)
        //添加其他动画,如果你想
    );
    set.setStartDelay(延迟);
    set.setDuration(持续时间)。开始();
}

}
 

重要提示:记住导入AnimatorSet和ObjectAnimator从nineoldandroids在你的类,而不是Android SDK中的人!

I have two views on the screen

one sits at the top of the screen and on sits directly below it

I need the green view to slide out the top - and make the blue view take up the entire screen as a result

this is what i am trying to do:

-- the problem is , when the animation is finished, the blue view just "jumps" up - and i want it to ease up with the disappearing green view, how do i do that?

slide_in_animation.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
            android:duration="1000"
            android:fromYDelta="-100%"
            android:toYDelta="0%" />
</set>

slide_out_animation.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
            android:duration="1000"
            android:fromYDelta="0%"
            android:toYDelta="-100%" />
</set>

MainActivity.java

slideInAnimation = AnimationUtils.loadAnimation(mActivity, R.anim.slide_in_animation);
slideOutAnimation = AnimationUtils.loadAnimation(mActivity,R.anim.slide_out_animation);


    slideInAnimation.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            mGreenView.setVisibility(View.VISIBLE);

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {

        }
    });

    slideOutAnimation.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {


        }

        @Override
        public void onAnimationEnd(Animation animation) {
            mGreenView.setVisibility(View.GONE);

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // To change body of implemented methods use File | Settings
            // | File Templates.
        }
    });

解决方案

This worked for me

First you must import this library: http://nineoldandroids.com/

The import is done by importing existing android project into your workspace, afterwards right click your Poject -> Properties -> Android. Here you will see a library section, click the Add button and add the nineoldandroids library.

First off, here is the layout xml used for this to work:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<FrameLayout
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:divider="@android:color/transparent"
        android:fastScrollEnabled="false"
        android:listSelector="@android:color/transparent"
        android:scrollbars="vertical"
        android:smoothScrollbar="true" />

    <View
        android:id="@+id/greenView"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="#ff00ff00"
        android:alpha="0"/>
</FrameLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/animate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clickHandler"
        android:text="animate" />

    <Button
        android:id="@+id/close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clickHandler"
        android:text="close" />
</LinearLayout>

Notice: Both the ListView and the green View could be layouts of any types with any kind of content.

And next a proof of concept Activity.

public class TestActivity extends Activity {

private View greenView;
private ListView listView;
private int greenHeight;

private boolean isShowingBox;

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

    // The animated view 
    greenView = (View)findViewById(R.id.greenView);
    listView = (ListView)findViewById(R.id.listView1);


    // Instanciating an array list (you don't need to do this, you already have yours)
    ArrayList<String> your_array_list = new ArrayList<String>();
    your_array_list.add("1");
    your_array_list.add("2");
    your_array_list.add("3");
    your_array_list.add("4");
    your_array_list.add("5");
    your_array_list.add("6");
    your_array_list.add("7");
    your_array_list.add("8");
    your_array_list.add("9");
    your_array_list.add("10");
    your_array_list.add("11");
    your_array_list.add("12");
    your_array_list.add("13");
    your_array_list.add("14");
    your_array_list.add("15");
    // This is the array adapter, it takes the context of the activity as a first // parameter, the type of list view as a second parameter and your array as a third parameter
    ArrayAdapter<String> arrayAdapter =      
    new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, your_array_list);
    listView.setAdapter(arrayAdapter);

    final LinearLayout layout = (LinearLayout)findViewById(R.id.parentLayout);

       final ViewTreeObserver vto = layout.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                if (Build.VERSION.SDK_INT < 16) {
                    layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                } else {
                    layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }

                greenHeight = greenView.getHeight();

            }
        });
}

public void clickHandler(View v) {
    if (isShowingBox) {
        isShowingBox = false;
        slideOut(1500, 0);
    } else {
        isShowingBox = true;
        slideIn(1500, 0);
    }
}

private void slideIn(int duration, int delay) { 
    AnimatorSet set = new AnimatorSet();
    set.playTogether(
        // animate from off-screen in to screen 
        ObjectAnimator.ofFloat(greenView, "translationY",  -greenHeight, 0),
        ObjectAnimator.ofFloat(listView, "translationY",  0, greenHeight),
        ObjectAnimator.ofFloat(greenView, "alpha", 0, 0.25f, 1)
        // add other animations if you wish
    );
    set.setStartDelay(delay);
    set.setDuration(duration).start();
}

private void slideOut(int duration, int delay) {
    AnimatorSet set = new AnimatorSet();
    set.playTogether(
        // animate from on-screen and out
        ObjectAnimator.ofFloat(greenView, "translationY", 0, -greenHeight),
        ObjectAnimator.ofFloat(listView, "translationY", greenHeight, 0),
        ObjectAnimator.ofFloat(greenView, "alpha", 1, 1, 1)
        // add other animations if you wish
    );
    set.setStartDelay(delay);
    set.setDuration(duration).start();
}

}

Important note: Remember to import the AnimatorSet and ObjectAnimator from nineoldandroids in your class and not the Android SDK ones!!!

这篇关于如何在动画通知查看幻灯片是推动内容视图下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆