在现有imageView上添加重叠图像

时间:2014-02-09 16:27:14

标签: android image onclick overlay

插入重叠图像时出现问题。 这是我的代码。 map 是我在Android的图库中浏览的图片。我想要做的是在我点击(x1,y1)或<时,将另一张 res / drawable / pin1.png 图像添加到现有地图上对话框中的em>(x2,y2)。当图片移动时,引脚必须移动。

    private ImageView map = (ImageView) findViewById(R.id.image_map);
    map.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(imagePath != null) {
                x = (int) event.getX();
                y = (int) event.getY();

                final String[] items = { "(x1,y1)", "(x2,y2)" };
                AlertDialog.Builder builder = new AlertDialog.Builder(MapUploadActivity.this);
                builder.setTitle("Select Position for " + "(" + x + "," + y + ")")
                       .setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        if (items[item] == "(x1,y1)") {
                            x1 = x;
                            y1 = y;
                            show_x1y1.setText("(x1,y1) = (" + x1 + "," + y1 + ")");
                        } else if (items[item] == "(x2,y2)") {
                            x2 = x;
                            y2 = y;
                            show_x2y2.setText("(x2,y2) = (" + x2 + "," + y2 + ")");
                        }
                    }
                });

                AlertDialog alert = builder.create();
                alert.show();

我有一个插图了解更多细节。 http://i1017.photobucket.com/albums/af293/palm_kawin91/Untitled-1-1.jpg

我想知道是否有人可以帮助我。我搜索过其他论坛,但他们并不满足我的愿望。 T_ _ _T

1 个答案:

答案 0 :(得分:0)

此示例代码可能会给您一些想法......

public class DrawView extends ImageView {

    private ArrayList<Point> points;
    private Paint paint;

    public DrawView(Context context) {
        super(context);
        init();
    }

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

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

    private void init() {
        points = new ArrayList<Point>();
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(!points.isEmpty()) {
            Iterator<Point> iterator = points.iterator();
            while(iterator.hasNext()) {
                Point point = iterator.next();
                canvas.drawCircle(point.x, point.y, 20, paint);;
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        if(event.getAction() == MotionEvent.ACTION_UP) {
            points.add(new Point((int)event.getX(), (int)event.getY()));
            invalidate();
        }
        return true;
    }

}