Home > Android Archive
Android Archive
Android: リソースの画像ファイルの拡大・縮小描画 - drawBitmap()
- 2010-01-02 (土)
- Android
表示する画像はEclipse上でAndroidプロジェクト作成時に自動的に作成されるIcon画像です。
画像パス:プロジェクト名/res/drawable-hdpi/icon.png
resフォルダ以下に置かれたリソースはコンパイル時にプログラムに組み込まれます。その画像リソースを読み込む際は、
Bitmap BitmapFactory.decodeResource(Resources r, int resourcesID)
を用います。読み込んだBitmapインスタンスを描画するには、Canvasクラスのインスタンスメソッドである
void drawBitmap(Bitmap image, int x, int y, Paint p)
を使います。なお、拡大・縮小する場合も上記のdrawBitmapをオーバーロードしたものを使います。
void drawBitmap(Bitmap image, Rect src, Rect dst, Paint p)
今回のサンプルプログラムでは、元画像の幅と高さを2倍したイメージを描画しています。
ImageSp.java
package info.yukun.imagesp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class ImageSp extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new ImageView(this));
}
}
ImageView.java
package info.yukun.imagesp;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.view.View;
public class ImageView extends View {
private Bitmap image;
public ImageView(Context context) {
super(context);
setBackgroundColor(Color.WHITE);
// リソースの画像ファイルの読み込み
Resources r = context.getResources();
image = BitmapFactory.decodeResource(r, R.drawable.icon);
}
@Override
protected void onDraw(Canvas canvas) {
// イメージ描画
canvas.drawBitmap(image, 0, 0, null);
int w = image.getWidth();
int h = image.getHeight();
// 描画元の矩形イメージ
Rect src = new Rect(0, 0, w, h);
// 描画先の矩形イメージ
Rect dst = new Rect(0, 200, w*2, 200 + h*2);
canvas.drawBitmap(image, src, dst, null);
}
}
- Comments: 0
- Trackbacks: 2
Android: 10進数→2進数変換アプリ
- 2008-10-29 (水)
- Android
試しに下図のような簡素な10進2進変換アプリを作ってみました。
以下は書いてみたコード。
ソースコード
res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Integer to Binary Calculator</string>
<string name="label_description">10進数を2進数に変換表示</string>
<string name="label_integer">10進数:</string>
<string name="label_binary">2進数:</string>
<string name="button_convert">変換</string>
</resources>
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<linearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<textView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/label_description"
/>
<linearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<textView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_integer"
/>
<editText android:id="@+id/text_integer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:maxLength="9"
android:text=""
/>
</linearLayout>
<button android:id="@+id/button_convert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_convert"
/>
<textView android:id="@+id/label_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
</linearLayout>
ITBCalculatorActivity.java
package info.yukun.android.itbcalc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ITBCalculatorActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) { // アクティビティが生成される際に必ず呼び出される
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // UIのレイアウトを設定
// 指定したリソース(R.java)インデックスからビュー(res/layout/main.xml)内のidのコンポーネント(ここではButton)インスタンスを取得
Button button = (Button) findViewById(R.id.button_convert);
button.setOnClickListener(convertToBinary); // ボタンが押された際のイベントを登録
}
// 登録するイベントリスナーはView.OnClickListenerを実装
private View.OnClickListener convertToBinary = new View.OnClickListener() {
public void onClick(View view) { // ボタンが押されたときに呼び出されるメソッド
EditText textInteger = (EditText) findViewById(R.id.text_integer); // 入力値が入っているコンポーネントを取得
String input = textInteger.getText().toString();
if (input.equals("")) return;
int intValue = Integer.parseInt(input);
String binValue = Integer.toBinaryString(intValue); // 10進数整数を2進数文字列に変換
TextView labelResult = (TextView) findViewById(R.id.label_result); // 結果を表示するTextViewを取得
labelResult.setText("2進数:" + binValue);
}
};
}
Androidアプリを書いてみて
EditText android:id="@+id/text_integer"の属性android:maxLengthを"9"としたのは、変換メソッドInteger.toBinaryString(int value)の引数が32bit整数の為です。本当は属性値を10としてonClickメソッド内で32bitの範囲内(-2 147 483 647 ~ 2 147 483 647 = (2^31) - 1)か否かを判定したほうが良いと思うのですが、今回は端折りました。
ビジュアルエディタやXMLでUIのレイアウトを作っていくというのはFlexのmxmlに似てて取っ掛かり易い感じです。
- Comments: 0
- Trackbacks: 0
Home > Android Archive
