在imageview android

时间:2015-04-27 16:38:46

标签: android android-layout

我想将textview放在android中的imageview之上。 Imageview正在显示位图,我想将textview放在imageview的顶部。 我也希望无论在哪里触摸imageview,textview都必须出现在那里。最好的方法是做什么。感谢

<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> 

//我的活动

 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;

3 个答案:

答案 0 :(得分:2)

你可以使用RealtiveLayout从这个不同的方法做到这一点:

首先是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>

然后是活动代码:

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

答案 1 :(得分:1)

这应该可以解决你的第二个问题!我没有测试过这段代码,但它应该可以工作:

确保将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;

    }

}

另外,对0mach0建议的XML进行更改。

答案 2 :(得分:0)

  1. 使用约束布局并按照以下定义您的Imageview
   <ImageView
    android:id="@+id/arrangementImageView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
  1. 您需要使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;
    }
}