将动画添加到alertdialogs

时间:2016-10-14 11:13:11

标签: android android-viewpager android-animation android-alertdialog

我希望逐个显示带有动画(左,右,淡入,淡出)的警报对话框列表,例如使用查看寻呼机扫描介绍屏幕如何实现此目的..

提前致谢..

3 个答案:

答案 0 :(得分:0)

您可以通过windowAnimations属性为其设置动画:

yourDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;

设置animation transition

<style name="DialogAnimation">
        <item name="android:windowEnterAnimation">@anim/anim_fade_in</item>
        <item name="android:windowExitAnimation">@anim/anim_fade_out</item>
</style>

答案 1 :(得分:0)

以下是完整的答案。

我们必须在res-&gt; anim文件夹下拍摄四个动画文件(如果不存在则创建它)

slide_in_left

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <translate android:duration="2000" android:fromXDelta="-100%" android:toXDelta="0%"/>
    <alpha android:duration="2000" android:fromAlpha="0.0" android:toAlpha="1.0" />
</set>

slide_in右

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <translate android:duration="2000" android:fromXDelta="100%" android:toXDelta="0%" />
    <alpha android:duration="2000" android:fromAlpha="0.0" android:toAlpha="1.0" />
</set>        

slide_out_left

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <translate android:duration="2000" android:fromXDelta="0%" android:toXDelta="-100%"/>
    <alpha android:duration="2000" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>

slide_out_right

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <translate android:duration="2000" android:fromXDelta="0%" android:toXDelta="100%"/>
    <alpha android:duration="2000" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>

然后出于测试目的,我已经采取了这两个xml文件

activity_price

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_orange_light">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="80dp"
        android:layout_marginBottom="80dp"
        android:layout_marginRight="50dp"
        android:layout_marginLeft="50dp"
        android:background="@android:color/white"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:gravity="center"
            android:text="How far you are willing to\n travel for food" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <Button
                android:id="@+id/button1"
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_marginRight="20dp"
                android:layout_weight="1"
                android:layout_gravity="bottom"
                android:text="$" />

            <Button
                android:id="@+id/button2"
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_gravity="bottom"
                android:layout_weight="1"
                android:text="$$" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="10dp"
            android:layout_weight="1">

            <Button
                android:text="$$$"
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:id="@+id/button3"
                android:layout_marginRight="20dp"
                android:layout_weight="1" />

            <Button
                android:text="$$$$"
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:id="@+id/button4"
                android:layout_weight="1" />
        </LinearLayout>


    </LinearLayout>
</RelativeLayout>

activity_travel_qtn

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_orange_light">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="80dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginTop="80dp"
        android:background="@android:color/white"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="30dp"
            android:text="How far you are willing to \n\t\t\ttravel for food" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <Button
                android:id="@+id/button5"
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_gravity="bottom"
                android:layout_marginRight="20dp"
                android:layout_weight="1"
                android:text="5 miles" />

            <Button
                android:id="@+id/button6"
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_gravity="bottom"
                android:layout_weight="1"
                android:text="10 miles" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:orientation="vertical">

            <Button
                android:id="@+id/button7"
                android:layout_width="120dp"
                android:layout_height="100dp"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="more" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:orientation="horizontal">
        <Button
            android:id="@+id/button9"
            android:text="previous"
            android:textAllCaps="false"
            android:layout_width="0dp"
            android:textColor="@android:color/white"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/black"/>
        <Button
            android:background="@android:color/black"
            android:id="@+id/button10"
            android:text="next"
            android:textColor="@android:color/white"
            android:textAllCaps="false"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    </LinearLayout>

</RelativeLayout>

然后这些文件对应的活动

ActivityPrice

public class ActivityPrice extends Activity implements View.OnClickListener
{
    Button price1,price2,price3,price4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_price);

        price1= (Button) findViewById(R.id.button1);
        price2= (Button) findViewById(R.id.button2);
        price3= (Button) findViewById(R.id.button3);
        price4= (Button) findViewById(R.id.button4);


        price1.setOnClickListener(this);
        price2.setOnClickListener(this);
        price3.setOnClickListener(this);
        price4.setOnClickListener(this);



    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.button1:
                Intent i = new Intent(ActivityPrice.this, ActivityTravel.class);
                startActivity(i);
                overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
                break;
            case R.id.button2:
                Intent j = new Intent(ActivityPrice.this, ActivityTravel.class);
                startActivity(j);
                overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
                break;
            case R.id.button3:
                Intent k = new Intent(ActivityPrice.this, ActivityTravel.class);
                startActivity(k);
                overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
                break;
            case R.id.button4:
                Intent l = new Intent(ActivityPrice.this, ActivityTravel.class);
                startActivity(l);
                overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
                break;
        }
    }
}

ActivityTravel

public class ActivityTravel extends Activity implements View.OnClickListener
{
    Button travel1,travel2,travel3,previous,next;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_travel_qtn);

        travel1= (Button) findViewById(R.id.button5);
        travel2= (Button) findViewById(R.id.button6);
        travel3= (Button) findViewById(R.id.button7);
        previous= (Button) findViewById(R.id.button9);
        next= (Button) findViewById(R.id.button10);


        previous.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
     switch (v.getId())
     {
         case R.id.button9:
             Intent i = new Intent(ActivityTravel.this, ActivityPrice.class);
             i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
             startActivity(i);
             overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_righ);
             break;
     }
    }
}

答案 2 :(得分:-1)

您可以使用jQuery Ui插件实现。请参阅以下链接: https://jqueryui.com/dialog/#animated