歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android開發之使用RatingBar設置游戲難度並保存

最近在學習游戲開發,在游戲中常常需要設置游戲的難度,在Android中我們可以使用RatingBar來實現。具體看下面的代碼:

效果圖如下:

  1. package com.cloay.pt.ui;  
  2.   
  3. import java.io.FileNotFoundException;  
  4. import java.io.IOException;  
  5.   
  6. import android.content.ContentResolver;  
  7. import android.content.Intent;  
  8. import android.content.SharedPreferences;  
  9. import android.graphics.Bitmap;  
  10. import android.graphics.BitmapFactory;  
  11. import android.net.Uri;  
  12. import android.os.Bundle;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.ImageView;  
  16. import android.widget.RatingBar;  
  17. import android.widget.RatingBar.OnRatingBarChangeListener;  
  18. import android.widget.TextView;  
  19.   
  20. import com.cloay.pt.BasicActivity;  
  21. import com.cloay.pt.R;  
  22. import com.cloay.pt.constants.Constant;  
  23. import com.cloay.pt.utils.ImageUtil;  
  24.   
  25. /** 
  26.  * 游戲設置類,游戲難度等 
  27.  * @author Cloay 
  28.  * 2011-12-23 
  29.  * 下午05:21:41 
  30.  */  
  31. public class SettingsActivity extends BasicActivity{  
  32.     private SharedPreferences settings;  //保存設置   
  33.     private RatingBar ratingBar;  
  34.     private TextView defaultPicture;  
  35.     private TextView picture;  
  36.     private ImageView previewImage;  
  37.     @Override  
  38.     protected void onCreate(Bundle savedInstanceState) {  
  39.         super.onCreate(savedInstanceState);  
  40.         setContentView(R.layout.settings);  
  41.         BasicActivity.allActivity.add(this);  
  42.         //初始化數據   
  43.         initData();  
  44.     }  
  45.       
  46.     /** 
  47.      * 初始化數據 
  48.      */  
  49.     private void initData() {  
  50.         settings = getSharedPreferences("settings"0);  
  51.         ratingBar = (RatingBar) findViewById(R.id.rating);  
  52.         //游戲默認圖片   
  53.         defaultPicture = (TextView) findViewById(R.id.def);  
  54.         //從圖庫中選擇圖片,自定義   
  55.         picture = (TextView) findViewById(R.id.picture);  
  56.         previewImage = (ImageView)findViewById(R.id.preview);  
  57.         if("".equals(settings.getString("picture"""))) {  
  58.             previewImage.setImageResource(R.drawable.def);  
  59.         }else{  
  60.             previewImage.setImageBitmap(ImageUtil.getBitmapFromSDCard(settings.getString("picture""")));  
  61.         }  
  62.         //設置難度選擇監聽   
  63.         ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {  
  64.             @Override  
  65.             public void onRatingChanged(RatingBar ratingBar, float rating,  
  66.                     boolean fromUser) {  
  67.                 settings.edit().putInt("level", (int)rating).commit();   //保存用戶設置後的游戲難度   
  68.                 showToast(SettingsActivity.this, (int)rating);  
  69.             }  
  70.         });  
  71.         //設置圖片選擇監聽   
  72.         defaultPicture.setOnClickListener(new OnClickListener() {  
  73.             @Override  
  74.             public void onClick(View v) {  
  75.                 settings.edit().putString("picture""").commit();  
  76.                 previewImage.setImageResource(R.drawable.def);  
  77.             }  
  78.         });  
  79.         picture.setOnClickListener(new OnClickListener() {  
  80.             @Override  
  81.             public void onClick(View v) {  
  82.                 Intent intent = new Intent();    
  83.                 /* 開啟Pictures畫面Type設定為image */    
  84.                 intent.setType("image/*");    
  85.                 /* 使用Intent.ACTION_GET_CONTENT這個Action */    
  86.                 intent.setAction(Intent.ACTION_GET_CONTENT);   
  87.                 /* 取得相片後返回本畫面 */    
  88.                 startActivityForResult(intent, 1);  
  89.             }  
  90.         });  
  91.           
  92.     }  
  93.       
  94.     @Override  
  95.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  96.         if (resultCode == RESULT_OK) {    
  97.             Uri uri = data.getData();    
  98.             ContentResolver cr = this.getContentResolver();    
  99.             try {    
  100.                 Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));    
  101.                 try {  
  102.                     saveBitmap("cloay", bitmap);  
  103.                 } catch (IOException e) {  
  104.                     e.printStackTrace();  
  105.                 }  
  106.                 settings.edit().putString("picture", Constant.picPath + "cloay.png").commit();  
  107.                 previewImage.setImageBitmap(bitmap);  
  108.             } catch (FileNotFoundException e) {    
  109.                 settings.edit().putString("picture""").commit();  
  110.                 previewImage.setImageResource(R.drawable.def);  
  111.             }    
  112.         }   
  113.         super.onActivityResult(requestCode, resultCode, data);  
  114.     }  
  115. }  
通過設置setOnRatingBarChangeListener監聽器來獲取用戶選擇RatingBar的值,獲取之後往往需要保存,通常使用SharedPrefence即可,也可選用SQLite等。SharedPrefence的使用詳細請看: Android SharedPreferences的使用 http://www.linuxidc.com/Linux/2011-11/48029.htm
Copyright © Linux教程網 All Rights Reserved