我有一个在用户触摸屏幕时画圆的代码,它可以完美工作,直到我添加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);
}
});
}
有人知道如何在带有图像集的视图中绘制某些东西吗?
[当您设置图像资源然后将其设置为ImageView的内容时,您的DragRectView从TouchImageView扩展(从不清楚的地方扩展。)
您可以在视图的构造函数中使用BitmapFactory从图像资源中解码位图。
Bitmap imgRes = BitmapFactory.decodeResource(getResources(), R.drawable.img);
并像这样在onDraw()中绘制位图
canvas.drawBitmap(imgRes, xPosition, yPosition, null);