ユーザーには見えないが、タッチするとアクションを実行する複数の「ヒットボックス」を持つ画像に依存するマルチタッチアプリを作成しています。
ヒットボックス情報を保持するキャンバスとビットマップを作成しました。ただし、onDraw でのビューのキャンバスのパンとスケーリングは、ヒットボックス領域を持つビットマップと同じではありません。
具体的なコードについては、以下を参照してください。(hitbox と b はビットマップ、hb はキャンバス)
public TouchImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mBackground = this.getBackground();
mBackground.setBounds(0, 0, mBackground.getIntrinsicWidth(), mBackground.getIntrinsicHeight());
this.setBackgroundResource(android.R.color.transparent);
// Create our ScaleGestureDetector
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
hitbox = BitmapFactory.decodeResource(context.getResources(), R.drawable.hitbox);
b = hitbox.copy(Bitmap.Config.ARGB_8888, true);
hb = new Canvas(b);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
super.onDraw(hb);
canvas.save();
hb.save();
canvas.translate(mPosX, mPosY);
hb.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor);
hb.scale(mScaleFactor, mScaleFactor);
mBackground.draw(canvas);
canvas.restore();
hb.restore();
}