其他分享
首页 > 其他分享> > android-视图中的对角线

android-视图中的对角线

作者:互联网

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

对角线

 <?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);
}

}

它为我工作.

标签:diagonal,drawable,layerdrawable,xml,android
来源: https://codeday.me/bug/20191029/1957182.html