我正在使用自定义imageview绘制一个矩形,并在onDraw中绘制recct。代码在这里
public class ExpandableImageView extends ImageView {
int l = 100, t = 70, r = 150, b = 180;
int count=0;
int h, w;
Rect rect;
Paint paint;
Canvas c;
public ExpandableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public ExpandableImageView(Context context) {
super(context);
}
public ExpandableImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Log.i(VIEW_LOG_TAG, "Inside onDraw");
c = canvas;
paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(3);
paint.setStyle(Style.STROKE);
rect = new Rect(l, t, r, b);
h = rect.height();
w = rect.width();
if (count == 0) {
canvas.drawRect(rect, paint);
count++;
}
}
public int getl() {
return l;
}
public int gett() {
return t;
}
public int getR() {
return r;
}
public int getB() {
return b;
}
public void doTouchEvent(MotionEvent e) {
l = (int) e.getX();
t = (int) e.getY();
Log.i(VIEW_LOG_TAG, "e.getX()" + e.getX());
Log.i(VIEW_LOG_TAG, "e.getY()" + e.getY());
Log.i(VIEW_LOG_TAG, "rect" + r);
}
}
现在,当我尝试在触摸事件上再次重绘rectview时,它无法正常工作 这是我主要活动的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
imageView = new ExpandableImageView(this);
setContentView(R.layout.cropimage);
paint = new Paint();
bobj = new BaldBooth1();
bm = BaldBooth1.bMap;
d = new BitmapDrawable(bm);
iv = ((ImageView) findViewById(R.id.image));
iv.setImageDrawable(d);
((ImageButton) findViewById(R.id.next)).setOnClickListener(this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
final int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
Log.i("", "Action Down");
imageView.doTouchEvent(event);
break;
}
case MotionEvent.ACTION_MOVE: {
Log.i("", "Action move");
Log.i("abc", "Top"+imageView.gett());
Log.i("abc", "Left"+imageView.getl());
iv.invalidate();
break;
}
}
return true;
}
}