设置图像资源时无法在视图中拖动和绘制



我有一个代码,当用户触摸屏幕时绘制圆圈,它完美运行,直到我添加 ImageResource 时,当我这样做时,圆圈不再绘制。

我需要使用图像资源,因为圆圈将围绕此图像中的某些对象绘制,背景资源可能是一个很好的资源,但它会扭曲图像。

班级:

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

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

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

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

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

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

canvas.drawBitmap(imgRes, xPosition, yPosition, null);

相关内容

  • 没有找到相关文章

最新更新