Вопрос

i should to do like at the picture.thisButtons have to change with animations. It must be like circle horizontal scroll view.When i press button "3" - "1" - moved to position "2", "2" moved with animation to position "3". Please give me some ideas, how can i perform it?

Это было полезно?

Решение

Try this..

STEP 1:

Create 4 Button's dummy_btn,btn_1,btn_2,btn_3 in xml like below

<?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>

STEP 2:

Declear the Button's in Global variables like below

Button btn_1,btn_2,btn_3,dummy_btn;

Initialize Button's like below

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);

STEP 3:

btn_1 ClickListener like below

 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);

        }

     });

and the remaining two 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();
            }

    });
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top