Have been looking on some tutorials for drawing canvas using SurfaceView, but the only thing that shows up is a black background.
public class FighterActivity extends Activity implements OnTouchListener {
/** Called when the activity is first created. */
SurfaceController surface;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
surface = new SurfaceController(this);
surface.setOnTouchListener(this);
setContentView(surface);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
surface.pause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
surface.resume();
}
public class SurfaceController extends SurfaceView implements Runnable{
Thread thread = null;
SurfaceHolder holder;
public SurfaceController(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
System.out.println("HERE");
}
public void run() {
// TODO Auto-generated method stub
System.out.println("Hello World2");
while(true){
if(!holder.getSurface().isValid()){
System.out.println("NOT VALID");
continue;
}
System.out.println("VALID!");
Canvas can = holder.lockCanvas();
can.drawARGB(255, 150, 150, 0);
holder.unlockCanvasAndPost(can);
}
}
public void pause(){
}
public void resume(){
}
}
public boolean onTouch(View view, MotionEvent me) {
// TODO Auto-generated method stub
return false;
}
}
It gets to the System.out.println("HERE"); and prints out HERE, but nothing more happends, In other words the thread does not get started since "Hello World2" is not printed, what is the problem?
Thanks for any help
I'm assuming you're building off of this: http://android-coding.blogspot.ca/2011/05/drawing-on-surfaceview.html
You'll notice there the onResumeMySurfaceView and onPauseMySurfaceView (resume and pause in your SurfaceController, respectively) start the actual thread. You'll need to do that in your code, too, e.g. in SurfaceController:
protected boolean running = false;
public void resume() {
running = true;
thread = new Thread(this);
thread.start();
}
Related
I have a class that extends Activity, When I try to access the context from the onCreate() method it gets printed but when I save it in a variable context and try to access it from the committext() function as shown below, It prints null. I also tried using "this" and getBaseContext() directly from the committext() function as it is a part of the same class, I get null pointer exception. Please help me figure out what is going wrong.
public class MainKeyboardActionListener extends Activity implements KeyboardView.OnKeyboardActionListener, View.OnTouchListener {
private Context context;
public static boolean active = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mode_keyboard, false);
context=getBaseContext();
System.out.println("contexxtt1"+context);
}
#Override
public void onStart() {
super.onStart();
active = true;
}
#Override
protected void onResume() {
super.onResume();
}
private static class myHandler extends Handler {
}
;
public void setInputConnection(InputConnection ic) {
}
#Override
public void onKey(int arg0, int[] arg1) {
// TODO Auto-generated method stub
}
#Override
public void onPress(int keyCode) {
}
#Override
public void onRelease(int keyCode) {
commitText(String.valueOf(keyCode));
}
private void handleException(int keyCode) {
}
private void removeHalantMode() {
}
private void commitText(String text) {
if(active==true) {
System.out.println("contexxtt"+context);
}
}
#Override
public void onText(CharSequence arg0) {
// TODO Auto-generated method stub
}
#Override
public void swipeDown() {
// TODO Auto-generated method stub
}
#Override
public void swipeLeft() {
// TODO Auto-generated method stub
}
#Override
public void swipeRight() {
// TODO Auto-generated method stub
}
#Override
public void swipeUp() {
// TODO Auto-generated method stub
}
#Override
protected void onPause() {
super.onPause();
}
#Override
public void onStop() {
super.onStop();
active = false;
}
#Override
public void onDestroy() {
super.onDestroy();
}
}
Make your myHandler class not static.
I'm trying to get an application to paint something on a canvas every half a second, but the SurfaceHolder.getSurface().isValid() returns false, and when I call SurfaceHolder.lockCanvas() this returns null.
As per this SO question, I should use a SurfaceHolder.Callback.surfaceCreated but the surface is never created.
The onCreate method from my main Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
ChartPainter p = new ChartPainter(this);
}
And part of my ChartPainter.java
public ChartPainter(Context context) {
super(context);
holder = getHolder();
final boolean a[] = new boolean[1];
a[0] = false;
holder.addCallback(new Callback() {
#Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
a[0] = true;
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
});
} [...]//more omitted code here
How do I create the surface for the SurfaceHolder?
Does ChartPainter extends SurfaceView?
Note: SurfaceHolder is usually used with SurfaceView. When the activity goes to the foreground and its SurfaceView is about to be rendered, WindowManager will ask SurfaceFlinger to create a new surface. Then SurfaceHolder's surfaceCreated() will be called.
I am new to graphics and surfaceviews etc. I have heard that it is better to draw on a seperate thread than on the main thread. i have 2 activities one which is drawn using ondraw of the view, the other uses surface view. both have the same code for drawing. however the latter gives a much darker backgroung color-even at max alpha setting(255). If I comment out the drawargb line I get a black background. I tried many things like setting background color of surfaceview object and setting pixelformat to rgb888 or transparent but none of these work.Following is the code:
SurfaceHolder ourHolder;Boolean isRunning=true;
Thread ourThread;ArrayList<Float> amtint=new ArrayList();float max;
String names[];String company;
public GraphSview(Context c) {
super(c);
ourHolder=getHolder();
ourThread=new Thread(this);
ourThread.start();}
public void pause(){
isRunning=false;
while(true){try {
ourThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}break;}ourThread=null;
}
public void Resume()
{
isRunning=true;
ourThread=new Thread(this);
ourThread.start();}
#Override
public void run() {
// TODO Auto-generated method stub
while(isRunning){
if(!ourHolder.getSurface().isValid())continue;
Canvas canvas=ourHolder.lockCanvas();
canvas.drawARGB(255, 33, 181, 238);
.... ourHolder.unlockCanvasAndPost(canvas);
code for the main activity is
public class GraphS extends Activity{
GraphSview ourSview;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ourSview=new GraphSview(this);
ourSview.getHolder().setFormat(PixelFormat.TRANSPARENT);
setContentView(ourSview);
//ourSview.setBackgroundColor(0Xffffffff);
}
#Override
protected void onPause() {
ourSview.pause();
super.onPause();
}
#Override
protected void onResume() {
ourSview.Resume(); // TODO Auto-generated method stub
super.onResume();
}
}
is there something wrong with the code?
is there any alternate way of creating and displaying graphs other than surface view?
following is my simple code, actually i want to log HELLO in the draw() method repeatedly but my surfacecreated method is not called and so thread is not started. plz help me
public class MainActivity extends Activity{
private MyThread myThread ;
Panel _View;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
_View = new Panel(this);
}
public class Panel extends SurfaceView implements SurfaceHolder.Callback {
Canvas canvas;
public Panel(Context context) {
super(context);
getHolder().addCallback(this);
}//end of panel
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
myThread= new MyThread(this);
myThread.setRunning(true);
myThread.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
myThread.setRunning(false);
}
public void Draw(Canvas canvas){
super.draw(canvas);
this.canvas = canvas;
Log.d("HELLO", 0+"");
}//end of DRAW()
}//end of Panel class
public class MyThread extends Thread {
Panel panel;
private SurfaceHolder myHolder;
boolean mRun=false;
public MyThread(Panel panel)
{
this.panel= panel;
this.myHolder = panel.getHolder();
}
public void setRunning(boolean run){
this.mRun=run;
}
public void run(){
Canvas canvas = null;
while(mRun)
{
canvas=myHolder.lockCanvas();
if(canvas!=null)
{
panel.Draw(canvas);
}
myHolder.unlockCanvasAndPost(canvas);
}
}
}//MyThread class ends
}//end of bird mania activity
The surface is not created until you add it to the View hierarchy. If it is your only View, you can set it with
setContentView( _View);
in your onCreate() method.
I have a problem with a thread in surfaceview. I can't understand how to onPause/onResume when I lock my phone. Whatever I do, the thread doesn't respond after locking/unlocking the phone.
In the activity:
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
surfaceView.SurfaceView_OnResume();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
surfaceView.SurfaceView_OnPause();
}
In the surfaceview
public void SurfaceView_OnResume() {
if (null != surfaceViewThread) {
surfaceViewThread.setRunning(true);
surfaceViewThread.notify();
}
}
public void MySurfaceView_OnPause() {
surfaceViewThread.setRunning(false);
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
boolean retry = true;
myGameThread.setRunning(false);
while (retry) {
try {
myGameThread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
In the thread:
public void setRunning(boolean run) {
runFlag = run;
}
Thread.setRunning(boolean b) is not in the Android API.
You can check it here. http://developer.android.com/reference/java/lang/Thread.html
And if I need a Thread, I prefer to use Runnable(Interface), not Thread(Object).
To control my Thread cycle, I will design my method:run() like this...
run(){
while(threadRun){
...//What you want to do in the thread.
while(threadPause){
}
}
}
The Boolean:threadRun will turn to false in Activity.onDestroy(), or any other time you really want to shut down the thread.
The Boolean:threadPause ... turn to false in Activity.onPause() and turn to true in Activity.onResume().