tototan -2ページ目

BMIの計算(多言語対応)

自作iPhoneアプリ一覧
※ 環境:Mac Lion,Eclipse 3.7(日本語バージョン)
※ xmlファイルはタグが多すぎて、入力文字数の制限を超えてしまうので、画像ファイルにしました。

以下の仕様書に基づいてアプリケーションを作成します。
1.アプリケーション名は、PracticalExercises7Bとすること。 
2.体重、身長を小数点で入力できるUIをXMLファイルで実装すること。 
3.体重と身長から、BMIを計算し、小数点第一位まで画面上に表示すること。 
4. BMIより、下表に対応した体格の診断結果をユーザーに通知すること。 BMI値の範囲 体格の傾向  18.5未満 痩せている  18.5 以上 25.0 未満 標準  25.0 以上 30.0 未満 太っている  30.0 以上 肥満 
5.計算を開始する、または、表示されている数値をクリア(初期化)するイベントを実装すること。
6.不正な値が入力された場合など、異常が発生した場合には、異常をユーザに通知すること。 
7.BMIの計算は、下式を用いて算出する。身長の単位は、m(メートル)を用いて計算を行う。    BMI値 = [体重(kg)] / ([身長(m)]×[身長(m)]) 
8.診断結果別に背景と診断結果表示文字列の色を変更する。 
9. 日本語と英語の多言語対応とする。
10. Androidのバージョンは、2.1を元に作成する。 

作業手順
1. 新規プロジェクトの作成
プロジェクト名: PracticalExersixes7
パッケージ名: hoge.practicalexersixes7(hogeはリバースドメインなどのユニークな文字列)
ビルド・ターゲット: Android 1.6

2. strings.xmlの編集
PracticalExercises7 → res → valuesの デフォルト(英語用)のstrings.xmlを編集 $tototan-2

【strings.xml】デフォルト(英語用)
$tototan-stringEN
3. 日本語用string.xmlを作成
  
resを右クリック → 新規 → その他 $tototan-3_1   
Android XML Fileを選択して次へ $tototan-3_2   
ファイル名に【strings.xml】と入力 → The destination file already existsという警告が表示されるが無視して次へ $tototan-3_3   
左のリストから【語Language】を選択して【->】をクリック   
言語テキストボックスへ【ja】と入力して完了 $tototan-3_4   
resの下に【values-ja】フォルダーができてその中に【strings.xml】がある事を確認   
strings.xmlを編集

【strings.xml】日本語用
$tototan-stringJP

5. layout.xmlを作成

  resの下のlayoutを右クリック【Android XML Lyaout File】を選択して次へ

  ルート要素のリストから【LinearLayout】を選択してファイル名に【layout】(何でもいい)と入力
【layout.xml】
$tototan-layout_1 $tototan-layout_2 $tototan-layout_3

6. javaファイルの編集

package com.me.tototan;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.graphics.Color;

import android.media.AudioManager;

import android.media.ToneGenerator;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import android.widget.EditText;

import android.widget.LinearLayout;

import android.widget.Toast;

import java.text.DecimalFormat;


import net.npaka.PracticalExercises7.R;


public class PracticalExercises7Activity extends Activity implements View.OnClickListener {

private float bmi;

private Button calcButton;

private Button  clearButton;

private EditText heightEt;

private EditText weightEt;

private TextView bmiTv;

private TextView resultTv;

private LinearLayout liLayout; // 背景画像切替用

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout); // レイアウトの指定

        

        // コンポーネントの関連付け

        calcButton = (Button)this.findViewById(R.id.button1);

        calcButton.setOnClickListener(this);

        clearButton = (Button)this.findViewById(R.id.button2);

        clearButton.setOnClickListener(this);

        heightEt = (EditText)this.findViewById(R.id.editText1);

        weightEt = (EditText)this.findViewById(R.id.editText2);

        bmiTv = (TextView)this.findViewById(R.id.result_view);

        resultTv = (TextView)this.findViewById(R.id.info_view);

        liLayout = (LinearLayout)this.findViewById(R.id.layout);

        

        Toast.makeText(this, R.string.message, Toast.LENGTH_LONG).show(); // トーストの表示

        

    }

   

    // ダイアログの表示

    private static void showDialog(final Activity activity,String title,String text) {

    AlertDialog.Builder ad = new AlertDialog.Builder(activity);

    ad.setTitle(title);

    ad.setMessage(text);

    ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dailog,int whichButton) {

    activity.setResult(Activity.RESULT_OK);

    }

    });

    ad.create();

    ad.show();

    }

    

    // ボタンクリックイベントの処理

    public void onClick(View view) {

    float height;

    float weight;

    

    // ボタンタップ時の効果音の作成

    ToneGenerator toneGenerator = new ToneGenerator(

            AudioManager.STREAM_SYSTEM,

            ToneGenerator.MAX_VOLUME);

    toneGenerator.startTone(ToneGenerator.TONE_PROP_BEEP);

    toneGenerator.stopTone();

    

    // 計算ボタンを押した時の処理

    if (view == calcButton) {

    try {

    height = Float.parseFloat(heightEt.getText().toString());

    weight = Float.parseFloat(weightEt.getText().toString());

    bmi = weight/((height/100)*(height/100));

    DecimalFormat df = new DecimalFormat("000.0"); // 小数点以下第一位でフォーマット

    String formattedBmi = df.format(bmi);

    bmiTv.setText(formattedBmi);

    resultTv.setText(hantei(bmi));

    } catch (NumberFormatException e) {

    showDialog(this,"",getString(R.string.warn_message_3)); // 例外発生時のダイアログの表示

    }

    }

    

    // クリアボタンを押した時の処理

    else if (view == clearButton) {

    bmiTv.setText("000.0");

resultTv.setTextColor(Color.MAGENTA);

resultTv.setTextSize(20);

    resultTv.setText(getString(R.string.info_message_1));

    heightEt.setText(null);

    weightEt.setText(null);

    liLayout.setBackgroundResource(R.drawable.beach);

    }

    }

    

    // BMI の判定

