android - 在图像上写文字的最佳方式?

标签 android imageview android-canvas

我想拍一张小日历图片并在上面写上日期和月份。这将在 ListView 中多次显示,因此我正在寻找最佳方法。

下面很好用,有没有更好的办法:

ImageView image = (ImageView) findViewById(R.id.imageView2);
try {
    // Load the png image into a bitmap
    Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.calendar_empty);
    // Have to copy the bitmap so it is mutable
    mBitmap = mBitmap.copy(Bitmap.Config.ARGB_8888, true);
    // Create a canvas from the bitmap to draw on
    Canvas canvas = new Canvas(mBitmap);
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    paint.setTextSize(12);
    paint.setAntiAlias(true);
    canvas.drawText("Sept", 11, 26, paint);
    paint.setTextSize(18);
    canvas.drawText("29", 14, 42, paint);
    // Display the results on the screen
    image.setImageDrawable(new BitmapDrawable(this.getResources(), mBitmap));
} catch (Exception e) {
    e.printStackTrace ();
}

最佳答案

你为什么不直接使用 ImageView,并在其上放置一个 TextView 作为标题的覆盖层,放在启用此功能的布局容器内 - 例如RelativeLayout 还是 FrameLayout?

我有几个项目用这种方式在图像上写标题,这种方法没有问题。

关于android - 在图像上写文字的最佳方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10164590/

相关文章:

android - 如何在android中使grid-view水平滚动

android - ParseInstallation.SaveAsync() 在 Xamarin 表单 Android 上导致 UnhandledException

java - 在后台加载图像、视频

android - RecyclerView 是否有等效的 addHeaderView?

swiftui - Clipped 并不是在 SwiftUI 中实际剪辑图像

android - 如何获取 View 的位图?

android - 如何使用 PorterDuff Xfermodes 和 Alpha Mask 删除位图的一部分?

android:在imageview中拉伸(stretch)图像以适应屏幕

android - 如何知道 ImageView 的 X 和 Y 坐标?

android - 如何在Android中制作透明(alpha)渐变蒙版以使一层淡入透明?