Users Online
· Guests Online: 150
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
020 Java Android Program to Demonstrate Surface View via Thread
Java Android Program to Demonstrate Surface View via Thread
This Android Java Program lets you create Create a Surface View via Thread.
Here is source code of the Program to create a Surface View via Thread. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.
Main Activity
package com.example.surface_view_thread;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
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{
Ourview v;
Bitmap bm;
float x,y;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
v = new Ourview(this);
v.setOnTouchListener(this);
bm = BitmapFactory.decodeResource(getResources(), R.drawable.image4);
x=0;y=0;
setContentView(v);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
v.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
v.resume();
}
// surface view is going to be a thread now
class Ourview extends SurfaceView implements Runnable {
Thread th = null;
SurfaceHolder holder;
boolean var = false;
public Ourview(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
}
@Override
public void run() {
// TODO Auto-generated method stub
while (var = true) {
// do stuff
if (!holder.getSurface().isValid()) {
continue;
}
Canvas c = holder.lockCanvas();// while drawing on a canvas we
// lock it and after drawing on
// it we unlock it
c.drawARGB(255, 250, 150, 20);//rgb values
c.drawBitmap(bm, x -(bm.getWidth()/2), y -(bm.getHeight()/2), null );
holder.unlockCanvasAndPost(c);
}
}
public void pause() {
var = false;
while (true) {
try {
th.join();// would pause the currently executing thread till
// the user finishes its job
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
th = null;
}
public void resume() {
var = true;
th = new Thread(this);
th.start();
}
}
@Override
public boolean onTouch(View v, MotionEvent me) {
// TODO Auto-generated method stub
return false;
}
}
This Android Java Program lets you create Create a Surface View via Thread.
Here is source code of the Program to create a Surface View via Thread. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.
Main Activity
package com.example.surface_view_thread;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
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{
Ourview v;
Bitmap bm;
float x,y;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
v = new Ourview(this);
v.setOnTouchListener(this);
bm = BitmapFactory.decodeResource(getResources(), R.drawable.image4);
x=0;y=0;
setContentView(v);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
v.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
v.resume();
}
// surface view is going to be a thread now
class Ourview extends SurfaceView implements Runnable {
Thread th = null;
SurfaceHolder holder;
boolean var = false;
public Ourview(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
}
@Override
public void run() {
// TODO Auto-generated method stub
while (var = true) {
// do stuff
if (!holder.getSurface().isValid()) {
continue;
}
Canvas c = holder.lockCanvas();// while drawing on a canvas we
// lock it and after drawing on
// it we unlock it
c.drawARGB(255, 250, 150, 20);//rgb values
c.drawBitmap(bm, x -(bm.getWidth()/2), y -(bm.getHeight()/2), null );
holder.unlockCanvasAndPost(c);
}
}
public void pause() {
var = false;
while (true) {
try {
th.join();// would pause the currently executing thread till
// the user finishes its job
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
th = null;
}
public void resume() {
var = true;
th = new Thread(this);
th.start();
}
}
@Override
public boolean onTouch(View v, MotionEvent me) {
// TODO Auto-generated method stub
return false;
}
}
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.