我有這個圖標:最簡單的辦法把文本上繪製程序
我打算用它作爲工作drawable
。
Drawable myIcon = getResources().getDrawable(R.drawable.icon);
我需要programmaticaly把一些文本(文件擴展名)。
這是我想要的結果:。
我不能讓幾個靜態圖標,因爲我可以接收任意文件擴展名
我有這個圖標:最簡單的辦法把文本上繪製程序
我打算用它作爲工作drawable
。
Drawable myIcon = getResources().getDrawable(R.drawable.icon);
我需要programmaticaly把一些文本(文件擴展名)。
這是我想要的結果:。
我不能讓幾個靜態圖標,因爲我可以接收任意文件擴展名
public BitmapDrawable writeOnDrawable(int drawableId, String text){
Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);
Paint paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.BLACK);
paint.setTextSize(20);
Canvas canvas = new Canvas(bm);
canvas.drawText(text, 0, bm.getHeight()/2, paint);
return new BitmapDrawable(bm);
}
使用此method.will幫助你。
謝謝。它與我所需要的相似 –
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/folder"
android:src="@drawable/image_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textColor="@color/green"
android:textStyle="bold"
android:id="@+id/text"
android:text="10"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
如果您不打印此圖片的某個位置,而您只需將其顯示在屏幕上,最簡單的方法可能是在img
的中心打印text
。
您可以通過使用RelativeLayout
<RelativeLayout> // Only works inside a RelativeLayout
<ImageView
android:id="@+id/myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/myImageSouce" />
<TextView
android:id="@+id/myImageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/myImage"
android:layout_alignTop="@+id/myImage"
android:layout_alignRight="@+id/myImage"
android:layout_alignBottom="@+id/myImage"
android:layout_margin="1dp"
android:gravity="center"
android:text="Hello"
android:textColor="#000000" />
</RelativeLayout>
這可能是最簡單的解決方案,它會在圖像的中心,基於尺寸的img對齊文本做到這一點。
這很簡單,在'RelativeLayout'內部使用'ImageView'和'TextView'。將TextView的重力設置爲佈局的中心。 –
這個和其他使用RelativeLayout的答案不適合我,因爲我需要接收Drawable。而且我不想將Layout轉換爲Bitmap/Drawable –