Androidで画像がリサイズ可能なクラスを作る
こちらのサイト「http://www.anddev.org/resize_and_rotate_image_-_example-t621.html」
を参考に画像の拡大・縮小をしたがかなり面倒な手順になるので、
画像の縮小・拡大を操作するクラスを作成した
■上記サイトの画像変換手順
BitmapDrawAbleクラスを使って画像を描画するには以下の3つのクラスが必要なる
- Bitmapクラス
- BitmapDrawAbleクラス
- Rectクラス
描画データのりサイズをするには、さらに2つのクラスが必要になる
- Matrixクラス
- Bitmapクラス(リサイズBitmapのため一時的に必要)
さらにMatrixクラスを使うにはリサイズ演算処理が必要になる
- リサイズスケールの算出
- 高さ、幅
- Matrixクラスにリサイズ設定
さらにリサイズ設定をしたMatrixを使ってリサイズBitmapを新たに作成しなくてはならない
Bitmap resizeBitmap = Bitmap.createBitmap(OrigBitmap, 0, 0, OrigBitmap.getWidth(), OrigBitmap.getHeight(), matrix, true);
さらに出来上がったリサイズBitmapを使ってBitmapDrawableを生成する
さらにリサイズデータにあったRectを初期化する
さらに出来上がったBitmapDrawableとRectを使ってboundする
このように画像をリサイズするにはリサイズ画像を用意するだけでなくその他の設定が必要にある
これらの手順が面倒くさいのでリサイズ可能な画像操作クラスを作成した。
- クラス名:BitmapResizable(もうちょいいい名前ないかな)
- フィールド:
- BitmapDrawable bitmapDrawAbel
- Bitmap bitmap
- Rect rect
- x
- y
- コンストラクタ
- Bitmap型の引数
- メソッド
- x,yのgetter setter
- resize:リサイズする
- draw:キャンバスに描画する
RectやBitmapを直にいじることはないのでgetter・setterはなしにした。
必要に応じて追加すればいい
■コンストラクタ
Bitmapを初期化
public BitmapResizable(Bitmap bitmap) { this.bitmap = bitmap; this.bitmapDrawAbel = new BitmapDrawable(this.bitmap); }
■resizeメソッド
画像のリサイズ処理を行う。
処理詳細
- リサイズスケールの設定
- Matrixにリサイズスケールを指定する
- リサイズしたBitmapオブジェクトを作成する
- BitmapDrawAbleにリサイズ画像をセットする
- 画像に合わせてRectを初期化
- setBoundする
/** * サイズ変換 * @param resizeWidth リサイズ後の幅 * @param resizeHeight リサイズ後の高さ */ public void resize(float resizeWidth, float resizeHeight){ float resizeScaleWidth; float resizeScaleHeight; Matrix matrix = new Matrix(); resizeScaleWidth = resizeWidth / this.bitmap.getWidth(); resizeScaleHeight = resizeHeight / this.bitmap.getHeight(); matrix.postScale(resizeScaleWidth, resizeScaleHeight); Bitmap resizeBitmap = Bitmap.createBitmap(this.bitmap, 0, 0, this.bitmap.getWidth(), this.bitmap.getHeight(), matrix, true); this.bitmapDrawAbel = new BitmapDrawable(resizeBitmap); }
■drawメソッド
canvasに描画する。
メンバ変数の座標で描画するものと、オーバーロードして引数に座標指定できるメソッドを作成
public void draw(Canvas canvas) { this.rect = new Rect( this.x, this.y, this.x + this.bitmapDrawAbel.getIntrinsicWidth(), this.y + this.bitmapDrawAbel.getIntrinsicHeight()); this.bitmapDrawAbel.setBounds(this.rect); this.bitmapDrawAbel.draw(canvas); } public void draw(int x, int y, Canvas canvas) { this.rect = new Rect( x, y, x + this.bitmapDrawAbel.getIntrinsicWidth(), y + this.bitmapDrawAbel.getIntrinsicHeight()); this.bitmapDrawAbel.setBounds(this.rect); this.bitmapDrawAbel.draw(canvas); }
使用例
■MyViewクラス
Viewクラスを継承した描画データ表示クラスの作成
MyViewクラス
- BitmapResizable bitmapResizable
- コンストラクタのオーバライド
- drawメソッドのオーバライド
処理
コンストラクタでリサイズ設定をする
drawメソッドでBitmapResizableのdrawを呼び出す
public MyView(Context context) { super(context); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.android1_img1); this.bitmapResizable = new BitmapResizable(bitmap); float resizeWidth = 100; float resizeHeight = 100; this.bitmapResizable.resize(0, 0, resizeWidth, resizeHeight); } @Override public void draw(Canvas canvas) { // TODO Auto-generated method stub super.draw(canvas); this.bitmapResizable.draw(canvas); }
■Actibity
setContentViewでMyViewクラスを引数に設定する
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new MyView(this)); }
ソース
■BitmapResizable
package resize.sample; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; public class BitmapResizable { private BitmapDrawable bitmapDrawAbel; private Bitmap bitmap; private Rect rect; private int x; private int y; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public BitmapResizable(Bitmap bitmap) { this.bitmap = bitmap; this.bitmapDrawAbel = new BitmapDrawable(this.bitmap); } /** * サイズ変換 * @param resizeWidth リサイズ後の幅 * @param resizeHeight リサイズ後の高さ */ public void resize(float resizeWidth, float resizeHeight){ float resizeScaleWidth; float resizeScaleHeight; Matrix matrix = new Matrix(); resizeScaleWidth = resizeWidth / this.bitmap.getWidth(); resizeScaleHeight = resizeHeight / this.bitmap.getHeight(); matrix.postScale(resizeScaleWidth, resizeScaleHeight); Bitmap resizeBitmap = Bitmap.createBitmap(this.bitmap, 0, 0, this.bitmap.getWidth(), this.bitmap.getHeight(), matrix, true); this.bitmapDrawAbel = new BitmapDrawable(resizeBitmap); } public void draw(int x, int y, Canvas canvas) { this.rect = new Rect( x, y, x + this.bitmapDrawAbel.getIntrinsicWidth(), this.y + this.bitmapDrawAbel.getIntrinsicHeight()); this.bitmapDrawAbel.setBounds(this.rect); this.bitmapDrawAbel.draw(canvas); } public void draw(Canvas canvas) { this.rect = new Rect( this.x, this.y, this.x + this.bitmapDrawAbel.getIntrinsicWidth(), this.y + this.bitmapDrawAbel.getIntrinsicHeight()); this.bitmapDrawAbel.setBounds(this.rect); this.bitmapDrawAbel.draw(canvas); } }
■MyView
package resize.sample; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.view.View; public class MyView extends View { private BitmapResizable bitmapResizable; public MyView(Context context) { super(context); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.android1_img1); this.bitmapResizable = new BitmapResizable(bitmap); float resizeWidth = 100; float resizeHeight = 100; this.bitmapResizable.resize(resizeWidth, resizeHeight); } @Override public void draw(Canvas canvas) { super.draw(canvas); this.bitmapResizable.draw(0, 0, canvas); } }