Back to project page Randm.
The source code is released under:
Apache License
If you think the Android project Randm listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.malvin.randm; /*from w ww . j a va 2s . c o m*/ import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.animation.AccelerateDecelerateInterpolator; import android.view.animation.DecelerateInterpolator; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import butterknife.ButterKnife; import butterknife.InjectView; import com.melnykov.fab.FloatingActionButton; import java.util.Locale; import java.util.Random; import timber.log.Timber; public class MainActivity extends ActionBarActivity { @InjectView(R.id.default_toolbar) Toolbar toolbar; @InjectView(R.id.generated_text) TextView generatedText; @InjectView(R.id.history_list) ListView historyList; @InjectView(R.id.fab) FloatingActionButton button; ArrayAdapter<String> arrayAdapter; private MenuItem mMenuClearAll; private boolean isUnique; private boolean useSeparator; private Random rand = new Random(System.currentTimeMillis()); private int mAnimDuration = 900; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.inject(this); initViews(); } @Override protected void onResume() { super.onResume(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); isUnique = prefs.getBoolean(getString(R.string.pref_is_unique), false); useSeparator = prefs.getBoolean(getString(R.string.pref_use_separator), false); } private void initViews() { setSupportActionBar(toolbar); View header = getLayoutInflater().inflate(R.layout.header_padding, historyList, false); historyList.addHeaderView(header); arrayAdapter = new ArrayAdapter<>(this, R.layout.simple_item); historyList.setAdapter(arrayAdapter); historyList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Timber.d("clicked " + String.valueOf(position)); } }); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String randVal = getRandomString(); if (isUnique) { while (arrayAdapter.getPosition(randVal) != -1) { randVal = getRandomString(); } } animateTo(randVal); } }); button.attachToListView(historyList); } private void animateTo(final String value) { int[] vals = new int[5]; for (int i = 0; i < 5; i++) { vals[i] = getRandomNumber(0, 10000); } ValueAnimator a = ValueAnimator.ofInt(vals); a.setDuration(mAnimDuration); //TODO add in settings a.setInterpolator(new DecelerateInterpolator()); a.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { Integer value = (Integer) animation.getAnimatedValue(); generatedText.setText(numToString(value)); } }); a.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); generatedText.setText(value); arrayAdapter.add(value); updateClearButtonVisibility(true); } }); ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(button, View.ROTATION, 0f, -1440f); objectAnimator.setDuration(mAnimDuration); objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); AnimatorSet animatorSet = new AnimatorSet(); animatorSet.playTogether(a, objectAnimator); animatorSet.start(); } private String getRandomString() { int generated = getRandomNumber(0, 10000); return numToString(generated); } private String numToString(int num) { String val; if (useSeparator) { val = String.format(Locale.getDefault(), "%,d", num); } else { val = String.valueOf(num); } return val; } private int getRandomNumber(int lowest, int highest) { // nextInt is normally exclusive of the top value, // so add 1 to make it inclusive return rand.nextInt((highest - lowest) + 1) + lowest; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); mMenuClearAll = menu.findItem(R.id.action_clear); return true; } private void updateClearButtonVisibility(boolean visible) { mMenuClearAll.setVisible(visible); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement switch (id) { case R.id.action_settings: Intent i = new Intent(this, SettingsActivity.class); startActivity(i); return true; case R.id.action_clear: arrayAdapter.clear(); updateClearButtonVisibility(false); return true; } return super.onOptionsItemSelected(item); } }