public String hantei(float value) {

if (value < 18.5) {

resultTv.setTextColor(Color.RED); // 文字色を指定

resultTv.setTextSize(30); // テキストサイズを指定

liLayout.setBackgroundResource(R.drawable.beach); // 背景画像を指定

return getString(R.string.bmi_categories_under_weight); // 判定結果を返す

} else if (value < 25.0) {

resultTv.setTextColor(Color.GREEN);

resultTv.setTextSize(30);

liLayout.setBackgroundResource(R.drawable.sky);

return getString(R.string.bmi_categories_normal_weight);

} else if (value < 30.0) {

resultTv.setTextColor(Color.RED);

resultTv.setTextSize(30);

liLayout.setBackgroundResource(R.drawable.bg1);

return getString(R.string.bmi_categories_over_weight);

} else {

resultTv.setTextColor(Color.RED);

resultTv.setTextSize(30);

liLayout.setBackgroundResource(R.drawable.bg1);

return getString(R.string.bmi_categories_obesity_weight);

}

}

}

BMIの計算、多言語化

自作iPhoneアプリ一覧

環境:Mac Lion,Eclipse 3.7(日本語バージョン)

以下の仕様書に基づいてアプリケーションを作成します。

1.アプリケーション名は、PracticalExercises7Bとすること。
2.体重、身長を小数点で入力できるUIをXMLファイルで実装すること。
3.体重と身長から、BMIを計算し、小数点第一位まで画面上に表示すること。
4. BMIより、下表に対応した体格の診断結果をユーザーに通知すること。

BMI値の範囲 体格の傾向
18.5未満 痩せている
18.5 以上 25.0 未満 標準
25.0 以上 30.0 未満 太っている
30.0 以上 肥満

5.計算を開始する、または、表示されている数値をクリア(初期化)するイベントを実装すること。
6.不正な値が入力された場合など、異常が発生した場合には、異常をユーザに通知すること。
7.BMIの計算は、下式を用いて算出する。身長の単位は、m(メートル)を用いて計算を行う。
  BMI値 = [体重(kg)] / ([身長(m)]×[身長(m)])
8.診断結果別に背景と診断結果表示文字列の色を変更する。
9. 日本語と英語の多言語対応とする。
10. Androidのバージョンは、2.1を元に作成する。 

作業手順
1. 新規プロジェクトの作成
プロジェクト名: PracticalExersixes7
パッケージ名: hoge.practicalexersixes7(hogeはリバースドメインなどのユニークな文字列)
ビルド・ターゲット: Android 1.6
2. strings.xmlの編集
PracticalExercises7 → res → valuesの
デフォルト(英語用)のstrings.xmlを編集
$tototan-2

3. 日本語用string.xmlを作成
  resを右クリック → 新規 → その他
$tototan-3_1
  Android XML Fileを選択して次へ
$tototan-3_2
  ファイル名に【strings.xml】と入力 → The destination file already existsという警告が表示されるが無視して次へ
$tototan-3_3
  左のリストから【語Language】を選択して【->】をクリック
  言語テキストボックスへ【ja】と入力して完了
