【问题标题】:Drawing multiple lines and remembering their thickness绘制多条线并记住它们的粗细
【发布时间】:2013-05-03 08:13:53
【问题描述】:

这可能是因为我度过了漫长的一天,而且已经很晚了,但我似乎无法弄清楚我做错了什么。

我只想能够在我手指的位置画线,每条线具有不同的宽度/颜色等。

每当 ACTION_DOWN 被触发时,我都会创建一个新的 Path 并继续将其附加到 ACTION_MOVE。当 ACTION_UP 被解雇时,我将当前的 PathPaint 放在我的 HashMap 中,从而节省了哪个 Path 使用了哪个 Paint,对吗?

当我从这个类之外调用setRadius(float radius) 时,我调用paint.setStrokeWidth(radius),从而改变了当前Paint 的笔画宽度。

但出于某种原因,每次我打电话给setStrokeWidth(radius) ALL 我的 Map.Entry 的变化?从而导致 Every Paint 的笔画宽度成为“新”笔画宽度,并使用新的笔画宽度重新绘制所有内容。

这可能很明显,但我似乎找不到错误。

这是我的 DrawView.java。

package com.example.paintandprint;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class DrawView extends View implements OnTouchListener {
    Path path = new Path();
    Paint paint = new Paint();

    Map<Path, Paint> pathMap = new HashMap<Path, Paint>();

    public DrawView(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);

        this.setOnTouchListener(this);

        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
    }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawPath(path, paint);

        for (Map.Entry<Path, Paint> p : pathMap.entrySet()) {
            canvas.drawPath(p.getKey(), p.getValue());
        }

    }

    public boolean onTouch(View view, MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            path = new Path();
            path.reset();
            path.moveTo(eventX, eventY);
            return true;
        case MotionEvent.ACTION_MOVE:
            path.lineTo(eventX, eventY);
            break;
        case MotionEvent.ACTION_UP:
            pathMap.put(path, paint);
            break;
        default:
            return false;
        }

        invalidate();
        return true;
    }

    public float getRadius() {
        return paint.getStrokeWidth();
    }

    public void setRadius(float radius) {
        paint.setStrokeWidth(radius);
    }
}

提前致谢!

【问题讨论】:

    标签: android path drawing hashmap paint


    【解决方案1】:

    在这些行中:

    case MotionEvent.ACTION_UP:
        pathMap.put(path, paint);
        break;
    

    你在“油漆”中发送什么???它不是指向填充了预设值的结构的指针吗?不是每个 pathMap.put 都发送指向相同数据的相同指针吗?这就是为什么如果你改变一次,你就会到处改变。

    您需要为您创建的每个 pathMap.put 创建一个新的 Paint,并将类的当前值复制到其中。

    case MotionEvent.ACTION_UP:
        Paint newPaint = new Paint();
        newPaint.set(paint); //copies the values over from the current class's paint
        pathMap.put(path, newPaint);
        break;
    

    【讨论】:

    • 非常感谢 - 我花了很长时间试图弄清楚如何更改 Paint 对象的宽度。
    猜你喜欢
    • 2015-12-12
    • 1970-01-01
    • 2016-02-16
    • 2018-10-13
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多