基本上有两个障碍如下:
以下是代码:
private ImageView mImageView;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.vp_view_img);
if(getIntent().hasExtra("BitmapImage"))
{
final Bitmap bitmap = (Bitmap) getIntent().getParcelableExtra("BitmapImage");
mImageView = (ImageView) findViewById(R.id.canvas_image);
mImageView.setImageBitmap(bitmap);
mImageView.setOnTouchListener(new ImageView.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
int x = (int)event.getX();
int y = (int)event.getY();
/*Toast.makeText(getBaseContext(), "Touch coordinates : "
+ String.valueOf(event.getX()) + "x"
+ String.valueOf(event.getY()),
Toast.LENGTH_SHORT).show();*/
ImageView imageView = (ImageView) v;
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
Toast.makeText(getBaseContext(), "Color Code :"+
redValue+blueValue+greenValue,Toast.LENGTH_SHORT).show();
return true;
}
});
}
else
{
Toast.makeText(getBaseContext(),
"No Image Found",
Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:3)
让我们从第二个问题开始:为了将Color
(表示为Integer
)转换为相应的十六进制值,您应该知道,十六进制颜色表示实际上是:
#RGB, #RRGGBB, #RRRGGGBBB, #RRRRGGGGBBBB
因此,您可以获取所需颜色的基色组件,并将它们单独转换为十六进制字符串,然后将它们放在一起:
/**
* Converts the passed integer (color) into it's hexadecimal representation
* @param pixel the Color to convert
* @return a String: the <code>pixel</code> parameter in #RRGGBB format
*/
private String rgbToHex(int pixel)
{
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
return "#" + getBaseColorAsHex(redValue) +
getBaseColorAsHex(greenValue) +
getBaseColorAsHex(blueValue);
}
/**
* Returns the hex representation of the <code>baseColor</code>
* parameter, padding up to 2 characters if necessary.
* @param baseColor the color whose hex representation is needed
* @return the hex code for the passed parameter
*/
private String getBaseColorAsHex(int baseColor)
{
final String hex = Integer.toHexString(baseColor);
return (hex.length() == 1 ? '0' : "") + hex;
}
从第一个问题开始,你应该提供你的logcat输出:在不知道错误的情况下,我们无法帮助解决它。
此外,您应该考虑将问题分成两部分,因为在这种形式下它不符合Stack Overflow的指导原则!
编辑关于第一个问题:一个严重的问题可能是您分析的图像小于屏幕,并且它被拉伸,因此当您点击屏幕并检索x
和y
坐标,将它们镜像到图像上会因其大小而失败:x或y的值可能大于宽度,也就是位图的高度。
除非您使用try-catch
块并记录异常,否则您将无法确定问题的确切位置。
答案 1 :(得分:-1)
如何使用选择器?
在res / drawable文件夹中,制作xml文件,如下面的代码
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@color/white"/> <!-- selected -->
<item android:state_pressed="true" android:color="@color/white" /> <!-- pressed -->
<item android:color="@color/black" />
</selector>