我想在我的应用中添加一个颜色选择器,并在布局中添加了一个ImageView:
<ImageView
android:id="@+id/img_colorpicker"
android:adjustViewBounds="false"
android:cropToPadding="false"
app:srcCompat="@drawable/color_picker"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="0.06" />
以下代码应在用户触摸时返回像素的颜色:
img_colorpicker.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int[] viewCoords = new int[2];
img_colorpicker.getLocationOnScreen(viewCoords);
int touchX = (int) event.getX();
int touchY = (int) event.getY();
System.out.println(touchX + "-" + touchY);
int imageX = touchX - viewCoords[0];
int imageY = touchY - viewCoords[1];
Bitmap bitmap = ((BitmapDrawable)img_colorpicker.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(touchX,touchY);
int redValue = Color.red(pixel); // I want these Values
int greenValue = Color.green(pixel); // I want these Values
int blueValue = Color.blue(pixel); // I want these Values
return false;
}
});
我认为位图具有真实的图像尺寸(2002 x 1127),因此计算出的触摸值不正确... 我需要找出位图的比例因子。
img_colorpicker.getScaleX(); - &GT;返回1