代码之家  ›  专栏  ›  技术社区  ›  Signcodeindie

Android:如何将文本写入图像中的特定区域

  •  2
  • Signcodeindie  · 技术社区  · 12 年前

    我正在创建一个显示通知编号的Imagebutton。这是我的示例图像:

    Icon Image

    我的目标是在图像中的特定黑色圆圈上写一个数字(1,2..)。我尝试了RelativeLayout和FrameLayout,但不认为这是实现结果的可靠方法。原因是边距,不同设备大小的填充会有所不同。

    有什么建议可以开始吗?

    5 回复  |  直到 12 年前
        1
  •  2
  •   Joel Fernandes    12 年前

    干得好:

    <FrameLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/pencil">
    
            <TextView
                android:id="@+id/circle"
                android:layout_width="22dp"
                android:layout_height="22dp"
                android:background="@drawable/circle"
                android:gravity="center"
                android:text="2"
                android:layout_gravity="bottom|right"
                android:layout_marginRight="8dp"
                android:layout_marginBottom="8dp"
                android:textColor="#FFFFFF"
                android:textSize="12sp" />
    
        </FrameLayout>
    

    图像: enter image description here enter image description here

    输出:

    enter image description here

        2
  •  0
  •   Toon Borgers    12 年前

    您可以创建从ImageView扩展的自定义View类。您必须重写onDraw方法并执行以下操作:

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // Calculate x and y positions and set up a Paint instance with correct color/size
        canvas.drawText(String.valueOf(myNumber), x, y, myPaint);
    }
    
        3
  •  0
  •   Vigneshwaran Murugesan    12 年前

    尝试将背景设置为图像的Textview

    <TextView android:id="@+id/img" android:layout_width="45sp" android:layout_height="45sp" android:background="@drawable/circle" android:gravity="center" android:text="r" /> 使用重力在图像内对齐文本。

        4
  •  0
  •   Ketan Mehta    12 年前
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="bottom|right"
        android:paddingRight="20dp"
        android:paddingBottom="15dp"
        android:textColor="@android:color/white"
        android:background="@drawable/text"
        android:text="1" />
    
        5
  •  0
  •   Hitesh Singh    12 年前
    try this code    
    Bitmap bitmap = ... // Load your bitmap here
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(); 
    paint.setColor(Color.BLACK); 
    paint.setTextSize(10); 
    canvas.drawText("Some Text here", x, y, paint);