温馨提示:本文翻译自stackoverflow.com,查看原文请点击:android - Custom Image View Cut Left and Right Edges Semi Circle
android android-custom-view imageview view

android - 自定义图像视图剪切左右边缘半圆

发布于 2020-05-03 05:22:58

在开发自定义视图时需要帮助。这是我需要发展的一种观点: 在此处输入图片说明

已经尝试了一种方法,但是它看起来并不像卡片,它的曲线和卡片可以在后台看到。在左右切边需要透明的东西。

引用了https://github.com/mreram/TicketView,但这不会产生类似于卡片的效果,需要一个在中间左右两侧具有切边的视图,看起来像卡片。关于自定义图像视图的想法如何?

查看更多

提问者
Reejesh PK
被浏览
7
Rakshit Nawani 2020-02-19 14:27

希望对您有所帮助,我确实创建了一个很久的类,在此之前扩展了FrameLayout可以为您完成工作。

这是输出的样子

结果图像

我是通过以下代码实现的。

MyRoundCornerFrame.java

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.FrameLayout;

public class MyRoundCornerFrame extends FrameLayout {
    private final static float CORNER_RADIUS = 30.0f; //card radius chnage accordingly
    private float cornerRadius;

    public MyRoundCornerFrame(Context context) {
        super(context);
        init(context, null, 0);
    }

    public MyRoundCornerFrame(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public MyRoundCornerFrame(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS, metrics);
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        int count = canvas.save();

        final Path path = new Path();
        path.addRoundRect(new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), cornerRadius, cornerRadius, Path.Direction.CW);
        canvas.clipPath(path, Region.Op.INTERSECT);

        canvas.clipPath(path);
        super.dispatchDraw(canvas);
        canvas.restoreToCount(count);
    }
}

我的XML文件如下所示

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:paddingTop="30dp">

    <your.package.name.MyRoundCornerFrame
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_margin="20dp">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@drawable/food"
            android:padding="20dp" />

    </your.package.name.MyRoundCornerFrame>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:background="@drawable/ticket_view"
        android:padding="10dp"
        tools:ignore="ContentDescription" />


</FrameLayout>

为了在中心显示半圆,我创建了一个XML,命名为ticket_view.xml layer-list,如下所示

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="5dp" />
        </shape>
    </item>

    <item
        android:width="50dp"
        android:height="50dp"
        android:gravity="start|center">
        <shape android:shape="oval">
            <solid android:color="@android:color/white" />

        </shape>
    </item>

    <item
        android:width="50dp"
        android:height="50dp"
        android:gravity="end|center">
        <shape android:shape="oval">
            <solid android:color="@android:color/white" />

        </shape>
    </item>


</layer-list>

这样做的一个优点是您不需要任何库即可创建此库。

希望这对您有所帮助,编码愉快