最近在學習游戲開發,在游戲中常常需要設置游戲的難度,在Android中我們可以使用RatingBar來實現。具體看下面的代碼:
效果圖如下:
- 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;
-
- /**
- * 游戲設置類,游戲難度等
- * @author Cloay
- * 2011-12-23
- * 下午05:21:41
- */
- public class SettingsActivity extends BasicActivity{
- private SharedPreferences 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);
- //初始化數據
- initData();
- }
-
- /**
- * 初始化數據
- */
- private void initData() {
- settings = getSharedPreferences("settings", 0);
- ratingBar = (RatingBar) findViewById(R.id.rating);
- //游戲默認圖片
- defaultPicture = (TextView) findViewById(R.id.def);
- //從圖庫中選擇圖片,自定義
- 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", "")));
- }
- //設置難度選擇監聽
- ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
- @Override
- public void onRatingChanged(RatingBar ratingBar, float rating,
- boolean fromUser) {
- settings.edit().putInt("level", (int)rating).commit(); //保存用戶設置後的游戲難度
- showToast(SettingsActivity.this, (int)rating);
- }
- });
- //設置圖片選擇監聽
- 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();
- /* 開啟Pictures畫面Type設定為image */
- intent.setType("image/*");
- /* 使用Intent.ACTION_GET_CONTENT這個Action */
- intent.setAction(Intent.ACTION_GET_CONTENT);
- /* 取得相片後返回本畫面 */
- 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);
- }
- }
通過設置setOnRatingBarChangeListener監聽器來獲取用戶選擇RatingBar的值,獲取之後往往需要保存,通常使用SharedPrefence即可,也可選用SQLite等。SharedPrefence的使用詳細請看: Android SharedPreferences的使用 http://www.linuxidc.com/Linux/2011-11/48029.htm