HiCards

Contenu du snippet

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create a RelativeLayout to hold the game content
        RelativeLayout layout = new RelativeLayout(this);
        layout.setBackgroundColor(Color.BLUE);

        // Create a TextView to display the "Hicards" text
        TextView hicardsText = new TextView(this);
        hicardsText.setText("Hicards");
        hicardsText.setTextColor(Color.WHITE);
        hicardsText.setTextSize(24f);

        // Set the layout parameters for the TextView
        RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        textParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        hicardsText.setLayoutParams(textParams);

        // Add the TextView to the layout
        layout.addView(hicardsText);

        // Set the layout as the content view for the activity
        setContentView(layout);
    }
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    private RelativeLayout layout;
    private TextView hicardsText;
    private RelativeLayout cardsLayout;
    private Button playButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create the initial layout with blue background and "Hicards" text
        layout = new RelativeLayout(this);
        layout.setBackgroundColor(Color.BLUE);

        hicardsText = new TextView(this);
        hicardsText.setText("Hicards");
        hicardsText.setTextColor(Color.WHITE);
        hicardsText.setTextSize(24f);

        RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        textParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        hicardsText.setLayoutParams(textParams);

        layout.addView(hicardsText);
        setContentView(layout);

        // Animate the "Hicards" text to disappear
        animateHicardsText();

        // Create the new layout with cards background and "Play" button
        createCardsLayout();
    }

    private void animateHicardsText() {
        AnimationSet animationSet = new AnimationSet(true);

        // Fade out animation
        AlphaAnimation fadeOut = new AlphaAnimation(1.0f, 0.0f);
        fadeOut.setDuration(1000);

        // Translate animation (move up)
        TranslateAnimation translateUp = new TranslateAnimation(
                0, 0, 0, -200);
        translateUp.setDuration(1000);

        animationSet.addAnimation(fadeOut);
        animationSet.addAnimation(translateUp);

        hicardsText.startAnimation(animationSet);

        // After the animation, remove the initial layout and show the new layout
        animationSet.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                layout.removeView(hicardsText);
                setContentView(cardsLayout);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {}
        });
    }

    private void createCardsLayout() {
        cardsLayout = new RelativeLayout(this);
        cardsLayout.setBackgroundResource(R.drawable.cards_background);

        hicardsText = new TextView(this);
        hicardsText.setText("Hicards");
        hicardsText.setTextColor(Color.GREEN);
        hicardsText.setTextSize(24f);

        RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        textParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        textParams.topMargin = 20;
        hicardsText.setLayoutParams(textParams);

        playButton = new Button(this);
        
        playButton.setText("Play");
        playButton.setBackgroundColor(Color.GREEN);
        playButton.setTextColor(Color.WHITE);

        RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        buttonParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        playButton.setLayoutParams(buttonParams);

        cardsLayout.addView(hicardsText);
        cardsLayout.addView(playButton);
    }
}