Thursday, 16 August 2012

Draw text (Android)

package test.app;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView;
import android.view.View.OnKeyListener;
import android.view.View;
import android.widget.EditText;

public class Test extends Activity {
      ImageView drawingImage;
      String text;

      @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        addKeyListener();
       
        drawingImage = (ImageView) this.findViewById(R.id.drawingImageView1);
        Bitmap bitmap = Bitmap.createBitmap((int) getWindowManager()
            .getDefaultDisplay().getWidth(), (int) getWindowManager()
            .getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawingImage.setImageBitmap(bitmap);

        // Draw Text

        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setTextSize(64);
        float text_x = 120;
        float text_y = 120;
        canvas.drawText(text, text_x, text_y, paint);

    }
      private void addKeyListener() {
        final EditText edittext1 = (EditText) findViewById(R.id.editText1);
        edittext1.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((event.getAction() == KeyEvent.ACTION_DOWN)
                        && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    text = edittext1.getText().toString();
                    return true;   
                }
                return false;
            }
        });   
    }
}

1 comment:

  1. Hey. Copy and paste does not work when it gets. Are state employees can add this code?

    ReplyDelete