Sunteți pe pagina 1din 3

Source Code Live Code

TypeFace
Typeface font = Typeface.create(Typeface.SANS_SERIF,
Typeface.BOLD_ITALIC);

Paint black = new Paint();


black.setTypeface(font);
black.setTextSize(60);

Paint forestGreen = new Paint();


forestGreen.setARGB(255,34,139,34);
forestGreen.setStyle(Paint.Style.STROKE);
forestGreen.setStrokeWidth(10);

canvas.drawRect(10, 20, 90, 270, black);


canvas.drawOval(new RectF(80, 10, 280, 210), forestGreen);
canvas.drawText("I Love Ma Chung", 150, 70, black);

@Override
public void init() {

GOval head = new GOval(100,100,200,200);


head.setFillColor(GColor.YELLOW);
add(head);

GOval eye1 = new GOval(140,140,30,30);


eye1.setFillColor(GColor.BLUE);
add(eye1);
GOval eye2 = new GOval(230,140,30,30);
eye2.setFillColor(GColor.BLUE);
add(eye2);

GOval nose = new GOval(190,180,20,20);


nose.setFillColor(GColor.BLACK);
add(nose);

GRect mouth = new GRect(170,230,60,20);


mouth.setFillColor(GColor.RED);
add(mouth);

GLabel text = new GLabel("I Love Ma Chung", 100, 400);


text.setFont(Typeface.MONOSPACE, Typeface.BOLD, 40f);
add(text);

SmileyFacesolution
//face
Paint yellow = new Paint();
yellow.setARGB(255, 255, 255, 0);
yellow.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawOval(new RectF(100, 100, 300, 300), yellow);
//eyes
Paint blue = new Paint();
blue.setARGB(255, 0, 0, 255);
blue.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawOval(new RectF(140, 140, 170, 170), blue);
canvas.drawOval(new RectF(230, 140, 260, 170), blue);
//nose
Paint black = new Paint();
black.setARGB(255, 0, 0, 0);
black.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawOval(new RectF(190, 180, 210, 200), black);
//mouth
Paint red = new Paint();
red.setARGB(255, 0, 0, 255);
red.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawRect(170, 230, 230, 250, red);
//text
black.setTypeface(Typeface.create(Typeface.MONOSPACE, Typeface.BOLD));
black.setTextSize(40f);
canvas.drawText("I Love Ma Chung", 100, 400, black);

Animasi
public class FaceView extends GCanvas {

private GCompound person;


private GOval head;
private float vx = 5;
private float vy = 3;
private float accel = 0.005f;

public FaceView(Context context, AttributeSet attrs) {


super(context, attrs);
}

@Override
public void init() {

person = new GCompound();

head = new GOval(100,100,200,200);


head.setFillColor(GColor.YELLOW);
person.add(head);

GOval eye1 = new GOval(140,140,30,30);


eye1.setFillColor(GColor.BLUE);
person.add(eye1);
GOval eye2 = new GOval(230,140,30,30);
eye2.setFillColor(GColor.BLUE);
person.add(eye2);

GOval nose = new GOval(190,180,20,20);


nose.setFillColor(GColor.BLACK);
person.add(nose);

GRect mouth = new GRect(170,230,60,20);


mouth.setFillColor(GColor.RED);
person.add(mouth);

GLabel text = new GLabel("I Love Ma Chung", 100, 400);


text.setFont(Typeface.MONOSPACE, Typeface.BOLD, 40f);
add(text);
add(person);
animate(20);
}
@Override
public void onAnimateTick() {
super.onAnimateTick();

if ( person.getX() < 0 || person.getRightX() >= getWidth()-300){


vx = -vx;
}
if (person.getY() < 0 || person.getBottomY() >= getHeight()-300){
vy = -vy;
}
person.moveBy(vx,vy);
}

Pada getWidth() dan getHeight() diberi min 300 agar bola tidak melebihi sisi kanan
dan bawah layout.
angka 300 disini bisa berubah ubah sesuai layar android yang digunakan.

Pada Tampilan di atas adalah tampilan SmileyFace yang belum ada animasi. Jika di
masukkan source code animasi di atas maka SmileFace tersebut akan bergerak atau
akan memantul ke kanan kiri dank e atas bawah.
12 | Summary Graphic 2D

S-ar putea să vă placă și