粘贴图像上的文本

时间:2018-06-13 04:49:37

标签: android textview imageview

我是Android开发的新手,我想知道如何从TextView获取文本并将该文本粘贴到图像并显示生成的图像。

例如,如果我在TextView中有一个文字,例如 你好,你好吗?我很好。好像你做得很好。 我希望将此文字粘贴到图片上。

想要得到这样的结果并显示在ImageViewenter image description here

我该怎么做?

2 个答案:

答案 0 :(得分:0)

首先在相对布局下放置图像视图。然后在该图像视图上使用TextView。

<?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="300dp"
        android:layout_height="300dp"
        android:src="@color/action_bar"
        android:layout_centerInParent="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Place Your Text Here"
        android:textColor="#fff"
        android:textSize="12sp"
        android:gravity="center"
        android:layout_centerInParent="true"/>


</RelativeLayout>

然后将文本复制并粘贴到图像视图上的TextView中

答案 1 :(得分:0)

使用Framelayout

<?xml version="1.0" encoding="utf-8"?>
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:id="@+id/iamge_text_panel"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:foreground="?attr/selectableItemBackgroundBorderless"
 android:gravity="center"
 android:orientation="vertical"
 android:padding="6dp">

<ImageView
    android:id="@+id/imageview"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:contentDescription="@string/image"
    android:src="@drawable/image_background" />

<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Drawing ,Text over images"
    android:textColor="@color/white" />
</FrameLayout>

创建自定义视图并覆盖画布

@SuppressLint("AppCompatCustomView")
public class CustomView extends ImageView {

public CustomView(Context context) {
    super(context);
}

public CustomView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    float left = getPaddingLeft();
    float top = getPaddingTop();
    float right = getPaddingLeft() + getWidth();
    float bottom = getPaddingTop() + getHeight();
    float centerX = (left + right) / 4f;
    float centerY = (top + bottom) / 2f;
    Paint pText = new Paint();
    pText.setColor(Color.RED);
    pText.setTextSize(50);
    canvas.drawText("Drawing Text over images", centerX, centerY, 
    pText);
    }
    }

在xml中添加此自定义视图

截图 enter image description here

相关问题