在我的应用程序中,我想通过自由手绘图在ImageView上选择一个区域。为此,我已将类扩展到ImageView,然后附加OntouchListener。当我运行应用程序时,ImageView上没有任何东西可以使用。任何人都可以找出问题所在。问候 代码:
public class MainActivity_ extends Activity {
ImageView im;
public static List<Point> points;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
im=(DrawView) findViewById(R.id.imageView1);
}
}
public class DrawView extends ImageView implements View.OnTouchListener{
public static List<Point> points;
Bitmap resultingImage;
private Paint paint;
int DIST = 2;
boolean flgPathDraw = true;
Point mfirstpoint = null;
boolean bfirstpoint = false;
Point mlastpoint = null;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.gallery_12);// convert image to bitmap
Context mContext;
public Bitmap get(){
return resultingImage;
}
public DrawView(Context c) {
super(c);
mContext = c;
setFocusable(true);
setFocusableInTouchMode(true);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setPathEffect(new DashPathEffect(new float[] { 10, 20 }, 0));
paint.setStrokeWidth(5);
paint.setColor(Color.WHITE);
this.setOnTouchListener(this);
points = new ArrayList<Point>();
bfirstpoint = false;
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
setFocusable(true);
setFocusableInTouchMode(true);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setPathEffect(new DashPathEffect(new float[] { 10, 20 }, 0));
paint.setStrokeWidth(15);
paint.setColor(Color.WHITE);
this.setOnTouchListener(this);
points = new ArrayList<Point>();
bfirstpoint = false;
}
public void onDraw(Canvas canvas) {
canvas.drawBitmap(bitmap, 0, 0, null);
Path path = new Path();
boolean first = true;
for (int i = 0; i < points.size(); i += 2) {
Point point = points.get(i);
if (first) {
first = false;
path.moveTo(point.x, point.y);
} else if (i < points.size() - 1) {
Point next = points.get(i + 1);
path.quadTo(point.x, point.y, next.x, next.y);
} else {
mlastpoint = points.get(i);
path.lineTo(point.x, point.y);
}
}
canvas.drawPath(path, paint);
}
public boolean onTouch(View view, MotionEvent event) {
/// if(event.getAction() != MotionEvent.ACTION_DOWN)
// return super.onTouchEvent(event);
//Toast.makeText(getApplicationContext(), "On touch is working",Toast.LENGTH_LONG).show();
Point point = new Point();
// point.x=(int) View.getLocationOnScreen
point.x = (int) event.getX();
point.y = (int) event.getY();
int[] pp={point.x,point.y};
view.getLocationOnScreen(pp);
view.getLocationOnScreen(pp);
point.x=pp[0];
point.y=pp[1];
if (flgPathDraw) {
if (bfirstpoint) {
if (comparepoint(mfirstpoint, point)) {
// points.add(point);
points.add(mfirstpoint);
flgPathDraw = false;
showcropdialog();
} else {
points.add(point);
}
} else {
points.add(point);
}
if (!(bfirstpoint)) {
mfirstpoint = point;
bfirstpoint = true;
}
}
invalidate();
Log.e("Hi ==>", "Size: " + point.x + " " + point.y);
if (event.getAction() == MotionEvent.ACTION_UP) {
Log.d("Action up*******~~~~~~~>>>>", "called");
mlastpoint = point;
if (flgPathDraw) {
if (points.size() > 12) {
if (!comparepoint(mfirstpoint, mlastpoint)) {
flgPathDraw = false;
points.add(mfirstpoint);
showcropdialog();
}
}
}
}
return true;
}
private boolean comparepoint(Point first, Point current) {
int left_range_x = (int) (current.x - 3);
int left_range_y = (int) (current.y - 3);
int right_range_x = (int) (current.x + 3);
int right_range_y = (int) (current.y + 3);
if ((left_range_x < first.x && first.x < right_range_x)
&& (left_range_y < first.y && first.y < right_range_y)) {
if (points.size() < 10) {
return false;
} else {
return true;
}
} else {
return false;
}
}
public void fillinPartofPath() {
Point point = new Point();
point.x = points.get(0).x;
point.y = points.get(0).y;
points.add(point);
invalidate();
}
public void resetView() {
points.clear();
paint.setColor(Color.WHITE);
paint.setStyle(Style.STROKE);
flgPathDraw = true;
invalidate();
}
private void showcropdialog() {
}
}
xml代码:
<com.example.crop4.DrawView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="46dp"
android:src="@drawable/gallery_12" />
答案 0 :(得分:0)
几乎没有问题。
首先,您根本不使用onCreate
。您可以在ImageView
中创建一个,但不要将其添加到视图树中。我想您希望将DrawView
替换为布局XML中的event.getX
。
然后,View.getLocationOnScreen
返回绝对位置(从屏幕的左边缘)但是要绘制,你需要相对位置(从画布的左边缘)。 Y轴也是如此。使用{{1}}获取屏幕上视图的位置。