drag n drop textview in android

后端 未结 1 1777
青春惊慌失措
青春惊慌失措 2021-02-02 01:40

I want to drag n drop the text view on image in android 2.X

please , look at this image below. \"alt

1条回答
  •  南笙
    南笙 (楼主)
    2021-02-02 02:35

    Instead, try getting your ImageView as a canvas to draw into.

    ImageView CanvasView = (ImageView) findViewById(R.id.fashion_pic)

    From here you can create your own Bitmap and re-draw it after a TouchEvent. The following snippet contains a necessary work-around for a bug in 2.1 (or 2.2, I can't remember):

    public void redrawImage() {
    
        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.fashion_pic);
        Bitmap proxy = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Config.ARGB_8888);
        Canvas c = new Canvas(proxy);
    
        //Here, we draw the background image.
        c.drawBitmap(bm, new Matrix(), null);
    
        //Here, we draw the text where the user last touched.
        c.drawText("Fashion", someGlobalXvariable, someGlobalYvariable, somePaint);
    
        CanvasView.setImageBitmap(proxy);
    }
    

    Here, you have created a canvas which already has your picture as the background and paints text at an x and y variable. Now, just set an OnTouchListener:

    CanvasView.setOnTouchListener( new OnTouchListener(){
    
        public boolean onTouch(View v, MotionEvent e) {
    
            someGlobalXvariable = e.getX();
            someGlobalYvariable = e.getY();
            redrawImage();
            return true;
        }
    });
    

    As you can see, the listener automatically updates your image when it is touched, and the text will follow the user's finger. Since you're using a Bitmap as your canvas, you can also add support for saving/loading an image if your app wants to have that kind of functionality.

    Edit: It may be more performance-friendly if you move lines 1-3 from redrawImage() and place them elsewhere, setting those objects as global objects. I'll let you tinker with that.

    0 讨论(0)
提交回复
热议问题