Example to draw text on Bitmap.
package com.example.androidimageview;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SeekBar;
public class MainActivity extends ActionBarActivity {
 SeekBar textSizeBar;
 ImageView image1, image2;
 Button btnDrawText;
 EditText textIn;
 Bitmap bitmapOriginal;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  textSizeBar = (SeekBar) findViewById(R.id.textsize);
  btnDrawText = (Button) findViewById(R.id.drawtext);
  textIn = (EditText) findViewById(R.id.textin);
  image1 = (ImageView) findViewById(R.id.image1);
  image2 = (ImageView) findViewById(R.id.image2);
  bitmapOriginal = BitmapFactory.decodeResource(getResources(),
    R.drawable.ic_launcher);
  image1.setImageBitmap(bitmapOriginal);
  btnDrawText.setOnClickListener(btnDrawTextOnClickListener);
  ReloadImage();
 }
 
 OnClickListener btnDrawTextOnClickListener = new OnClickListener(){
  @Override
  public void onClick(View v) {
   ReloadImage();
  }};
 private void ReloadImage() {
  int textSize = textSizeBar.getProgress();
  String textToDraw = textIn.getText().toString();
  Bitmap newBitmap = bitmapOriginal.copy(bitmapOriginal.getConfig(), true);
  Canvas newCanvas = new Canvas(newBitmap);
  Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  paint.setColor(Color.RED);
  paint.setTextSize(textSize);
  Rect bounds = new Rect();
  paint.getTextBounds(textToDraw, 0, textToDraw.length(), bounds);
  int x = 0;
  int y = newBitmap.getHeight();
  
  newCanvas.drawText(textToDraw, x, y, paint);
  
  image1.setImageBitmap(newBitmap);
  image2.setImageBitmap(newBitmap);
 }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidimageview.MainActivity" >
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    
    <EditText
        android:id="@+id/textin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <SeekBar
        android:id="@+id/textsize"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="50"
        android:progress="10" />
    
    <Button
        android:id="@+id/drawtext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Draw text on bitmap" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
        <ImageView
            android:id="@+id/image1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        
        <ImageView
            android:id="@+id/image2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#D0D0D0" />
    </LinearLayout>
</LinearLayout>

 
No comments:
Post a Comment