试过一个办法,但它看起来不像是一张卡片,它的曲线和卡片在背景中可以看到。在切割的左右边缘需要透明的东西。
参考了https://github.com/mreram/TicketView,但这并没有像卡片一样的效果,需要一个在中间左右切边的视图,看起来像一张卡片。关于自定义图像视图看起来像这样的任何想法?
试过一个办法,但它看起来不像是一张卡片,它的曲线和卡片在背景中可以看到。在切割的左右边缘需要透明的东西。
参考了https://github.com/mreram/TicketView,但这并没有像卡片一样的效果,需要一个在中间左右切边的视图,看起来像一张卡片。关于自定义图像视图看起来像这样的任何想法?
我希望这会对你有所帮助,我很久以前就创建了一个类,它扩展了 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>
为了在中心显示半圆,我创建了一个名为ticket_view.xml layer-list 的 XML,如下所示
<?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>
这样做的一个优点是您不需要任何库来创建它。
我希望这会对你有所帮助,快乐的编码