Wednesday, November 10, 2010

Using canvas to draw a text whereve a click is made on screen.

How to draw a text wherever a click is made.

- A custom view is implemented, which overrides onDraw() function call.

- To get the touch point (x,y) on screen, override the onTouchEvent() and check for action ACTION_DOWN and update the global variables x,y.

- Later in onDraw(), on the canvas object supplied from framework, use the drawText api to draw the text on the touched point.

- A paint object is initialised with the color and font size values. Use this in drawText() api.



drawText(String text, float x, float y, Paint paint)
Draw the text, with origin at (x,y), using the specified paint.



- Create a instance of this custom view and set this as setContentView() of activity to display this view.



imagemove.java
---------------



package com.android.imagemove;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

public class imagemove extends Activity {

public class DisplayView extends View
{
int positionX = 5;
int positionY = 15;
Paint mPaint;
DisplayView(Context context)
{
super(context);
mPaint = new Paint();
mPaint.setTextSize(25);
mPaint.setColor(0xFF0000FF);
mPaint.setTextSize(16);

}
@Override
public boolean onTouchEvent(MotionEvent event)
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN: {
positionX = (int)event.getX();
positionY = (int)event.getY();

invalidate();
}
}
return true;
}
@Override
public void onDraw(Canvas canvas)
{
System.out.println("X & Y"+positionX+":"+positionY);
canvas.drawText("Welcome", positionX, positionY, mPaint);
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DisplayView view = new DisplayView(this);
setContentView(view);
}

}

1 comment:

  1. Hey great content...But I need to customize this .
    I have loaded one image in Background and I am able to move that DisplayView but I need to save this DisplayView on a bitmap...Based on the location of DisplayView.
    Bitmap is saved but without the DisplayView.Please guide me.

    Below is my code:
    onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    llCartLayout = (LinearLayout) findViewById(R.id.CartLayout);
    BtnSave = (Button) findViewById(R.id.BtnSave);
    DisplayView displayView = new DisplayView(this);
    llCartLayout.addView(displayView);

    BtnSave.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    saveImage(processedBitmap);
    }
    });
    }
    Display view class is same except: public void onDraw(Canvas canvas) {
    // Toast.makeText(MainActivity.this,"X & Y" + positionX + ":" + positionY,Toast.LENGTH_SHORT).show();
    canvas.drawText("Welcome", positionX, positionY, mPaint);
    callProcessBitmapMethod();
    }
    callProcessBitmapMethod() {
    processedBitmap = ProcessingBitmap();}
    private Bitmap ProcessingBitmap() {
    Bitmap bm1 = null;
    Bitmap newBitmap = null;
    Resources resources = getResources();
    bm1 = BitmapFactory.decodeResource(resources, R.drawable.img_background);
    Bitmap.Config config = bm1.getConfig();
    if (config == null) {
    config = Bitmap.Config.ARGB_8888;
    }
    newBitmap = Bitmap.createBitmap(bm1.getWidth(), bm1.getHeight(), config);
    newCanvas = new Canvas(newBitmap);
    newCanvas.drawBitmap(bm1, 0, 0, null);

    return newBitmap;
    }

    ReplyDelete