サブクラスには
- BitmapDrawable
- ColorDrawable
- RotateDrawable
- ScaleDrawable etc...
今回は BitmapDrawable クラスを使ってみた
BitmapDrawable は
java.lang.Object
↳ android.graphics.drawable.Drawable
↳ android.graphics.drawable.BitmapDrawable
を使用しますというわけで BitmapDrawable をいじってみた
サンプルプログラム
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
public class DrawableTest extends Activity {
BitmapDrawable bitmapDrawable;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new DrawableTestView(this));
}
class DrawableTestView extends View {
public DrawableTestView(Context context) {
super(context);
// BitmapDrawable を作成
bitmapDrawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.icon);
}
@Override
public void onDraw(Canvas canvas) {
// BitmapDrawable の範囲を設定
bitmapDrawable.setBounds(0, 0, 48, 48);
// BitmapDrawable の描画
bitmapDrawable.draw(canvas);
// BitmapDrawable のアルファ値を設定
bitmapDrawable.setAlpha(128);
bitmapDrawable.setBounds(48, 0, 96, 48);
bitmapDrawable.draw(canvas);
bitmapDrawable.setAlpha(255);
// BitmapDrawable にアンチエイリアスを設定
bitmapDrawable.setAntiAlias(true);
bitmapDrawable.setBounds(0, 48, 240, 288);
bitmapDrawable.draw(canvas);
bitmapDrawable.setAntiAlias(false);
bitmapDrawable.setBounds(240, 48, 480, 288);
bitmapDrawable.draw(canvas);
bitmapDrawable.setBounds(0, 0, getWidth(), getHeight());
// BitmapDrawable にグラビティを設定
bitmapDrawable.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
bitmapDrawable.draw(canvas);
}
}
}
プログラムを実行してみると...
こんな感じ!(左:アンチエイリアスあり, 右:なし)
この画像だとアンチエイリアスの違いはよく分からないですね
状況によって使い分けるのが良さそう
参考サイト
http://developer.android.com/intl/ja/reference/android/graphics/drawable/Drawable.html
http://developer.android.com/intl/ja/reference/android/graphics/drawable/BitmapDrawable.html

0 件のコメント:
コメントを投稿