开发者

Is it possible to mask a View in android?

开发者 https://www.devze.com 2023-04-07 11:50 出处:网络
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

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. 开发者_如何学JAVABut 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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消