$tototan-3_4
  resの下に【values-ja】フォルダーができてその中に【strings.xml】がある事を確認
  strings.xmlを編集
【strings.xml】

SurfaceViewを使った複数図形の移動

自作iPhoneアプリ一覧 

内容:

1. SurfaceViewを利用してUIスレッドから独立して描画を行う。(高速な連続描画が必要なときに使える)

2. 円のX座標、Y座標、X方向への移動量、Y方向への移動量、半径、色のフィールドと移動のメソッドを持つ【CircleData】クラスを内部クラスとして作成し、ArrayListを利用して複数のインスタンスを生成することで複数の円を動かす。

環境:Mac Lion,Eclipse 3.7(日本語バージョン)

1. ファイル  新規 → プロジェクト

031401


2. Android プロジェクトを選択して【次へ】をクリック

031402


3. プロジェクト名【PracticalExercises6】を指定して【次へ】をクリック

031403


4. インストール済みのAVDにチェックをして【次へ】をクリック

031404

5. パッケージ名(リバースドメインなど+プロジェクト名【.PracticalExercises6】)を入力して【完了】をクリック

031405

6. 左側のパッケージエクスプローラーのプロジェクト名【PracticalExercises6】を展開してパッケージ名のポップアップから 新規 → クラスを選択

031406

7. クラス名【PracticalExercises6View】を入力して【完了】をクリック




【PracticalExercises6.java】

package com.me.tototan.PracticalExdrcises6;

import android.app.Activity;

import android.os.Bundle;

import android.graphics.PixelFormat;

import android.view.Window;

public class PracticalExercises6Activity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        getWindow().setFormat(PixelFormat.TRANSLUCENT); // 半透明をサポートするフォーマットを選ぶ

        setContentView(new PracticalExercises6View(this));    }

}

【PracticalExercises6View.java

package com.me.tototan.PracticalExercises6;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.Color;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

import java.util.ArrayList;


public class PracticalExercises6View extends SurfaceView implements SurfaceHolder.Callback,Runnable {

private SurfaceHolder holder; // サーフェイスホルダー

private Thread thread; // スレッド

   // コンストラクタ

public PracticalExercises6View(Context context) {

super(context);

// サーフェイスホルダーの生成

holder = getHolder();

holder.addCallback(this);

holder.setFixedSize(getWidth(), getHeight());

}

// サーフェイスの生成

public void surfaceCreated(SurfaceHolder holder) {

// スレッドの開始

thread = new Thread(this);

thread.start();

}

// サーフェイスの変更

public void surfaceChanged(SurfaceHolder holder,int format,int w,int h) {

} // サーフェイスの破棄

public void surfaceDestroyed(SurfaceHolder holder) {

thread = null;

}

// スレッドの処理

public void run() {

Canvas canvas;

// ArrayListのインスタンスを生成

ArrayList<CircleData> list = new ArrayList<PracticalExercises6View.CircleData>();

list.add (new CircleData(0,0,10,10,10,Color.BLUE));

list.add (new CircleData(0,0,15,15,20,Color.YELLOW));

list.add (new CircleData(0,0,20,10,30,Color.CYAN));

list.add (new CircleData(0,0,10,20,40,Color.GREEN));

list.add (new CircleData(0,0,20,20,50,Color.MAGENTA));

list.add (new CircleData(0,0,20,15,30,Color.RED));

  while(thread !=null) { // ダブルバッファリング

    Paint paint = new Paint();

    canvas = holder.lockCanvas();

canvas.drawColor(Color.WHITE);

    for (int i = 0; i < list.size(); i++) {

paint.setColor(list.get(i).color);

canvas.drawCircle(list.get(i).px,list.get(i).py,list.get(i).radius, paint);

}

    holder.unlockCanvasAndPost(canvas);     // 移動

    for (int i = 0; i < list.size(); i++) {

int width = getWidth();

int height = getHeight();

list.get(i).move(width,height);

    }

// スリーブ

try {

Thread.sleep(50);

} catch (Exception e) {

}

}

 }

// 円のデータを保持するクラス

     public class CircleData {

     private int px; // X座標

     private int py; // Y座標

     private int vx; // X速度

     private int vy; // Y速度

     private int radius; // 半径

     private int color; // 色

      public CircleData(int px, int py, int vx, int vy,int radius,int color) {

    this.px = px;

    this.py = py;

    this.vx = vx;

    this.vy = vy;

    this.radius = radius;

    this.color = color;

     }

      void move(int width, int hight) {

// 移動

if (px < 0 || width < px) vx = -vx;

if (py < 0 || hight < py) vy = -vy;

px += vx;

py += vy;

    }


    }

}