触摸点上的图像上的文字

时间:2013-12-28 18:40:32

标签: android image touch

我需要在发生触摸事件的图像的X,Y坐标上应用一些文本。就像Facebook上的图像标记一样。因此,每次触摸发生时,用户都可以在触摸点的txtview中放置一些文本。

到目前为止,我所做的是我正在从图库加载图像并在图像查看器上显示它。

public class LoadImg extends Activity  {
private static int RESULT_LOAD_IMAGE = 1;
int x,y;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    x=0;y=0;
    String picturePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
    if(!picturePath.equals(""))
    {
       ImageView imageView = (ImageView) findViewById(R.id.imageView1);
       imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }

    Button buttonLoadImage = (Button) findViewById(R.id.button1);
    buttonLoadImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent i = new Intent(
                    Intent.ACTION_PICK,
                     android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent  data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null  != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        PreferenceManager.getDefaultSharedPreferences(this).edit().putString("picturePath", picturePath).commit();
        cursor.close();
        ImageView imageView = (ImageView) findViewById(R.id.imageView1);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

           }  }

同时实现OnTouchListener,我正在尝试此代码但无法正常工作。任何帮助。

public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub

    x=(int) event.getX();
    y=(int) event.getY();
    imageshow();
    return true;    
}
private void imageshow() {
    // TODO Auto-generated method stub
    if(x!=0 && y!=0)
    {
     TextView tv=new TextView(this);
     LinearLayout.LayoutParams trparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        tv.setLayoutParams(trparams);
    tv.layout(x, y, 0, 0);

    }
}

this

这样的东西

1 个答案:

答案 0 :(得分:0)

我从这个问题中理解的是,你有一个图像视图,我们在哪里触摸图像,你需要一个文本。
我使用FrameLayout完成了此操作。

首先,我将ImageView与FrameLayout包装为

<FrameLayout
    android:id="@+id/frameLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageView1"
    android:layout_marginTop="98dp" >

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="38dp"
        android:src="@drawable/old1" />

</FrameLayout>

这不是必须的,要成为FrameLayout。 只需用一些布局包装图片。

然后在MainActivity中,我完成了

public class MainActivity extends Activity {
FrameLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    layout=(FrameLayout) findViewById(R.id.frameLayout1);
    layout.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
             int x=(int) event.getX();
              int  y=(int) event.getY();
              Log.d("Nzm", "x="+x+"y="+y);
                FrameLayout mFrame=new FrameLayout(MainActivity.this);
                 TextView tv=new TextView(MainActivity.this);

                if(x!=0 && y!=0)
                {
                    FrameLayout.LayoutParams mParams=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                    mFrame.setLayoutParams(mParams);
                    mFrame.setPadding(x, y, 0, 0);
                    tv.setLayoutParams(mParams);
                    tv.setText("Tooltip");
                    mFrame.addView(tv);
                    layout.addView(mFrame);
                }
            return true;
        }
    });
}
}