以编程方式将文本放在可绘制对象上的最简单方法
Simplest way to put text on drawable programmatically
我有这个图标:
我将以 drawable
的身份使用它。
Drawable myIcon = getResources().getDrawable( R.drawable.icon );
我需要以编程方式在上面添加一些文本(文件扩展名)。
这是我想要的结果:。
我无法制作多个静态图标,因为我可以接收任意文件扩展名
<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>
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帮你。
如果您不打算在某处打印此图像而只需将其显示在屏幕上,可能最简单的方法是在 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 大小将文本对齐到图像的中心。
我有这个图标:
我将以 drawable
的身份使用它。
Drawable myIcon = getResources().getDrawable( R.drawable.icon );
我需要以编程方式在上面添加一些文本(文件扩展名)。
这是我想要的结果:
我无法制作多个静态图标,因为我可以接收任意文件扩展名
<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>
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帮你。
如果您不打算在某处打印此图像而只需将其显示在屏幕上,可能最简单的方法是在 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 大小将文本对齐到图像的中心。