Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.30 KB | None | 0 0
  1. //activity_main
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.     xmlns:app="http://schemas.android.com/apk/res-auto"
  5.     xmlns:tools="http://schemas.android.com/tools"
  6.     android:layout_width="match_parent"
  7.     android:layout_height="match_parent"
  8.     tools:context=".MainActivity">
  9.  
  10.     <Button
  11.         android:id="@+id/b1"
  12.         android:layout_width="wrap_content"
  13.         android:layout_height="wrap_content"
  14.         android:layout_centerHorizontal="true"
  15.         android:layout_marginTop="150dp"
  16.         android:text="b1" />
  17.  
  18.     <Button
  19.         android:id="@+id/b2"
  20.         android:layout_width="wrap_content"
  21.         android:layout_height="wrap_content"
  22.         android:layout_centerVertical="true"
  23.         android:layout_marginLeft="50dp"
  24.         android:layout_marginTop="200dp"
  25.         android:text="b2" />
  26.  
  27.     <Button
  28.         android:id="@+id/b3"
  29.         android:layout_width="wrap_content"
  30.         android:layout_height="wrap_content"
  31.         android:layout_centerHorizontal="true"
  32.         android:layout_marginTop="300dp"
  33.         android:text="b3" />
  34.  
  35.     <Button
  36.         android:id="@+id/b4"
  37.         android:layout_width="wrap_content"
  38.         android:layout_height="wrap_content"
  39.         android:layout_alignParentRight="true"
  40.         android:layout_centerVertical="true"
  41.         android:layout_marginRight="50dp"
  42.         android:text="b4" />
  43.  
  44.     <Button
  45.         android:id="@+id/startButton"
  46.         android:layout_width="wrap_content"
  47.         android:layout_height="wrap_content"
  48.         android:layout_alignParentBottom="true"
  49.         android:layout_centerHorizontal="true"
  50.         android:text="start" />
  51. </RelativeLayout>
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59. //MainActivity
  60.  
  61. package com.sbm.myapplication;
  62.  
  63. import android.content.Context;
  64. import android.os.Bundle;
  65. import android.os.Handler;
  66. import android.support.v7.app.AppCompatActivity;
  67. import android.view.View;
  68. import android.view.animation.AlphaAnimation;
  69. import android.view.animation.Animation;
  70. import android.widget.Button;
  71.  
  72. import java.util.Random;
  73.  
  74. public class MainActivity extends AppCompatActivity {
  75.  
  76.     Random rnd = new Random();
  77.     Button b1, b2, b3, b4, startButton; //ארבעה כפתורים מהבהבים + כפתור הפעלה
  78.     Button [] buttons;
  79.  
  80.     @Override
  81.     protected void onCreate(Bundle savedInstanceState) {
  82.         super.onCreate(savedInstanceState);
  83.         setContentView(R.layout.activity_main);
  84.  
  85.         b1 = findViewById(R.id.b1);
  86.         b2 = findViewById(R.id.b2);
  87.         b3 = findViewById(R.id.b3);
  88.         b4 = findViewById(R.id.b4);
  89.         startButton = findViewById(R.id.startButton);
  90.  
  91.         startButton.setOnClickListener(new View.OnClickListener() {
  92.             @Override
  93.             public void onClick(View view) {
  94.  
  95.                 buttons = new Button[]{b1,b2,b3,b4};
  96.  
  97.                 final Animation animation1 = new AlphaAnimation(1.0f, 0.0f);
  98.                 animation1.setDuration(650);
  99.  
  100.                 animation1.setAnimationListener(new Animation.AnimationListener() {
  101.                     @Override
  102.                     public void onAnimationStart(Animation animation) {
  103.  
  104.                     }
  105.  
  106.                     @Override
  107.                     public void onAnimationEnd(Animation animation) {
  108.                         //כשהאנימציה קוראת לonAnimationEnd האנימציה לא באמת מסתיימת,
  109.                         //הוספתי משהו שגורם לאפליקציה "לישון" ל200 מילי שניות אחרי זה האנימציה כבר מסתיימת ואפשר להפעיל אחת חדשה
  110.                         new Handler().postDelayed(new Runnable() {
  111.                             @Override
  112.                             public void run() {
  113.  
  114.                                 buttons[rnd.nextInt(4)].startAnimation(animation1);
  115.  
  116.                             }
  117.                         }, 200);
  118.                     }
  119.  
  120.                     @Override
  121.                     public void onAnimationRepeat(Animation animation) {
  122.  
  123.                     }
  124.                 });
  125.  
  126.                 buttons[rnd.nextInt(4)].startAnimation(animation1); //הבהוב ראשון, אחרי זה ההבהובים ימשיכו רנדומלית
  127.  
  128.             }
  129.         });
  130.  
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement