Reputation: 21
Basically there are two hindrances as follows:
Here's the code:
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();
}
}
Upvotes: 1
Views: 732
Reputation: 19220
Let's start with the second question: In order to convert a Color
(represented as an Integer
) into the corresponding hex value, you should know, that hexadecimal color representations are actually:
#RGB, #RRGGBB, #RRRGGGBBB, #RRRRGGGGBBBB
So you can take the base color components of the color you want, and turn them individually into a hex string, then put them together:
/**
* 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;
}
As of the first question, you should provide your logcat output: without any knowledge of the error, we cannot help solving it.
Also, you should consider splitting up your question in two for real, because in this form it does not fit into the Stack Overflow's guidelines!
Edit: About the first question asked: One serious problem could be that the image you are analyzing is smaller than the screen, and it is stretched, so when you tap on the screen and retrieve the x
and y
coordinates, mirroring them onto the image fails due to it's size: the value of x or y might be greater then the width, respectively the height of your bitmap.
Unless you use try-catch
blocks and log the exception, you won't be able to tell where the problem really is.
Upvotes: 3
Reputation: 246
how about use selector?
in res/drawable folder, make xml file like below code
<?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>
Upvotes: -1