【问题标题】:Text on image at the point of touch触摸点图像上的文本
【发布时间】:2013-12-28 18:40:31
【问题描述】:

我需要在发生触摸事件的图像的 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

【问题讨论】:

    标签: android image touch


    【解决方案1】:

    我从这个问题中了解到,您有一个图像视图,并且无论我们在哪里触及图像,您都需要一个文本。
    我使用FrameLayout 完成了这项工作。

    首先,我用 FrameLayout 将 ImageView 包装为

    <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;
            }
        });
    }
    }
    

    【讨论】:

    • Thnx .. 但这样每次触摸都会出现静态文本(工具提示)。我希望用户可以在每次触摸时添加不同的文本。这可能吗??
    • 代替TextView,您可以根据需要创建任何视图。
    • 这对于当前的问题来说太长而且不合适。最好将其作为另一个问题提出。另外,如果有用,请不要忘记标记此答案:)
    • 你做到了 :) 另外,考虑在你的旧问题上做标记。
    • 此链接的已接受答案工作正常stackoverflow.com/questions/36812567/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多