对角线穿过视图

2024-04-08

根据某些条件,我必须对角剪切列表单元格。为此,我使用以下代码制作了对角线可绘制图像:

对角线.xml

 <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:top="0dp"
        android:bottom="0dp"
        >
        <rotate
            android:fromDegrees="315"
            android:toDegrees="315"
            android:pivotX="0%"
            android:pivotY="0%" >
            <shape
                android:shape="line"
                >
                <stroke
                    android:width="10dp"
                    android:color="@color/grey" />
            </shape>
        </rotate>
    </item>
    </layer-list>

在列表单元格的 xml 中,它用作:

 <ImageView 
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@drawable/diagonal_line"
    android:layerType="software"
    android:scaleType="fitXY"
    />

该对角线出现在单元格 xml 的图形视图中,但在列表膨胀后不会出现。目前,它的可见性与任何条件无关,即可见性始终为真。

知道问题出在哪里吗?


对角线的自定义视图是:

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class DiagonalLineView extends View {

private int dividerColor;
private Paint paint;

public DiagonalLineView(Context context)
{
    super(context);
    init(context);
}

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

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

private void init(Context context)
{
    Resources resources = context.getResources();
    dividerColor = resources.getColor(R.color.grey);

    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(dividerColor);
         paint.setStrokeWidth(resources.getDimension(R.dimen.vertical_divider_width));
}

@Override
protected void onDraw(Canvas canvas)
{
    super.onDraw(canvas);
    canvas.drawLine(0, getHeight(), getWidth(), 0, paint);
}

}

这对我有用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

对角线穿过视图 的相关文章

随机推荐