如何使用动画更改按钮

时间:2014-04-17 06:07:09

标签: android view scrollview android-animation

我应该在图片中做。this按钮必须随动画而改变。它必须像圆形水平滚动视图。当我按下按钮“3” - “1” - 移动到位置“2”时,“2”随动画移动到位置“3”。请给我一些想法,我该怎么做呢?

1 个答案:

答案 0 :(得分:1)

试试这个..

第1步:

在xml中创建4 Button's dummy_btn,btn_1,btn_2,btn_3 ,如下所示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center" >

    <Button
        android:id="@+id/dummy_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:text="Button"
        android:visibility="gone" />

    <Button
        android:id="@+id/btn_1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:text="Button 1" />

    <Button
        android:id="@+id/btn_2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:text="Button 2" />

    <Button
        android:id="@+id/btn_3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:text="Button 3" />

</LinearLayout>

第2步:

清除全局变量中的Button,如下所示

Button btn_1,btn_2,btn_3,dummy_btn;

初始化Button's,如下所示

btn_1 = (Button) findViewById(R.id.btn_1);
btn_2 = (Button) findViewById(R.id.btn_2);
btn_3 = (Button) findViewById(R.id.btn_3);
dummy_btn = (Button) findViewById(R.id.dummy_btn);

第3步:

btn_1 ClickListener如下所示

 btn_1.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {

            // Get all currently displaying text of Button
            final String btn_1_string = btn_1.getText().toString().trim();
            final String btn_2_string = btn_2.getText().toString().trim();
            final String btn_3_string = btn_3.getText().toString().trim();

            // First Button Left to Right Animation
            TranslateAnimation anim = new TranslateAnimation(0f, 200f, 0f, 0f); 
            anim.setDuration(2000);
            btn_1.startAnimation(anim);

            // Second Button Left to Right Animation
            anim = new TranslateAnimation(0f, 200f, 0f, 0f); 
            anim.setDuration(2000);
            btn_2.startAnimation(anim);

            // Third Button Left to Right Animation
            anim = new TranslateAnimation(0f, 200f, 0f, 0f); 
            anim.setDuration(2000);
            btn_3.startAnimation(anim);

            btn_3.setVisibility(View.INVISIBLE);

            dummy_btn.setText(btn_3.getText().toString().trim());

            // Dummy Button Left to Right Animation for like marquee animation
            dummy_btn.setVisibility(View.VISIBLE);
            anim = new TranslateAnimation(-200f, 0f, 0f, 0f); 
            anim.setDuration(2000);
            dummy_btn.startAnimation(anim);

            // After 2000 Millis displaying text
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    btn_2.setText(btn_1_string);
                    btn_3.setText(btn_2_string);
                    btn_1.setText(btn_3_string);
                    btn_3.setVisibility(View.VISIBLE);
                    dummy_btn.setVisibility(View.GONE);                     
                }

            }, 2000);

        }

     });

和其余两个Button ClickListener

     btn_2.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            btn_1.performClick();
        }

     });

     btn_3.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                btn_1.performClick();
            }

    });