Android实现控件动画效果

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private ImageView iv;
    private int j = 0;
    private Button enter;
    private LinearLayout leftLayout, rightLayout;

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

//        iv = (ImageView) findViewById(R.id.iv);

        change();

        leftLayout = (LinearLayout) findViewById(R.id.layout_left);
        rightLayout = (LinearLayout) findViewById(R.id.layout_right);

        TopBar topBar = (TopBar) findViewById(R.id.topbar);
        topBar.setOnTopBarClickListener(new TopBar.TopBarClickListener() {
            @Override
            public void onLeftClick() {
                Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();

                /*LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
                View view = inflater.inflate(R.layout.item, null);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                addContentView(view,params);*/

//                twoAnimation();

//                offSet();

//                flash();

                Animation animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.all);
                leftLayout.startAnimation(animation);

                leftLayout.setVisibility(View.VISIBLE);
                rightLayout.setVisibility(View.GONE);
            }

            @Override
            public void onRightClick() {
                Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show();

                rightLayout.setVisibility(View.VISIBLE);
                leftLayout.setVisibility(View.GONE);
            }
        });
    }

    //Activity交换时的动画效果
    public void change() {
        enter = (Button) findViewById(R.id.btn_enter);
        enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "haha", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, EnterActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.activity_enter, R.anim.activity_out);
            }
        });
    }

    //闪动效果
    public void flash() {
        Animation animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.flash);
        leftLayout.startAnimation(animation);
    }

    //给某些动画设置了延迟时间
    public void offSet() {
        Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.offset);
        leftLayout.startAnimation(animation);
    }

    //一个动画接着一个动画
    public void twoAnimation() {
        Animation animation1 = new AlphaAnimation(0.1f, 1.0f);
        final Animation animation2 = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f);
        animation1.setDuration(2000);
        animation2.setDuration(2000);
        leftLayout.startAnimation(animation1);
        animation1.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                leftLayout.startAnimation(animation2);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }
    
}

EnterActivity.java

public class EnterActivity extends AppCompatActivity {
    private ImageView iv;

    private Button btn;

    private List<String> mData;
    private ListView listview;

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

        iv = (ImageView) findViewById(R.id.iv);
        btn = (Button) findViewById(R.id.btn_myworld);

        listview = (ListView) findViewById(R.id.listview);
        mData = new ArrayList<String>();

        for (int i = 0; i < 10; i++) {
            mData.add("" + i);
        }

        ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(
                EnterActivity.this, android.R.layout.simple_list_item_1, mData);
        listview.setAdapter(mAdapter);

        LayoutAnimationController lac = new LayoutAnimationController(
                AnimationUtils.loadAnimation(this, R.anim.flash));
        lac.setOrder(LayoutAnimationController.ORDER_RANDOM);
        listview.setLayoutAnimation(lac);
        listview.startLayoutAnimation();

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listview.setVisibility(View.GONE);

                iv.setVisibility(View.VISIBLE);
                iv.setImageResource(R.drawable.image_list);
                AnimationDrawable drawable = (AnimationDrawable) iv.getDrawable();
                drawable.start();
            }
        });
    }
}

TopBar.java

public class TopBar extends RelativeLayout {
    private Button leftBtn, rightBtn;
    private TextView title;

    private int leftTextColor;
    private Drawable leftBackground;
    private String leftText;

    private int rightTextColor;
    private Drawable rightBackground;
    private String rightText;

    private int titleColor;
    private float titleTextSize;
    private String titleText;

    private LayoutParams leftParams, rightParams, titleParams;

    public interface TopBarClickListener {
        void onLeftClick();
        void onRightClick();
    }

    private TopBarClickListener listener;

    public void setOnTopBarClickListener (TopBarClickListener listener) {
        this.listener = listener;
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        //1.
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.topBar);

        leftTextColor = ta.getColor(R.styleable.topBar_leftTextColor, 0);
        leftBackground = ta.getDrawable(R.styleable.topBar_leftBackground);
        leftText = ta.getString(R.styleable.topBar_leftText);

        rightTextColor = ta.getColor(R.styleable.topBar_rightTextColor, 0);
        rightBackground = ta.getDrawable(R.styleable.topBar_rightBackground);
        rightText = ta.getString(R.styleable.topBar_rightText);

        titleColor = ta.getColor(R.styleable.topBar_titleTextColors, 0);
        titleTextSize = ta.getDimension(R.styleable.topBar_titleTextSize, 0);
        titleText = ta.getString(R.styleable.topBar_titleText);

        //回收资源,避免浪费,避免缓存造成的影响
        ta.recycle();

        //2.
        leftBtn = new Button(context);
        rightBtn = new Button(context);
        title = new TextView(context);

        leftBtn.setTextColor(leftTextColor);
        leftBtn.setBackground(leftBackground);
        leftBtn.setText(leftText);
        leftBtn.setGravity(Gravity.CENTER);

