将动态图像添加到编辑文本中

问题描述:

我现在正在自学android,并尝试使用Java做一个笔记应用程序。将动态图像添加到编辑文本中

我的问题是我不能将图像动态添加到编辑文本中。尽管我已经搜索了很多,但我仍然没有办法做到这一点。

请帮助我,至少关于这个想法。

下图说明了我正在努力解决的问题。

demo

+0

我没有得到这个问题。 –

+0

为什么要将图像添加到EditText中? 你想用短信和绘图功能创建一个笔记应用程序吗?你想在EditText上绘画? – FarshidABZ

+0

我正在做一个笔记应用程序的功能。每当我使用相机拍摄照片时,照片将被动态设置为编辑文本。希望你得到它。 –

我不认为这是可能的开箱即用。只需在EditText上添加ImageView即可。或者更好的是,使用EditText和LinearLayout(用于动态添加视图)将自定义视图绑定在一起。

像这样的东西(请记住这是伪代码):

活动:

public class MainActivity extends AppCompatActivity { 

CustomView customView; 

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

    customView = findViewById(R.id.custom_view); 

    someAction() 
} 

public void someAction() { 
    customView.addImage(...); 
} 

自定义视图

public class CustomView extends FrameLayout { 

private LinearLayout dynamicViewsParent; 

public CustomView(@NonNull Context context) { 
    super(context); 
    init(); 

} 

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

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

private void init() { 
    View inflatedView = View.inflate(getContext(), R.layout.custom_view, this); 
    dynamicViewsParent = inflatedView.findViewById(R.id.dynamicViewsWrapper); 
} 

public void addImage(SomeKindOfSource source) { 
    View view = createViewFrom(source); 
    dynamicViewsParent.addView(view); 
} 

}

和La YOUT为CustomView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/custom_view"> 

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

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TEXT"/> 

+0

这是作为答案发布的,但它不会试图回答这个问题。它应该可能是一个评论或完全删除。 –

+0

问题是我不知道每张纸条上会有多少张照片。因此,在xml中添加imageview以上的编辑文本将不是好主意。希望我的想法正确 –

+0

而不是一个imageview只使用linearlayout(上面的edittext)并动态添加子项。 – KrzysztofB

使用

setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)

editText.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.you_image,0,0); 

您可以在xml代码中添加ImageView以上的EditText

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

<ImageView 
    android:id="@+id/imageview" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:src="@mipmap/ic_launcher" /> 

<EditText 
    android:id="@+id/edittext" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="content" /> 
</LinearLayout> 
+0

'setCompoundDrawablesWithIntrinsicBounds'做什么?当我使用相机拍摄照片时,它将不在mipmap目录中。 –

+0

您可以在XML代码中的EditText上添加ImageView。 – KeLiuyue

+0

在xml中定义imageview将限制我可以使用相机拍摄的照片。例如,在你的xml代码中,我只能设置一次图像(根据我的理解)。我真正想要的是每次使用相机拍照时,我都可以添加照片来编辑文本 –

谢谢你的支持。感谢你们,我发现了动态添加图像的方式(不是编辑文本,对于我的误解抱歉)

这里是我需要它的人的源代码。我的代码是一团糟,但它的工作原理。

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Move to Camera" /> 
</LinearLayout> 

MainActivity.java

public class MainActivity extends AppCompatActivity { 
    LinearLayout layout; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button btnMove = (Button) findViewById(R.id.button); 
     layout = (LinearLayout) findViewById(R.id.activity_main); 

     btnMove.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(intent,113); 
      } 
     }); 

    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (data != null) { 
      if (requestCode == 113 && resultCode == RESULT_OK) { 
       Bitmap bitmap = (Bitmap) data.getExtras().get("data"); 
       LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
       layout.setLayoutParams(params); 
       setContentView(layout); 
       layout.setOrientation(LinearLayout.VERTICAL); 
       ImageView imgView = new ImageView(this); 
       imgView.setLayoutParams(params); 
       layout.addView(imgView); 
       int width = bitmap.getWidth(); 
       int height = bitmap.getHeight(); 
       Matrix matrix = new Matrix(); 
       matrix.postRotate(90); 
       Bitmap image = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); 
       imgView.setImageBitmap(image); 
       EditText edtText = new EditText(this); 
       edtText.setLayoutParams(params); 
       edtText.setBackground(null); 
       layout.addView(edtText); 
       Button btn = new Button(this); 
       LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
       btn.setLayoutParams(btnParams); 
       btn.setText("Move to Camera"); 
      } 
     } 
    } 

}