Android uses ratingbar to set game difficulty and save

Source: Internet
Author: User

Recently I have been studying game development. In games, it is often necessary to set the difficulty of the game. In Android, we can use RatingBar. For details, refer to the following code:

As follows:

Package com.cloay.pt. ui; import java. io. fileNotFoundException; import java. io. IOException; import android. content. contentResolver; import android. content. intent; import android. content. sharedPreferences; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android.net. uri; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. imageView; import android. widget. ratingBar; import android. widget. ratingBar. onRatingBarChangeListener; import android. widget. textView; import com.cloay.pt. basicActivity; import com.cloay.pt. r; import com.cloay.pt. constants. constant; import com.cloay.pt. utils. imageUtil;/*** game settings, game difficulty, etc. * @ author Cloay * 2011-12-23*05:21:41 */public class SettingsActivity extends BasicActivity {private SharedPreferences settings; // save and set private RatingBar ratingBar; private TextView defaultPicture; private TextView picture; private ImageView previewImage; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. settings); BasicActivity. allActivity. add (this); // initialize data initData () ;}/ *** initialize data */private void initData () {settings = getSharedPreferences ("settings", 0 ); ratingBar = (RatingBar) findViewById (R. id. rating); // default Game Image defaultPicture = (TextView) findViewById (R. id. def); // select an image from the image library and customize picture = (TextView) findViewById (R. id. picture); previewImage = (ImageView) findViewById (R. id. preview); if ("". equals (settings. getString ("picture", "") {previewImage. setImageResource (R. drawable. def);} else {previewImage. setImageBitmap (ImageUtil. getBitmapFromSDCard (settings. getString ("picture", "");} // select the listener ratingBar to set the difficulty. setOnRatingBarChangeListener (new OnRatingBarChangeListener () {@ Overridepublic void onRatingChanged (RatingBar ratingBar, float rating, boolean fromUser) {settings. edit (). putInt ("level", (int) rating ). commit (); // save the game difficulty showToast (SettingsActivity. this, (int) rating) ;}}); // sets the image to listen to defapicpicture. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {settings. edit (). putString ("picture ",""). commit (); previewImage. setImageResource (R. drawable. def) ;}}); picture. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {Intent intent = new Intent ();/* set Pictures image Type to image */intent. setType ("image/*");/* use Intent. ACTION_GET_CONTENT this Action */intent. setAction (Intent. ACTION_GET_CONTENT);/* returns the current screen after obtaining the photo */startActivityForResult (intent, 1) ;}}@ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {if (resultCode = RESULT_ OK) {Uri uri = data. getData (); ContentResolver cr = this. getContentResolver (); try {Bitmap bitmap = BitmapFactory. decodeStream (cr. openInputStream (uri); try {saveBitmap ("cloay", bitmap);} catch (IOException e) {e. printStackTrace ();} settings. edit (). putString ("picture", Constant. picPath + "cloay.png "). commit (); previewImage. setImageBitmap (bitmap);} catch (FileNotFoundException e) {settings. edit (). putString ("picture ",""). commit (); previewImage. setImageResource (R. drawable. def) ;}} super. onActivityResult (requestCode, resultCode, data );}}

You can set the setOnRatingBarChangeListener listener to obtain the value of the user-selected RatingBar. After obtaining the value, you often need to save it. Generally, you can use SharedPrefence or SQLite. For details about how to use SharedPrefence, see the usage of Android SharedPreferences.

Note: Please indicate the source for reprinting!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.