এবার আমরা জানব canvas এ কিভাবে text add করা যায়। তার আগে এর আগের পোস্ট টি দেখে আসা ভাল-circle draw
package com.example.graphics2d;
import android.R.color;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.view.View;
public class Circle2D extends View {
public Circle2D(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawColor(Color.BLACK);
সবার আগে flag add করে নিতে হবে
Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.CYAN);---কালার এড করলাম
paint.setTextSize(20);----্টেস্ক সাইজ় এড করলাম
paint.setTypeface(null);----টাইপফেস নাল রাখলাম । এখানে বলে রখা দরকার এটি ডিফল্ process . it ser sans-sarif as a font and normal mode if we change this we have to add [Typeface face=Typeface.create(Typeface.MONOSPACE, Typeface.BOLD_ITALIC)] and have to set face replaced by null in paint.setTypeface() .
paint.setFlags(Paint.UNDERLINE_TEXT_FLAG);-----to underLine the text
canvas.drawText("It is a Circle",20,20, paint);----it is the main form to add text . canvas.drawText() has four argument one-text two-width three--height forth-- Paint that we set before
paint.setColor(Color.RED);
canvas.drawCircle(canvas.getWidth()/2,canvas.getHeight()/2, canvas.getWidth()/3, paint);
}
}
এবার Typeface face=Typeface.create(Typeface.MONOSPACE, Typeface.BOLD_ITALIC); সেট করলে text এমন দেখাবে
package com.example.graphics2d;
import android.R.color;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.view.View;
public class Circle2D extends View {
public Circle2D(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawColor(Color.BLACK);
Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
Typeface face=Typeface.create(Typeface.MONOSPACE, Typeface.BOLD_ITALIC);---for this we set our custom Typeface
paint.setColor(Color.CYAN);
paint.setTextSize(20);
paint.setTypeface(face);
paint.setFlags(Paint.UNDERLINE_TEXT_FLAG);
canvas.drawText("It is a Circle",20,20, paint);
paint.setColor(Color.RED);
canvas.drawCircle(canvas.getWidth()/2,canvas.getHeight()/2, canvas.getWidth()/3, paint);
}
}