Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to mask a View in android?

Tags:

android

Is it possible to mask views? For example, if I have a design that calls for a List View to be visible within an oval shaped opening. Is there a way to create a mask for a view? Is it called something else? Because all the references I find to masking in the android docs are talking about masking a canvas object or drawable. But I don't think making a drawable of an interactive object like a list View would be a good approach. Is this just a limitation to deal with for now?

like image 670
Plastic Sturgeon Avatar asked Sep 26 '11 17:09

Plastic Sturgeon


People also ask

What is Android view view?

What is Android View? A View is a simple building block of a user interface. It is a small rectangular box that can be TextView, EditText, or even a button. It occupies the area on the screen in a rectangular area and is responsible for drawing and event handling.

How do I put Android in fullscreen mode?

Using Android Studio (current version is 2.2. 2 at moment) is very easy to add a fullscreen activity. See the steps: Right click on your java main package > Select “New” > Select “Activity” > Then, click on “Fullscreen Activity”.

What is masking in Android?

Masking refers to the process of putting something in place of something else. Therefore by Masking an EditText, the blank space is replaced with some default text, known as Mask. This mask gets removed as soon as the user enters any character as input, and reappears when the text has been removed from the EditText.


2 Answers

Yes, it is - you have to override the drawing method of your view - i.e:

...... final Path path = new Path(); path.addRoundRect(new RectF(0,0,getWidth(),getHeight()),10,10,Direction.CW); ...... @Override protected void dispatchDraw(Canvas canvas){     canvas.clipPath(path);     super.dispatchDraw(canvas); } 

this will draw your view only in the boundaries set by path.

like image 160
asenovm Avatar answered Sep 28 '22 08:09

asenovm


Yes, you can even mask complete layouts. Shameless selfplug

<com.christophesmet.android.views.maskableframelayout.MaskableFrameLayout android:id="@+id/frm_mask_animated" android:layout_width="100dp" app:porterduffxfermode="DST_IN" app:mask="@drawable/animation_mask" android:layout_height="100dp">  <ImageView android:layout_width="match_parent"            android:layout_height="match_parent"            android:scaleType="centerCrop"            android:src="@drawable/unicorn"/> 

You can find it here

like image 21
Christophe Smet Avatar answered Sep 28 '22 09:09

Christophe Smet