        rightBtn.setTextColor(rightTextColor);
        rightBtn.setBackground(rightBackground);
        rightBtn.setText(rightText);

        title.setTextColor(titleColor);
        title.setTextSize(titleTextSize);
        title.setText(titleText);
        title.setGravity(Gravity.CENTER);

        setBackgroundColor(0xFFF12456);

        //3.
        leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
        addView(leftBtn, leftParams);

        rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
        addView(rightBtn, rightParams);

        titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
        addView(title, titleParams);

        //4.
        leftBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onLeftClick();
            }
        });

        rightBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onRightClick();
            }
        });

    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <my.com.example.x550v.uidisigndmo.TopBar
        android:id="@+id/topbar"
        android:layout_width="match_parent"
        android:layout_height="40dp"

        custom:titleText="我的世界"
        custom:titleTextSize="10sp"
        custom:titleTextColors="#fff"
        
        custom:leftBackground="@drawable/button"
        custom:leftText="上一页"
        custom:leftTextColor="#000"

        custom:rightBackground="@drawable/button"
        custom:rightText="下一页"
        custom:rightTextColor="#000"/>

    <Button
        android:id="@+id/btn_enter"
        android:text="Enter"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <!--<ImageView
        android:id="@+id/iv"
        android:background="#f7b2b2"
        android:layout_below="@id/topbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />-->
    <LinearLayout
        android:id="@+id/layout_left"
        android:orientation="vertical"
        android:layout_below="@id/topbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone">
        <Button
            android:text="白日依山尽"
            android:textSize="20sp"
            android:layout_gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <ImageView
            android:layout_gravity="center_horizontal"
            android:background="@drawable/ahri1"
            android:layout_width="200dp"
            android:layout_height="380dp" />
        <Button
            android:text="黄河入海流"
            android:textSize="20sp"
            android:layout_gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_right"
        android:orientation="vertical"
        android:layout_below="@id/topbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone">
        <Button
            android:text="欲穷千里目"
            android:textSize="20sp"
            android:layout_gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <ImageView
            android:layout_gravity="center_horizontal"
            android:background="@drawable/ahri2"
            android:layout_width="200dp"
            android:layout_height="380dp" />
        <Button
            android:text="更上一层楼"
            android:textSize="20sp"
            android:layout_gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</RelativeLayout>

activity_enter.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="my.com.example.x550v.uidisigndmo.EnterActivity">
    <Button
        android:id="@+id/btn_myworld"
        android:text="我的世界观"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <ImageView
        android:id="@+id/iv"
        android:layout_gravity="center_horizontal"
        android:visibility="gone"
        android:layout_width="250dp"
        android:layout_height="500dp" />

</LinearLayout>

attrs.xml

<resources>
    <declare-styleable name="topBar">
        <attr name="titleText" format="string"/>
        <attr name="titleTextSize" format="dimension" />
        <attr name="titleTextColors" format="color"/>

        <attr name="leftBackground" format="reference|color"/>
        <attr name="leftText" format="string"/>
        <attr name="leftTextColor" format="color"/>

        <attr name="rightBackground" format="reference|color"/>
        <attr name="rightText" format="string"/>
        <attr name="rightTextColor" format="color"/>

     </declare-styleable>
</resources>

image_list.xml

<animation-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/ahri1"
        android:duration="2000"/>
    <item
        android:drawable="@drawable/ashe"
        android:duration="2000"/>
    <item
        android:drawable="@drawable/ahri2"
        android:duration="2000"/>
    <item
        android:drawable="@drawable/brand"
        android:duration="2000"/>
</animation-list>

all.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillBefore="false"
    android:fillAfter="true">
    <!--<alpha-->
        <!--android:duration="2000"-->
        <!--android:fromAlpha="0.1"-->
        <!--android:toAlpha="1.0"/>-->

    <!--<scale-->
        <!--android:duration="2000"-->
        <!--android:fillAfter="false"-->
        <!--android:fromXScale="0.1"-->
        <!--android:fromYScale="0.1"-->
        <!--android:toXScale="1.0"-->
        <!--android:toYScale="1.0"-->
        <!--android:pivotX="80%"-->
        <!--android:pivotY="80%"-->
        <!--android:interpolator="@android:anim/accelerate_interpolator"/>-->

    <translate
        android:duration="2000"
        android:fromXDelta="-400"
        android:fromYDelta="-400"
        android:toXDelta="400"
        android:toYDelta="400" />


        <!--<rotate-->
        <!--android:duration="2000"-->
        <!--android:fromDegrees="0"-->
        <!--android:toDegrees="360"-->
        <!--android:pivotX="30%"-->
        <!--android:pivotY="30%"/>-->
</set>
原文地址:https://www.cnblogs.com/tianhengblogs/p/5263343.html