【问题标题】:Dynamically generated line with glow effective具有发光效果的动态生成线条
【发布时间】:2011-09-11 00:38:21
【问题描述】:

我想用这样的发光效果画线

问题 - 我必须根据用户的交互在程序中生成这一行(行的形式将在onTouchEvent - ACTION_MOVE 中生成)。

我可以在没有 xml 文件或绘制 premaid 位图的情况下生成这种效果吗?

【问题讨论】:

    标签: android paint touch-event


    【解决方案1】:

    我是这样模仿这个效果的:

    1. BlurMaskFilter画线;
    2. 在其上画法线。

    我使用 Path 类生成直线并保存 MOVE_ACTION 事件的坐标,以仅生成我需要的部分路径。

    创建 2 个Paint()s:

    _paintSimple = new Paint();
    _paintSimple.setAntiAlias(true);
    _paintSimple.setDither(true);
    _paintSimple.setColor(Color.argb(248, 255, 255, 255));
    _paintSimple.setStrokeWidth(20f);
    _paintSimple.setStyle(Paint.Style.STROKE);
    _paintSimple.setStrokeJoin(Paint.Join.ROUND);
    _paintSimple.setStrokeCap(Paint.Cap.ROUND);
    
    _paintBlur = new Paint();
    _paintBlur.set(_paintSimple);
    _paintBlur.setColor(Color.argb(235, 74, 138, 255));
    _paintBlur.setStrokeWidth(30f);
    _paintBlur.setMaskFilter(new BlurMaskFilter(15, BlurMaskFilter.Blur.NORMAL)); 
    

    然后画两次我的Path():

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawPath(mPath, _paintBlur);
        canvas.drawPath(mPath, _paintSimple);
    }
    

    【讨论】:

    • 什么是mPath ??我做了新的var。同名路径 mPath = new Path();没有变化!
    • mPath 将是您用来构建要绘制的线的路径对象。您可以将点添加到路径(用户触摸过的点),然后绘制路径。
    • 这仅在禁用硬件加速时有效,请在您的视图上使用setLayerType(View.LAYER_TYPE_SOFTWARE, null) 来执行此操作。
    • 正如@Tom 所说,必须设置layerType。这也可以通过在 XML 中设置 android:layerType="software" 来实现。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多