简体   繁体   English

在imageview android上动态添加textview

[英]dynamically add textview on top of imageview android

I want to place textview on top of an imageview in android. 我想将textview放在android中的imageview之上。 Imageview is displaying a bitmap and I want to place textview on the top of imageview. Imageview正在显示位图,我想将textview放在imageview的顶部。 I also want that wherever the imageview is touched, textview must appear there.What is the best way to go about doing that. 我也希望无论在哪里触摸imageview,textview都必须出现在那里。最好的方法是做什么。 Thanks 谢谢

<LinearLayout
android:id="@+id/ll"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageView
    android:id="@+id/iv_image2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:scaleType="fitXY"
    android:adjustViewBounds="true"

    />



</LinearLayout> 

// my activity //我的活动

 switch (action) {
                case MotionEvent.ACTION_DOWN:

                        Bitmap.Config config = bm.getConfig();
                        int width = bm.getWidth();
                        int height = bm.getHeight();


                           bm2 = Bitmap.createBitmap(width, height, config);
                           c = new Canvas(bm2);

                           c.drawBitmap(bm, 0, 0, null);

                        iv2.setImageBitmap(bm2);
                    }


                    break;

You can do it from different approaches like this one, using RealtiveLayout: 你可以使用RealtiveLayout从不同的方法做到这一点:

First the XML: 首先是XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

Then the activity code: 然后是活动代码:

public class LogDetailActivity extends Activity {

    TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_detail);

        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        mTextView = (TextView) findViewById(R.id.textView);
        mTextView.setText("Example Text");

        mTextView.setVisibility(View.GONE);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTextView.setVisibility(View.VISIBLE);
            }
        });
    }
}

This should solve your second problem! 这应该解决你的第二个问题! I haven't tested this code, but it should work: 我没有测试过这段代码,但它应该可以工作:

Make sure you add implements View.OnTouchListener to your class declaration! 确保将implements View.OnTouchListener添加到类声明中!

imageView.setOnTouchListener(this);

// later on the in the code, add this method

public boolean onTouch(View v, MotionEvent event) {

    switch(v.getId()) {

        case R.id.imageViewId:

            float xPosition = event.getX();
            float yPosition = event.getY();

            textView.setX(xPosition);
            textView.setY(yPosition);

            break;

    }

}

Also, make the changes to the XML that 0mach0 suggested. 此外,对0mach0建议的XML进行更改。

  1. Use constraint layout and define your Imageview as per below. 使用约束布局并按照以下定义Imageview
   <ImageView
    android:id="@+id/arrangementImageView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
  1. You need to make imageView listen to onTouchListener 您需要让imageView监听onTouchListener
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_arrangement);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    panelLayout = (ConstraintLayout) findViewById(R.id.panelLayout);
    arrangementImageView = findViewById(R.id.arrangementImageView);

    arrangementImageView.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:

            int offsetX = 10; // Up to you to set
            int offsetY = 10;

            TextView myObj = new    TextView(v.getContext().getApplicationContext());
            myObj.setCompoundDrawablesWithIntrinsicBounds(R.drawable.myImage, 0, 0, 0);
            myObj.setTextViewId(1001);
            panelLayout.addView(myObj, -1);

            ConstraintSet set = new ConstraintSet();

            set.clone(panelLayout);
            set.connect(myObj.getId(), ConstraintSet.TOP, panelLayout.getId(), ConstraintSet.TOP, (int) event.getRawY() - offsetY);
            set.connect(myObj.getId(), ConstraintSet.LEFT, panelLayout.getId(), ConstraintSet.LEFT, (int) event.getRawX() - offsetX);
            set.applyTo(panelLayout);

            break;
        }

        return false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM