2011-03-09 61 views
78

我有一個imageView圖像,在圖像上我想放置文本。我怎樣才能做到這一點?Android圖像上的文字

+1

簡單的方法是拿一個textView並且像你一樣設置背景在ImageView中。它會讓你的工作變得簡單.. – Nepster 2014-05-23 12:08:05

+0

只看下面的鏈接。 我希望這將有助於ü... https://stackoverflow.com/questions/11100428/add-text-to-image-in-android-programmatically/26064945#26064945 – 2014-09-26 17:19:14

回答

5

有很多方法。你使用RelativeLayout或AbsoluteLayout。例如

使用親屬,例如,您可以讓圖像與父母的左側對齊,並且還將文本與父母左對齊......然後,您可以在文本視圖上使用邊距和填充以及引力來獲取它排列在你想要的圖像上。

+1

我來到這個方法意外地。但是當你想在imagebutton或imageview上放置一些文本時,它非常方便。示例 - 將當前溫度放在當前條件的圖像上。 – Phobos 2011-03-09 08:26:09

5

你可能

  • 使從類的ImageView和
  • 覆蓋的方法onDraw繼承一個新的類。先在該方法中調用super.onDraw(),然後
  • 然後繪製一些要顯示的文本。

如果你這樣做,你可以使用它作爲一個佈局組件,這使得它更容易與其他組件一起佈局。

2

您可以使用一個TextView並改變其背景,要使用

+0

這個答案的問題是你不能控制像imageView這樣的背景屬性。比例尺。 – 2013-09-08 12:25:39

131

這就是我做的形象和它的工作完全一樣,你問一個RelativeLayout的內部:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativelayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ImageView 
     android:id="@+id/myImageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/myImageSouce" /> 

    <TextView 
     android:id="@+id/myImageViewText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@id/myImageView" 
     android:layout_alignTop="@id/myImageView" 
     android:layout_alignRight="@id/myImageView" 
     android:layout_alignBottom="@id/myImageView" 
     android:layout_margin="1dp" 
     android:gravity="center" 
     android:text="Hello" 
     android:textColor="#000000" /> 

</RelativeLayout> 
+0

非常感謝這個例子。我試圖使用compoundedDrawable textview,但我不能設置圖像url。但是,在相對佈局中使用此變通辦法來識別layout_align設置很重要。 – AntonSack 2013-10-05 11:17:54

+0

把控制的id到對齊解決了很多,謝謝 – 2017-10-15 00:25:44

14

你可能需要從不同勢側收取,如果:它似乎更容易有一個TextView與在背景上繪製:

<TextView 
      android:id="@+id/text" 
      android:background="@drawable/rounded_rectangle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
     </TextView> 
+9

這個答案的問題是你不能控制像imageView這樣的背景屬性。比例尺。 – 2013-09-08 12:28:39

2

爲此,您可以只用一個TextView的與android:drawableLeft/Right/Top/Bottom將圖像定位到TextView。此外,您還可以使用的TextView和繪製之間的一些填充有android:drawablePadding=""

使用方法如下:

<TextView 
    android:id="@+id/textAndImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    android:drawableBottom="@drawable/yourDrawable" 
    android:drawablePadding="10dp" 
    android:text="Look at the drawable below"/> 

有了這個,你不需要額外的ImageView的。也可以在TextView的多個邊上使用兩個drawable。

您將面臨的唯一問題是,drawable不能縮放ImageView的方式。

1

嘗試下面的代碼,這將有助於you`

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="150dp"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:src="@drawable/gallery1"/> 


    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:background="#7ad7d7d7" 
     android:gravity="center" 
     android:text="Juneja Art Gallery" 
     android:textColor="#000000" 
     android:textSize="15sp"/> 
</RelativeLayout> 
-1

下面的代碼,這將幫助你

public class TextProperty { 
private int heigt;        //讀入文本的行數 
private String []context = new String[1024]; //存儲讀入的文本 

/* 
*@parameter wordNum 
* 
*/ 
public TextProperty(int wordNum ,InputStreamReader in) throws Exception { 
    int i=0; 
    BufferedReader br = new BufferedReader(in); 
    String s; 
    while((s=br.readLine())!=null){ 
     if(s.length()>wordNum){ 
      int k=0; 
      while(k+wordNum<=s.length()){ 
       context[i++] = s.substring(k, k+wordNum); 
       k=k+wordNum; 
      } 
      context[i++] = s.substring(k,s.length()); 
     } 
     else{ 
      context[i++]=s; 
     } 
    } 
    this.heigt = i; 
    in.close(); 
    br.close(); 
} 


public int getHeigt() { 
    return heigt; 
} 

public String[] getContext() { 

    return context; 
} 

}

public class MainActivity extends AppCompatActivity { 

private Button btn; 
private ImageView iv; 
private final int WORDNUM = 35; //轉化成圖片時 每行顯示的字數 
private final int WIDTH = 450; //設置圖片的寬度 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    iv = (ImageView) findViewById(R.id.imageView); 
    btn = (Button) findViewById(R.id.button); 

    btn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      int x=5,y=10; 
      try { 
       TextProperty tp = new TextProperty(WORDNUM, new InputStreamReader(getResources().getAssets().open("1.txt"))); 
       Bitmap bitmap = Bitmap.createBitmap(WIDTH, 20*tp.getHeigt(), Bitmap.Config.ARGB_8888); 
       Canvas canvas = new Canvas(bitmap); 
       Paint paint = new Paint(); 
       paint.setColor(Color.WHITE); 
       paint.setTextAlign(Paint.Align.LEFT); 
       paint.setTextSize(20f); 

       String [] ss = tp.getContext(); 
       for(int i=0;i<tp.getHeigt();i++){ 
        canvas.drawText(ss[i], x, y, paint); 
        y=y+20; 
       } 

       canvas.save(Canvas.ALL_SAVE_FLAG); 
       canvas.restore(); 
       String path = Environment.getExternalStorageDirectory() + "/image.png"; 
       System.out.println(path); 
       FileOutputStream os = new FileOutputStream(new File(path)); 
       bitmap.compress(Bitmap.CompressFormat.PNG, 100, os); 
       //Display the image on ImageView. 
       iv.setImageBitmap(bitmap); 
       iv.setBackgroundColor(Color.BLUE); 
       os.flush(); 
       os.close(); 
      } 
      catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 


     } 
    }); 
} 

}