1) anim/blinkinh_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="400"
android:fromAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toAlpha="1.0" />
</set>
2) MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Android Blinking Animation"
android:textSize="22sp"
android:textStyle="bold"
android:textAlignment="center"/>
<Button
android:id="@+id/blinking_animation"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginTop="16dp"
android:text="Blinking Animation"
android:layout_gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="@+id/start"
android:text="Start" />
</LinearLayout>
3) MainActivity.java
public class MainActivity extends AppCompatActivity {
Button start, button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start);
button = (Button) findViewById(R.id.blinking_animation);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(start.getText().toString().matches("Start")){
startBlinkingAnimation();
start.setText("Stop");
} else {
button.setAnimation(null);
start.setText("Start");
}
}
});
}
public void startBlinkingAnimation() {
Animation startAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.blinking_animation);
button.startAnimation(startAnimation);
}
}
No comments:
Post a Comment