vishalydv23
Lurker
i was trying to create an app which will simply draw the line of white color on a black background. Somewhere there is some error but i m not able to make out.
here is the code. help me to create the application
[HIGH]
package com.experimenting1.app;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
public class MainActivity extends Activity implements OnTouchListener {
Vanimation anim;
float x, y, sX, sY, fX, fY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
anim = new Vanimation(this);
anim.setOnTouchListener(this);
x = 0;
y = 0;
sX = 0;
sY = 0;
fY = 0;
fX = 0;
// anim.setBackgroundColor(Color.BLACK);
setContentView(anim);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
anim.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
anim.resume();
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
x = arg1.getX();
y = arg1.getY();
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
sX = arg1.getX();
sY = arg1.getY();
fX = fY = 0;
break;
case MotionEvent.ACTION_UP:
fX = arg1.getX();
fY = arg1.getY();
break;
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class Vanimation extends SurfaceView implements Runnable {
SurfaceHolder sh;
Thread th;
Boolean isRunning = true;
Paint p = new Paint();
public Vanimation(Context context) {
super(context);
sh = getHolder();
// TODO Auto-generated constructor stub
}
public void pause() {
isRunning = false;
while (true) {
try {
th.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
th = null;
}
public void resume() {
isRunning = true;
th = new Thread(this);
th.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
while (isRunning) {
if (!sh.getSurface().isValid()) {
continue;
}
}
p.setColor(Color.WHITE);
Canvas canvas = sh.lockCanvas();
//canvas.drawRGB(255, 255, 255);
if (x != 0 && y != 0) {
canvas.drawLine(sX, sY, fX, fY, p);
}
sh.unlockCanvasAndPost(canvas);
}
}
}
[/HIGH]
here is the code. help me to create the application

[HIGH]
package com.experimenting1.app;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
public class MainActivity extends Activity implements OnTouchListener {
Vanimation anim;
float x, y, sX, sY, fX, fY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
anim = new Vanimation(this);
anim.setOnTouchListener(this);
x = 0;
y = 0;
sX = 0;
sY = 0;
fY = 0;
fX = 0;
// anim.setBackgroundColor(Color.BLACK);
setContentView(anim);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
anim.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
anim.resume();
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
x = arg1.getX();
y = arg1.getY();
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
sX = arg1.getX();
sY = arg1.getY();
fX = fY = 0;
break;
case MotionEvent.ACTION_UP:
fX = arg1.getX();
fY = arg1.getY();
break;
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class Vanimation extends SurfaceView implements Runnable {
SurfaceHolder sh;
Thread th;
Boolean isRunning = true;
Paint p = new Paint();
public Vanimation(Context context) {
super(context);
sh = getHolder();
// TODO Auto-generated constructor stub
}
public void pause() {
isRunning = false;
while (true) {
try {
th.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
th = null;
}
public void resume() {
isRunning = true;
th = new Thread(this);
th.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
while (isRunning) {
if (!sh.getSurface().isValid()) {
continue;
}
}
p.setColor(Color.WHITE);
Canvas canvas = sh.lockCanvas();
//canvas.drawRGB(255, 255, 255);
if (x != 0 && y != 0) {
canvas.drawLine(sX, sY, fX, fY, p);
}
sh.unlockCanvasAndPost(canvas);
}
}
}
[/HIGH]