0% found this document useful (0 votes)
46 views4 pages

Listing Program

This document contains code for an Android application that allows drawing on a canvas. It includes: - A Latihan class that extends Activity and handles menu options like clearing the canvas. - A LatihanView class that extends View and handles touch input, drawing lines between points, and redrawing when the size changes. - Methods for handling touch events, drawing lines and points, clearing the canvas, and resizing as needed. The application allows freeform drawing on a canvas using multi-touch input and maintains the drawing history across touches. The menu provides options to clear the canvas or exit.

Uploaded by

farid pkrs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views4 pages

Listing Program

This document contains code for an Android application that allows drawing on a canvas. It includes: - A Latihan class that extends Activity and handles menu options like clearing the canvas. - A LatihanView class that extends View and handles touch input, drawing lines between points, and redrawing when the size changes. - Methods for handling touch events, drawing lines and points, clearing the canvas, and resizing as needed. The application allows freeform drawing on a canvas using multi-touch input and maintains the drawing history across touches. The menu provides options to clear the canvas or exit.

Uploaded by

farid pkrs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

LISTING PROGRAM

Latihan.java

package com.danu.skripsi;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;

public class Latihan extends Activity {


private static final int CLEAR_ID = Menu.FIRST;
private static final int SAMPLES_ID = Menu.FIRST+1;
LatihanView mView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
mView = new LatihanView(this);
setContentView(mView);
mView.requestFocus();
}
@Override public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, CLEAR_ID, 0, "Clear");
menu.add(0, SAMPLES_ID, 0, "Keluar").setCheckable(true);
return super.onCreateOptionsMenu(menu);
}
@Override public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
@Override public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case CLEAR_ID:
mView.clear();
return true;
case SAMPLES_ID:
finish();
default:
return super.onOptionsItemSelected(item);
}
}
@Override protected void onResume() {
super.onResume();
}
@Override protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}

@Override protected void onPause() {


super.onPause(); } }
LISTING PROGRAM
LatihanView.java

package com.danu.skripsi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;

public class LatihanView extends View {


private static final int MAXFINGERS = 20; // count the toes too!

private Bitmap mBitmap;


private Canvas mCanvas;
private final Rect mRect = new Rect();
private final Paint mPaint;
private float mCurX;
private float mCurY;
private float[] mPrevX = new float[MAXFINGERS]; // previous values of x,y to draw line
private float[] mPrevY = new float[MAXFINGERS];

public boolean bShowSamples = true;


Bitmap newBitmap;
public LatihanView(Context c) {
super(c);
setFocusable(true);
mPaint = new Paint();
mPaint.setAntiAlias(true);
}
public void clear() {
if (mCanvas != null) {
//mPaint.setARGB(0xff, 255, 255, 0);
//mCanvas.drawPaint(mPaint);
newBitmap.eraseColor(Color.argb(100, 255, 255, 0));
mCanvas.setBitmap(newBitmap);
invalidate();
}
}
@Override protected void onSizeChanged(int w, int h, int oldw,
int oldh) {
int curW = mBitmap != null ? mBitmap.getWidth() : 0;
int curH = mBitmap != null ? mBitmap.getHeight() : 0;
if (curW >= w && curH >= h) {
return;
}
if (curW < w) curW = w;
if (curH < h) curH = h;
newBitmap = Bitmap.createBitmap(curW, curH,Bitmap.Config.ARGB_8888);
newBitmap.eraseColor(Color.argb(100, 255, 255, 0)); /* screen goes white */
Canvas newCanvas = new Canvas();
newCanvas.setBitmap(newBitmap);
if (mBitmap != null) {
newCanvas.drawBitmap(mBitmap, 0, 0, null);
}
mBitmap = newBitmap;
mCanvas = newCanvas;
for (int finger=0; finger<MAXFINGERS; finger++){
mPrevX[finger]=-1;
mPrevY[finger]=-1;
}
}
@Override protected void onDraw(Canvas canvas) {
if (mBitmap != null) {
canvas.drawBitmap(mBitmap, 0, 0, null);
} }
@Override public boolean onTouchEvent(MotionEvent event) {
int action = event.getActionMasked();
int numPtrs;
switch (action) {
case MotionEvent.ACTION_DOWN: /* primary pointer */
case MotionEvent.ACTION_POINTER_DOWN: /* any subsequent pointer */
numPtrs = event.getPointerCount();
for (int finger = 0; finger < numPtrs; finger++) {
mPrevX[finger] = mPrevY[finger] = -1;
}
break;
case MotionEvent.ACTION_MOVE: /* any number of pointers move */
int N = event.getHistorySize();
numPtrs = event.getPointerCount();
for (int histndx = 0; histndx < N; histndx++) {
for (int finger = 0; finger < numPtrs; finger++) {
mCurX = event.getHistoricalX(finger, histndx);
mCurY = event.getHistoricalY(finger, histndx);
drawPoint(mCurX, mCurY,
event.getHistoricalPressure(finger, histndx),
event.getHistoricalSize(finger, histndx), finger);
} }
for (int finger = 0; finger < numPtrs; finger++) {
mCurX = event.getX(finger);
mCurY = event.getY(finger);
drawPoint(mCurX, mCurY, event.getPressure(finger), event.getSize(finger),finger);
} break;
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
for (int finger=0; finger<MAXFINGERS; finger++){
mPrevX[finger]=-1;
mPrevY[finger]=-1;
} break; }
return true; }
private void drawLine(int finger) {
mPaint.setARGB(255,250,1,1);
mPaint.setStrokeWidth(4);
mCanvas.drawLine(mPrevX[finger],mPrevY[finger],mCurX,mCurY,mPaint);
}
private void drawPoint(float x, float y, float pressure, float width, int finger) {
int lowX,lowY,highX,highY;
if (mBitmap != null) {
float radius = (float) 2.0;
mPaint.setARGB(255,255,0,0);
if ((mPrevX[finger]==-1 && mPrevY[finger]==-1)) {
mPrevX[finger] = x;
mPrevY[finger] = y;
mRect.set((int) (x - 10*radius), (int) (y - 10*radius),
(int) (x + 10*radius), (int) (y + 10*radius));
} else {
drawLine(finger);
if (mPrevX[finger] <= mCurX){
lowX = (int) (mPrevX[finger] - 3*radius);
highX = (int) (mCurX + 3*radius);
} else {
lowX = (int) (mCurX - 3*radius);
highX = (int) (mPrevX[finger] + 3*radius);
}
if (mPrevY[finger] <= mCurY){
lowY = (int) (mPrevY[finger] - 3*radius);
highY = (int) (mCurY + 3*radius);
} else {
lowY = (int) (mCurY - 3*radius);
highY = (int) (mPrevY[finger] + 3*radius);
}
mRect.set(lowX,lowY,highX,highY);
mPrevX[finger] = mCurX;
mPrevY[finger] = mCurY;
}
invalidate(mRect);
}}}

You might also like