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 and difficulty
* @ Author Cloay
* 2011-12-23
* 05:21:41 pm
*/
Public class SettingsActivity extends BasicActivity {
Private SharedPreferences settings; // Save the settings
Private RatingBar ratingBar;
Private TextView defaultPicture;
Private TextView picture;
Private ImageView previewImage;
@ Override
Protected 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 it
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 to set the difficulty
RatingBar. setOnRatingBarChangeListener (new OnRatingBarChangeListener (){
@ Override
Public void onRatingChanged (RatingBar ratingBar, float rating,
Boolean fromUser ){
Settings. edit (). putInt ("level", (int) rating). commit (); // save the game difficulty set by the user
ShowToast (SettingsActivity. this, (int) rating );
}
});
// Set the Image Selection listener
DefaultPicture. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Settings. edit (). putString ("picture", ""). commit ();
PreviewImage. setImageResource (R. drawable. def );
}
});
Picture. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Intent intent = new Intent ();
/* Enable Pictures image Type to set to image */
Intent. setType ("image /*");
/* Use the Intent. ACTION_GET_CONTENT Action */
Intent. setAction (Intent. ACTION_GET_CONTENT );
/* Return to the current screen after obtaining the photo */
StartActivityForResult (intent, 1 );
}
});

}

@ Override
Protected 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 );
}
}
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 and difficulty
* @ Author Cloay
* 2011-12-23
* 05:21:41 pm
*/
Public class SettingsActivity extends BasicActivity {
Private SharedPreferences settings; // Save the settings
Private RatingBar ratingBar;
Private TextView defaultPicture;
Private TextView picture;
Private ImageView previewImage;
@ Override
Protected 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 it
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 to set the difficulty
RatingBar. setOnRatingBarChangeListener (new OnRatingBarChangeListener (){
@ Override
Public void onRatingChanged (RatingBar ratingBar, float rating,
Boolean fromUser ){
Settings. edit (). putInt ("level", (int) rating). commit (); // save the game difficulty set by the user
ShowToast (SettingsActivity. this, (int) rating );
}
});
// Set the Image Selection listener
DefaultPicture. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Settings. edit (). putString ("picture", ""). commit ();
PreviewImage. setImageResource (R. drawable. def );
}
});
Picture. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Intent intent = new Intent ();
/* Enable Pictures image Type to set to image */
Intent. setType ("image /*");
/* Use the Intent. ACTION_GET_CONTENT Action */
Intent. setAction (Intent. ACTION_GET_CONTENT );
/* Return to the current screen after obtaining the photo */
StartActivityForResult (intent, 1 );
}
});

}
 
@ Override
Protected 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. SharedPrefence use detailed please see: Android SharedPreferences use http://www.bkjia.com/kf/201203/125000.html
 

From Cloay's column
 

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.