package com.bitcare.view; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class NavMapView extends View { private Bitmap[] mBitmaps; private OnClickBitmapListener clickBitmapListener; public NavMapView(Context context) { super(context); } public NavMapView(Context context, AttributeSet attrs) { super(context, attrs); } /** * 設置圖片 * @author yubin * @date 2013-10-12 */ public final void setBitmaps(Bitmap[] bitmap) { this.mBitmaps = bitmap; invalidate(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mBitmaps != null) { for (int i = 0; i < mBitmaps.length; i++) { canvas.drawBitmap(mBitmaps[i], 0, 0, null); } } } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { which(event.getX(), event.getY()); } return true; } public void which(float x, float y) { if (mBitmaps != null) { for (int i = 0; i < mBitmaps.length; i++) { // 判斷座標點不超過圖片得寬高 if ((int) x > mBitmaps[0].getWidth() || (int) y > mBitmaps[0].getHeight()) { clickBitmapListener.ClickBitmap(-1); break; } Bitmap mBitmap = mBitmaps[i]; // 判斷座標點是不是在圖片得透明區域 if (mBitmap.getPixel((int) x, (int) y) != 0) { clickBitmapListener.ClickBitmap(i); break; } } } } public final void setOnClickBitmapListener(OnClickBitmapListener listener) { this.clickBitmapListener = listener; }; public static interface OnClickBitmapListener { /** * @param index -1表示超出範圍 * @author yubin * @date 2013-10-12 */ void ClickBitmap(int index); } }
版權聲明:本文爲博主原創文章,未經博主容許不得轉載。java