OGeek|极客世界-中国程序员成长平台

标题: android,如何在edittext中绘制虚线 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-9 06:17
标题: android,如何在edittext中绘制虚线

我引用了这个链接:How do I make a dotted/dashed line in Android? ,并使用了 DashPathEffect。但这对我不起作用?为什么?我的代码:

public class NoteEditText extends EditText {
    private Paint mPaint;

    public NoteEditText(Context context) {
        super(context);
    }

    public NoteEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setStrokeWidth(1);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setColor(Color.DKGRAY);
        PathEffect effects = new DashPathEffect(new float[]{5,5,5,5},1);  
        mPaint.setPathEffect(effects);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int height = this.getHeight();
        int lineHeight = this.getLineHeight();
        int lineNum = height / lineHeight;
        L.l("line count: " + lineNum);
        for (int i = 0; i < lineNum; i++) {
            int y = (i + 1) * lineHeight;
            canvas.drawLine(0, y, this.getWidth() - 1, y, mPaint);
        }
    }
}



Best Answer-推荐答案


硬件加速不支持setPathEffect方法。默认情况下它是打开的(我认为是从 Android 4.0 开始)

http://developer.android.com/guide/topics/graphics/hardware-accel.html#unsupported

您可以使用以下代码 fragment 在构造函数中关闭硬件加速:

setLayerType(View.LAYER_TYPE_SOFTWARE, null);

关于android,如何在edittext中绘制虚线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12401311/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://www.ogeek.cn/) Powered by Discuz! X3.4