安卓开发区分地图不同的块

来源:互联网 发布:unity3d怎么创建地面 编辑:程序博客网 时间:2024/06/07 07:11
安卓开发区分地图不同的块
关于取屏幕的像素值问题,由于项目的需要,在地图上要划分不同的块来实现点击事件,解决办法就是把不同的块设为不同的颜色值,点击屏幕时得到坐标,然后把屏幕坐标转化为bitmap里实际的坐标,然后就可以直接获得像素值,代码如下:

DisplayMetrics metric = getResources().getDisplayMetrics();
BitmapFactory.Options bfoOptions = new BitmapFactory.Options();
bfoOptions.inScaled = false;
// getWindowManager().getDefaultDisplay().getMetrics(metric);
if (bitmap == null)
bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.all, bfoOptions);
float density = metric.density;
int sWidth = (int) (metric.widthPixels); // 屏幕宽度(像素)
int sHeight = (int) (metric.heightPixels);


int bWidth = bitmap.getWidth();// bitmap宽高
int bHeight = bitmap.getHeight();


double rWidthPro = (double) bWidth / sWidth;// 比例
double rHeightPro = (double) bHeight / sHeight;


int pX = (int) (event.getRawX() * rWidthPro);// bitmap实际坐标
int pY = (int) (event.getRawY() * rHeightPro) - 50;
// if (event.getRawY() > sHeight / 3)
pY = pY - 50;
int value = bitmap.getPixel(pX, pY);
ColorValue colorValue = new ColorValue();
colorValue.x = Color.red(value);
colorValue.y = Color.green(value);
colorValue.z = Color.blue(value);

final String cityName = getCityName(colorValue);


if (cityName == null)
return true;
int resid = getResources().getIdentifier(cityName, "drawable",
getPackageName());
Drawable drawable = getResources().getDrawable(resid);
image.setImageResource(resid);


0 0