0

I have the below layout at first :

Original

Later on a particular condition i want to make the layout grayed out like this :

Grayscale

I tried to add a gray view in front of the layout like this :

  <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <include layout="@layout/search_item"/> //this is the original layout
        <View
            android:background="#90000000"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </RelativeLayout>

but this is not working. anyone can help me with this?

Parth Anjaria
  • 3,961
  • 3
  • 30
  • 62
  • Simply disable it. – Phantômaxx Nov 05 '16 at 10:09
  • @Rotwang how do we do it? – Parth Anjaria Nov 05 '16 at 10:10
  • By using `setEnabled(true|false);` – Phantômaxx Nov 05 '16 at 10:13
  • setEnabled(true|false); on what? on the layout? – Parth Anjaria Nov 05 '16 at 10:14
  • http://stackoverflow.com/a/7069377/2649012 – Phantômaxx Nov 05 '16 at 10:18
  • using `setEnabled` does not make your view (and its child views) draw in grayscale, you have to create a custom `ViewGroup` (`FrameLayout` for example) and override its `draw` / `dispatchDraw` method – pskink Nov 05 '16 at 10:34
  • @pskink Not necessarily. You can define a [StateListDrawable](https://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html) for your Views – Phantômaxx Nov 05 '16 at 11:06
  • @Rotwang StateListDrawable? what would you do with `SLD`? how would it make a red heart became grey? – pskink Nov 05 '16 at 11:08
  • @pskink Did you know? SLD has an [`enabled`](https://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html#attr_android:state_enabled) state. Just associate a greyscale drawable to that state. – Phantômaxx Nov 05 '16 at 11:09
  • @Rotwang the OP has an `ImageView` with a red heart, how will it change a red heart into grey heart? SLD has nothing do with child views drawing – pskink Nov 05 '16 at 11:13
  • @pskink The OP is supposed to provide 2 version of the bitmap: colorful and greyscale. – Phantômaxx Nov 05 '16 at 11:22
  • @Rotwang also two versions of yellow / grey star in the `RatingBar`? also two versions of the avatar on the left (most likely downloaded from the web service)? no, he just want one view and its children to be drawn in grey scale, no matter what is drawn on it – pskink Nov 05 '16 at 11:26
  • @pskink Of course. 2 versions of all. This is the fastest way. Rather than calculating a greyscaled version of every bitmap. – Phantômaxx Nov 05 '16 at 11:58
  • @Rotwang you dont need any second greyscaled versions of your bitmaps, just draw all child views in grayscale (no matter how many / what they are) – pskink Nov 05 '16 at 12:00
  • @pskink Then loose CPU time doing your drawings, if you prefer to. – Phantômaxx Nov 05 '16 at 12:02
  • @Rotwang did you observe dramatic loosing CPU time because of calling `Paint#setColorFilter` ? – pskink Nov 05 '16 at 12:04
  • @pskink Maybe not "dramatic" but far from being optimal. – Phantômaxx Nov 05 '16 at 12:05
  • @Rotwang ok do you know that every `Drawable` color tinting is made by calling `Paint#setColorFilter` ? this is used by every app that calls `Drawable#setTintList(ColorStateList tint)` and nobody suffers since everything is done in a hardware layer – pskink Nov 05 '16 at 12:10
  • @pskink but tinting is not swapping a drawable, is it? – Phantômaxx Nov 05 '16 at 14:22
  • @Rotwang i have no idea what `swapping` you are talking about, run [those](http://pastebin.com/YhWz00d2) 30 lines of code twice: with `GRAYSCALE` set to `true` and `false` and tell me where you see "far from being optimal.` performace loss – pskink Nov 05 '16 at 14:25
  • @pskink `swapping` means `changing one thing fo another one`. Drawing takes longer, I guess. Therefore, not an optimal way. – Phantômaxx Nov 05 '16 at 14:28
  • @Rotwang but what drawable do you want to change into another? what does it have to do with the greyscale painting? did you run the code i posted? – pskink Nov 05 '16 at 15:48
  • @pskink I'm not talking of painting. I'm talking of swapping. That's the difference: slower vs faster. – Phantômaxx Nov 05 '16 at 17:38
  • @Rotwang swapping? so who and where swaps what? the OP wants to paint the view and all its children in grayscale, not swap (whatever it means) anything, he wants to paint – pskink Nov 05 '16 at 17:50
  • @pskink Swapping is the way a StateLstDrawable works. Which is the better choice for the OP. Of course, you're free not to get the subtle difference and go on with painting. – Phantômaxx Nov 05 '16 at 18:17
  • @Rotwang and after replacing some Drawable into another the view doesn't need to be **redrawn** (repainted)? it automagically pops up on the screen without painting? – pskink Nov 05 '16 at 18:21
  • @pskink Well that's an internal gear. But I don't force a new one my own. – Phantômaxx Nov 05 '16 at 18:29
  • I can get the gray image of heart and star but on the left is the profile picture of the user, so i will get it from the webservice, so the issue is to convert it to gray. and the performance is not much of an issue here cause i have to create the view only once. so what do you suggest? – Parth Anjaria Nov 07 '16 at 05:08
  • make your custom `ViewGroup`, like [this](http://pastebin.com/YhWz00d2) – pskink Nov 07 '16 at 05:54
  • you can even create stuff like [that](http://pastebin.com/MJsg2bqH), click on the image to see it in action – pskink Nov 07 '16 at 10:10

0 Answers0