Diagonal line across view

余生颓废 提交于 2019-12-07 16:31:22

问题


Based on some condition, I have to diagonally cut the list cell. For this I have made diagonal drawable image using this code:

diagonal_line.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>

In list cell's xml it is used as:

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

This diagonal line appears in graphics view of cell's xml but not after list is inflated. For now its visbility is independent of any condition i.e. it is visibility is always true.

Any idea where the issue is?


回答1:


Custom view for diagonal line is:

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

}

It worked for me.



来源:https://stackoverflow.com/questions/26278084/diagonal-line-across-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!