使用淡入淡出按钮将颜色从透明颜色更改为普通颜色

时间:2018-06-27 21:29:54

标签: android button fadein

我基本上想要的是使圆形按钮在按下开关时逐渐消失,并逐渐变回在打开时的状态。我想出了一种淡出动画的方法,但是在透明之后,我似乎无法再次找回它。

我的代码

 new Thread(new Runnable() {
        @Override
        public void run() {
            aSwitch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (aSwitch.isChecked()) {
                        Animation anim = new AlphaAnimation(1.0f,0.0f);
                        anim.setDuration(800);
                        changer.startAnimation(anim);

                        anim.setAnimationListener(new Animation.AnimationListener() {
                            @Override
                            public void onAnimationStart(Animation animation) {
                                database.child("Lock Status").addValueEventListener(listener);
                                checklive = true;
                                PerformCommands();
                            }

                            @Override
                            public void onAnimationEnd(Animation animation) {
                                changer.setBackgroundColor(Color.TRANSPARENT);
                            }

                            @Override
                            public void onAnimationRepeat(Animation animation) {

                            }
                        });
                        System.out.println("Switch On");
                    }
                    else{
                        count = 0;
                        checklive = false;
                        System.out.println("Switch Off");
                        changer.setOnCheckedChangeListener(null);
                        changer.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                if (isChecked){
                                    Toast.makeText(getActivity(), "Please go live first to unlock and lock door", Toast.LENGTH_SHORT).show();
                                }
                                else{
                                    Toast.makeText(getActivity(), "Please go live first to unlock and lock door", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        text.setText("Waiting to go live....");
                        database.child("Lock Status").removeEventListener(listener);
                    }
                }
            });
        }
    }).start();

我的按钮XML

<ToggleButton
    android:id="@+id/button5"
    android:layout_width="315dp"
    android:layout_height="315dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="24dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/circle_button"
    android:text=""
    android:textOff=""
    android:textOn=""
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.944"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.702" />

圆形按钮XML

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

<size android:height="283dp" android:width="283dp"/>
<solid android:color="#666c68"/>

<corners android:radius="278dp"/>

如果有人可以帮助我过渡,我将非常感激。谢谢

0 个答案:

没有答案