嗨,我想在触摸图像视图时获得像素颜色,当 xml 中图像视图的宽度和高度是包装内容时,此代码工作正常
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".2"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="@+id/iv_cam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
</LinearLayout>
,当我在 xml 文件中设置图像视图包装内容的宽度和高度以匹配父级时,会出现问题
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".2"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="@+id/iv_cam"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" >
</ImageView>
</LinearLayout>
通过这样做,小图像变得拉伸错误来 E/MessageQueue-JNI(1006): java.lang.IllegalArgumentException: x must be < bitmap.width()
我该如何解决这个...
@Override public boolean onTouch(View v, MotionEvent 事件) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
ImageView iv = ((ImageView)v);
Bitmap bmp = ((BitmapDrawable)iv.getDrawable()).getBitmap();
int pixel = bmp.getPixel((int)event.getX(),(int)event.getY());// error comes in this line
int alphaValue = Color.alpha(pixel);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
Toast.makeText(this,"[" +alphaValue+"," +redValue+","+greenValue+","+blueValue+"]", Toast.LENGTH_LONG).show();
}
return false;
}