Java Android, how can I make ImageView darker onClick

As the iOS developer told me, iOS makes the image darker when someone clicks it (I don't know how true this is), so I need the same thing.

as I understand it, I need a regular selector.xml to make drawable with different states. I need to darken the image in a simple way, perhaps in xml-drawable, so I do not need to use Photoshop, in which I have no skills.

Is there any way to do this at the xml level?

+6
source share
4 answers

You can place a dark rectangle behind your bitmap and set Bitmap's Opacity instead of trying in xml .

+4
source

In your java class, you can use the following code:

 mView.getBackground().setColorFilter(Color.parseColor(<Color code of your choice>), PorterDuff.Mode.DARKEN); 

This line above changes the color of the view to the fraction of the color that you offer in Color.parseColor (), and the hue as you define it with PorterDuff.Mode. Call the above code with the click of a button to check if a color change is entering.

+6
source

I think you can do this by adding a clickListener to your image ...

 ImageView image = (ImageView) view.findViewById(R.id.image); icon.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ImageView image = (ImageView)v; image.setImageAlpha(alpha); } }); 
0
source

I used

 private void darkenImage(ImageView imageView) { imageView.setColorFilter(Color.rgb(123, 123, 123), android.graphics.PorterDuff.Mode.MULTIPLY); } private void undarkenImage(ImageView imageView) { imageView.clearColorFilter(); } private boolean isImageDarkened(ImageView image) { return image.getColorFilter() != null; } 

works very well!

0
source

Source: https://habr.com/ru/post/958980/


All Articles