2017-10-14 18 views
-3

저는 이제 Android를 자습하고 Java를 사용하여 메모 응용 프로그램을 작성하려고합니다.이미지를 동적으로 텍스트 편집에 넣기

제 문제는 이미지를 편집 텍스트에 동적으로 추가 할 수 없다는 것입니다. 비록 내가 그것에 대해 많이 연구했지만, 나는 여전히 그것을 만들 방법이 없다.

아이디어에 관해서는 저를 도우십시오.

다음 그림은 내가 어려움을 겪고있는 문제를 보여줍니다.

demo

+0

나는 문제를하지 않았다. –

+0

EditText에 이미지를 추가하려는 이유가 무엇입니까? 문자 및 그림 기능이있는 메모 응용 프로그램을 만들시겠습니까? EditText에 페인트하고 싶습니까? – FarshidABZ

+0

메모 응용 프로그램의 기능을 수행하고 있습니다.카메라를 사용하여 사진을 찍을 때마다 사진이 동적으로 편집 텍스트로 설정됩니다. 당신이 그것을 얻을 수 있기를 바랍니다. –

답변

-1

가 나는 상자에서 가능하다고 생각하지 않습니다. 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); 
} 

}

및 라 이 행해져 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 대신에 (예를 들어 위에 edittext) linearlayout을 사용하고 아이들을 동적으로 추가하십시오. – KrzysztofB

0

사용

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

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

또는

xxx 코드의 EditText 위에 ImageView을 추가 할 수 있습니다.

<?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'는 무엇을합니까? 카메라를 사용하여 사진을 촬영하면 밉맵 디렉토리에 표시되지 않습니다. –

+0

XML 코드의 EditText 위에 ImageView를 추가 할 수 있습니다. – KeLiuyue

+0

xml의 이미지 뷰를 정의하면 카메라를 사용하여 찍을 수있는 사진이 축소됩니다. 예를 들어, XML 코드에서 한 번만 이미지를 설정할 수있었습니다 (필자의 이해에 따라). 내가 정말로 원하는 것은 카메라를 사용하여 사진을 찍을 때마다 텍스트를 편집하기 위해 사진을 추가 할 수 있다는 것입니다. –

0

지원해 주셔서 감사합니다. 너희들을 위해 고마워, 나는 이미지를 동적으로 추가하는 방법을 발견했다. (편집 텍스트가 아니라, 오해에 대해 미안하다.)

여기 내 소스 코드는 그것을 필요로하는 사람을위한 것이다. 내 코드는 엉망이지만 작동합니다.

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

} 
관련 문제