0

当用户触摸屏幕时,我有一个绘制圆圈的代码,它可以完美地工作,直到我添加一个 ImageResource,当我这样做时,圆圈不再被绘制。

我需要使用 ImageResource 因为圆圈将围绕此图像中的某些对象绘制,BackgroundResource 可能是一个不错的选择,但它会扭曲图像。

班上:

public class DragRectView extends TouchImageView {

    private Paint mRectPaint;

    private int mStartX = 0;
    private int mStartY = 0;
    private int mEndX = 0;
    private int mEndY = 0;
    private boolean mDrawCircle = false;
    private TextPaint mTextPaint = null;

    private OnUpCallback mCallback = null;

    public interface OnUpCallback {
        void onRectFinished(Rect rect);
    }

    public DragRectView(final Context context) {
        super(context);
        init();
    }

    public DragRectView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DragRectView(final Context context, final AttributeSet attrs,
                        final int defStyle) {
    super(context, attrs, defStyle);
    init();
}

/**
 * Sets callback for up
 *
 * @param callback {@link OnUpCallback}
 */
public void setOnUpCallback(OnUpCallback callback) {
    mCallback = callback;
}

/**
 * Inits internal data
 */
private void init() {
    mRectPaint = new Paint();
    mRectPaint.setColor(getContext().getResources().getColor(android.R.color.holo_green_light));
    mRectPaint.setStyle(Paint.Style.STROKE);
    mRectPaint.setStrokeWidth(5); // TODO: should take from resources

    mTextPaint = new TextPaint();
    mTextPaint.setColor(getContext().getResources().getColor(android.R.color.holo_green_light));
    mTextPaint.setTextSize(20);
}

@Override
public boolean onTouchEvent(final MotionEvent event) {

    // TODO: be aware of multi-touches
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mDrawCircle = false;
            mStartX = (int) event.getX();
            mStartY = (int) event.getY();
            invalidate();
            break;

        case MotionEvent.ACTION_MOVE:
            final int x = (int) event.getX();
            final int y = (int) event.getY();

            if (!mDrawCircle || Math.abs(x - mEndX) > 5 || Math.abs(y - mEndY) > 5) {
                mEndX = x;
                mEndY = y;
                invalidate();
            }

            mDrawCircle = true;
            break;

        case MotionEvent.ACTION_UP:
            if (mCallback != null) {
                mCallback.onRectFinished(new Rect(Math.min(mStartX, mEndX), Math.min(mStartY, mEndY),
                        Math.max(mEndX, mStartX), Math.max(mStartY, mEndY)));
            }
            invalidate();
            break;

        default:
            break;
    }

    return true;
}

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

    if (mDrawCircle) {
        canvas.drawCircle(mStartX,mStartY, (float) Math.sqrt(Math.pow(mEndX-mStartX,2)+Math.pow(mEndY-mStartY,2)),mRectPaint);
    }
}

我的主要活动是:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DragRectView dragRectView = findViewById(R.id.dragView);
    //dragRectView.setImageResource(R.drawable.img);
    dragRectView.setOnUpCallback(new DragRectView.OnUpCallback() {
        @Override
        public void onRectFinished(Rect rect) {
            System.out.println(rect);
        }
    });
}

有谁知道如何在带有图像集的视图中绘制一些东西?

4

1 回答 1

0

当您设置图像资源时,它会将其设置为您的 ImageView 的内容,您的 DragRectView 从 TouchImageView 扩展(从不清楚的地方扩展)。

您可以在视图的构造函数中使用 BitmapFactory 从图像资源中解码位图。

Bitmap imgRes = BitmapFactory.decodeResource(getResources(), R.drawable.img);

并像这样在 onDraw() 中绘制位图

canvas.drawBitmap(imgRes, xPosition, yPosition, null);
于 2019-10-08T22:16:25.577 回答