拖N的安卓下降的TextView [英] drag n drop textview in android

查看:152
本文介绍了拖N的安卓下降的TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拖ñ下降的图像文本视图中的Andr​​oid 2.X

请,看看下面这个图片。

下面,蓝色重presents的ViewGrop,白是的ImageView和图像设置在ImageView的。 文写着时尚是一个TextView.This是我怎么也实现结构。现在我想允许用户选择的TextView并将其拖动到任何部分的图像,然后将其放在图像在需要的位置。

截至目前的参考,我试图参照下面的网址,但每当我使用的按钮TextView的事情,而不是有越来越变态。

链接文本>

谁能给我的路线图和例子来完成它?

解决方案

相反,尝试让你的ImageView作为画布绘制成。

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

从这里,你可以创建自己的位图和的TouchEvent后重新绘制。下面的代码片段包含了一个必要的变通为2.1(或2.2,我记不清了)错误:

 公共无效redrawImage(){

    位图BM = BitmapFactory.de codeResource(getResources(),R.drawable.fashion_pic);
    位图代理= Bitmap.createBitmap(bm.getWidth(),bm.getHeight(),Config.ARGB_8888);
    帆布C =新的Canvas(代理);

    //这里,我们绘制背景图像。
    c.drawBitmap(BM,新的Matrix(),NULL);

    //这里,我们绘制了用户最后感动的文字。
    c.drawText(时尚,someGlobalXvariable,someGlobalYvariable,somePaint);

    CanvasView.setImageBitmap(代理);
}
 

在这里,你已经创建已经有你的照片作为背景,并以X和Y变量绘制文本的画布。现在,只需设置一个OnTouchListener:

  CanvasView.setOnTouchListener(新OnTouchListener(){

    公共布尔onTouch(视图V,MotionEvent E){

        someGlobalXvariable = e.getX();
        someGlobalYvariable = e.getY();
        redrawImage();
        返回true;
    }
});
 

正如你所看到的,听者会自动更新您的图片时,它被触及,该文本将遵循用户的手指。由于您使用位图作为你的画布,你还可以添加保存/如果你的应用程序要具有这种功能加载图像的支持。

编辑:如果您移动从 redrawImage线1-3()和其他地方安置他们,这些设置可能更多的表现友好对象为全局对象。我让你鼓捣了。

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

please , look at this image below.

Here, "Blue" color represents the ViewGrop , "White" is ImageView and the image is set on the ImageView. the text written "Fashion" is a TextView.This is how i have implement the structure. Now i want to allow the user to select the TextView and drag it to any part of the image and then drop it over the image at desired position.

As of now for a reference i tried to refer the following url but whenever i use TextView instead of Button things are getting abnormal.

link text

Can anyone give me the road map or example to get it done ?

解决方案

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.

这篇关于拖N的安卓下降的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