We want to make a blinking
spotlight for decorating our app.
In xml file, create a
LinearLayout with five imageViews.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
>
<ImageView
android:id="@+id/img1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:contentDescription="@null"
/>
<ImageView
android:id="@+id/img2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:contentDescription="@null"
/>
<ImageView
android:id="@+id/img3"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:contentDescription="@null"
/>
<ImageView
android:id="@+id/img4"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:contentDescription="@null"
/>
<ImageView
android:id="@+id/img5"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:contentDescription="@null"
/>
</LinearLayout>
Images will have same
dimension, when we change images follow time, it look like they are blinking.
Declare a global
variable CountDownTimer w.
Import library.
Copy this lines to below
findViewByid.
w=new
CountDownTimer(3000000, 1000) {
public void onTick(long mil) {
if(mil%3==0){
im.setBackgroundResource(R.drawable.l1);
im2.setBackgroundResource(R.drawable.l2);
im3.setBackgroundResource(R.drawable.l3);
im4.setBackgroundResource(R.drawable.l1);
im5.setBackgroundResource(R.drawable.l2);
}
else if(mil%3==1){
im.setBackgroundResource(R.drawable.l2);
im2.setBackgroundResource(R.drawable.l3);
im3.setBackgroundResource(R.drawable.l1);
im4.setBackgroundResource(R.drawable.l2);
im5.setBackgroundResource(R.drawable.l3);
}
else {
im.setBackgroundResource(R.drawable.l3);
im2.setBackgroundResource(R.drawable.l1);
im3.setBackgroundResource(R.drawable.l2);
im4.setBackgroundResource(R.drawable.l3);
im5.setBackgroundResource(R.drawable.l1);
}
}
public void onFinish()
{
}
}.start();
We set count
down time to very big, CountDownTimer(3000000, 1000)
Number 1000 mean
one second.
We change images
follow time, there are three phases change after 1, 2, 3 second. We divide time
to 3 and get remainder, use them to set if command.
We do nothing in
public
void onFinish(), just let them blinking for long time.
If it is a game,
we pop up an inform Game over.
When user press
Back button, we destroy all, copy this method to above last bracket.
public void onBackPressed()
{
w.cancel();
finish();
}
No comments:
Post a Comment