Is it possible to mask a View in android?

前端 未结 2 769
醉酒成梦
醉酒成梦 2020-12-04 13:21

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 v

相关标签:
2条回答
  • 2020-12-04 14:09

    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 讨论(0)
  • 2020-12-04 14:26

    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.

    0 讨论(0)
提交回复
热议问题