1. 程式人生 > >Android 仿QQ底部導航圖片點選

Android 仿QQ底部導航圖片點選

1.倆張圖

這樣

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="200"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatMode="reverse"
        android:toDegrees="180" />
</set>

 


然後這樣

public class MainActivity extends AppCompatActivity {

    private ImageView image;
    private ImageView image2;
    private int lastindex;  //上一條
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        final Animation animationRotate = AnimationUtils.loadAnimation(this, R.anim.rotate);
        animationRotate.setInterpolator(new LinearInterpolator());//設定為線性旋轉

        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                image.startAnimation(animationRotate);
                animationRotate.setFillAfter(!animationRotate.getFillAfter());
            }
        });

        final AnimatorSet animatorSetsuofang = getAnimatorSet(image2);

        image2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                animatorSetsuofang.start();
                animatorSetsuofang.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        image2.setImageResource(R.drawable.appmanager);


                        //一個思路 可以 寫個變數  int x  和當前一樣  就return
                        // 在點選新的圖片是 讓之前的 還原 重新setImage

                    }
                });
            }
        });
    }
    
    private AnimatorSet getAnimatorSet(ImageView imageView) {
        final AnimatorSet animatorSetsuofang = new AnimatorSet();//組合動畫
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 1f,0, 1f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 1f,0, 1f);
        animatorSetsuofang.setDuration(1000);
        animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
        animatorSetsuofang.play(scaleX).with(scaleY);//兩個動畫同時開始
        return animatorSetsuofang;
    }

    private void initView() {
        image = (ImageView) findViewById(R.id.image);
        image2 = (ImageView) findViewById(R.id.image2);
    }
}