Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.94 KB | None | 0 0
  1. package com.example.noxqs.helloandroid;
  2.  
  3. import android.graphics.Color;
  4. import android.support.v7.app.ActionBarActivity;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.view.animation.Animation;
  11. import android.view.animation.AnimationUtils;
  12. import android.widget.Button;
  13. import android.widget.RelativeLayout;
  14.  
  15.  
  16. public class MainActivity extends ActionBarActivity {
  17.  
  18.     private Button helloWorldButton;
  19.     private int i = 1;
  20.     private RelativeLayout rLayout;
  21.     private int heightButton;
  22.     private int widthButton;
  23.  
  24.     @Override
  25.     protected void onCreate(Bundle savedInstanceState) {
  26.         super.onCreate(savedInstanceState);
  27.         setContentView(R.layout.activity_main);
  28.  
  29.         helloWorldButton = (Button) findViewById(R.id.greeting_btn);
  30.         rLayout = (RelativeLayout) findViewById(R.id.rLayout);
  31.  
  32.         helloWorldButton.setOnClickListener(new View.OnClickListener() {
  33.             @Override
  34.             public void onClick(View v) {
  35.  
  36.                 heightButton = helloWorldButton.getHeight();
  37.                 widthButton = helloWorldButton.getWidth();
  38.  
  39.                 helloWorldButton.setText(String.valueOf(i));
  40.  
  41.                 if (i % 2 == 0) helloWorldButton.setTextColor(Color.GREEN);
  42.                 else helloWorldButton.setTextColor(Color.BLUE);
  43.                 i++;
  44.             }
  45.         });
  46.     }
  47.  
  48.  
  49.  
  50.     @Override
  51.     public boolean onCreateOptionsMenu(Menu menu) {
  52.         // Inflate the menu; this adds items to the action bar if it is present.
  53.         getMenuInflater().inflate(R.menu.menu_main, menu);
  54.         return true;
  55.     }
  56.  
  57.     @Override
  58.     public boolean onOptionsItemSelected(MenuItem item) {
  59.         // Handle action bar item clicks here. The action bar will
  60.         // automatically handle clicks on the Home/Up button, so long
  61.         // as you specify a parent activity in AndroidManifest.xml.
  62.         int id = item.getItemId();
  63.  
  64.         switch (id) {
  65.             case R.id.action_settings:
  66.                 return true;
  67.  
  68.             case R.id.size_btn:
  69.                 resizeBtn(item);
  70.                 return true;
  71.  
  72.             case R.id.resizeAnimation_btn:
  73.                 resizeAnimation(item);
  74.                 return true;
  75.  
  76.             default:
  77.                 return super.onOptionsItemSelected(item);
  78.         }
  79.     }
  80.  
  81.     private void resizeAnimation(MenuItem item) {
  82.  
  83.         int widthRelativeLayout = rLayout.getWidth();
  84.         int heightRelativeLayout = rLayout.getHeight();
  85.  
  86.  
  87.         if (item.getTitle().equals("GrowAnimation"))
  88.         {
  89.             item.setTitle("ShrinkAnimation");
  90.  
  91.             ResizeAnimation resizeAnimation = new ResizeAnimation(helloWorldButton, widthRelativeLayout, heightRelativeLayout);
  92.             resizeAnimation.setDuration(2000);
  93.             helloWorldButton.startAnimation(resizeAnimation);
  94.  
  95.  
  96.         } else {
  97.             item.setTitle("GrowAnimation");
  98.  
  99.             ResizeAnimation resizeAnimation = new ResizeAnimation(helloWorldButton, widthButton, heightButton);
  100.             resizeAnimation.setDuration(2000);
  101.             helloWorldButton.startAnimation(resizeAnimation);
  102.         }
  103.  
  104.  
  105.     }
  106.  
  107.     private void resizeBtn(MenuItem item) {
  108.  
  109.         if (item.getTitle().equals("Grow")) {
  110.             item.setTitle("Shrink");
  111.  
  112.             RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
  113.             lp.addRule(RelativeLayout.CENTER_IN_PARENT);
  114.             helloWorldButton.setLayoutParams(lp);
  115.         } else {
  116.             item.setTitle("Grow");
  117.  
  118.             RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  119.             lp.addRule(RelativeLayout.CENTER_IN_PARENT);
  120.             helloWorldButton.setLayoutParams(lp);
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement