I try to use my own SurfaceView in XML and I am unable to do it. I get NullPointerException.
According internet it should look like this:
Activity:
package editor;
import android.app.Activity;
import android.os.Bundle;
import com.example.balls_menu_v1.R;
public class EditorActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editor);
EditorView ev = (EditorView) findViewById(R.id.editorView);
}
}
If I comment findViewById I get NullPointerException.
SurfaceView:
package editor;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class EditorView extends SurfaceView {
public EditorView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void onFinishInflate() {
super.onFinishInflate();
SurfaceHolder holder = getHolder();
Canvas canvas = holder.lockCanvas();
canvas.drawColor(Color.GREEN);
holder.unlockCanvasAndPost(canvas);
}
}
Layout: editor.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<editor.EditorView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/editorView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
I found answer: implement SurfaceHolder.Callback, add all 3 constructors of SurfaceView and add getHolder().addCallback(this); to each constructor.
Code:
public class EditorView extends SurfaceView implements SurfaceHolder.Callback{
public EditorView(Context context) {
super(context);
getHolder().addCallback(this);
// TODO Auto-generated constructor stub
}
public EditorView(Context context, AttributeSet attrs) {
super(context, attrs);
getHolder().addCallback(this);
// TODO Auto-generated constructor stub
}
public EditorView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
getHolder().addCallback(this);
// TODO Auto-generated constructor stub
}
public void doDraw() {
SurfaceHolder holder = getHolder();
Canvas canvas = holder.lockCanvas();
canvas.drawColor(Color.GREEN);
holder.unlockCanvasAndPost(canvas);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {}
#Override
public void surfaceCreated(SurfaceHolder holder) {
doDraw();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
you can't call Canvas canvas = holder.lockCanvas();`
before the oncreate flow finish,
you should call it after the oncreate finish
Related
When I try to create my GameView, I get an error "function call expected".
in my Main activity:
GameView gv = GameView(this, null);
public class GameView extends View {
private Drawable mCustomImage;
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
mCustomImage = context.getResources().getDrawable(R.drawable.bg);
}
protected void onDraw(Canvas canvas) {
Rect imageBounds = canvas.getClipBounds(); // Adjust this for where you want it
mCustomImage.setBounds(imageBounds);
mCustomImage.draw(canvas);
}
}
}
How do I create this dynamically?
I'm trying to add a view to my activity. The GameView will then control the position of its subviews programmatically.
This will make scene
I made some new changes in your GameView class. Below is copy of new GameView class.
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
public class GameView extends View {
private Context mContext;
private Drawable mCustomImage;
public GameView(Context context) {
super(context);
mContext = context;
mCustomImage = ContextCompat.getDrawable(context, R.mipmap.ic_launcher);
}
public GameView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
mContext = context;
mCustomImage = ContextCompat.getDrawable(context, R.mipmap.ic_launcher);
}
public GameView(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
mCustomImage = ContextCompat.getDrawable(context, R.mipmap.ic_launcher);
}
public GameView(Context context, #Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mContext = context;
mCustomImage = ContextCompat.getDrawable(context, R.mipmap.ic_launcher);
}
#Override
protected void onDraw(Canvas canvas) {
Rect imageBounds = canvas.getClipBounds();
mCustomImage.setBounds(imageBounds);
mCustomImage.draw(canvas);
}
}
Adding it through xml inside activity.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<GameView
android:layout_width="50dp"
android:layout_height="50dp" />
</LinearLayout>
</ScrollView>
Output
Adding it through code in activity
public class CustomActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draw_line);
initialiseView();
}
private void initialiseView() {
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout);
GameView mGameView = new GameView(this);
mLinearLayout.addView(mGameView);
}
}
Output
EDIT
If you want to set parameters while adding it pragmatically then initialiseView method will be like
private void initialiseView() {
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout);
GameView mGameView = new GameView(this);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(50, 50);
mGameView.setLayoutParams(layoutParams);
mLinearLayout.addView(mGameView);
}
Output :
GameView is a class, not a method, so you must call new to use them,
try this:
GameView gv = new GameView(this, null);
public class GameView extends View {
private Drawable mCustomImage;
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
mCustomImage = context.getResources().getDrawable(R.drawable.bg);
}
....
Use new GameView(this, null); inside onCreate of your activity to create a new instance of your view.
Then use the method setContentView(View) to use it in your activity.
public class MainActivity extends AppCompatActivity{
private GameView gameView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameView = new GameView(this, null);
setContentView(gameView);
//other code
}
//other methods
}
I have 3 views:
GLSurfaceView
Custom SurfaceView
View
The GLSurfaceView is at the bottom
The Custom SurfaceView should be above and on the top
The View is above and on the bottom
This is my code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
GameGlSurfaceView gameGlSurfaceView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameGlSurfaceView = new GameGlSurfaceView(this);
setContentView(gameGlSurfaceView);
View hudView = getLayoutInflater().inflate(R.layout.activity_main, null);
addContentView(hudView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
View inpudPadView = getLayoutInflater().inflate(R.layout.input_pad, null);
LinearLayout.LayoutParams inpudPadViewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
inpudPadViewParams.gravity = Gravity.TOP;
addContentView(inpudPadView, inpudPadViewParams);
}
}
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class SurfacePanel extends SurfaceView implements SurfaceHolder.Callback {
Context context;
Bitmap bitmapTest;
MyThread mythread;
public SurfacePanel(Context ctx, AttributeSet attrSet ) {
super(ctx, attrSet);
context = ctx;
bitmapTest = Bitmap.createBitmap(200, 100, Bitmap.Config.ARGB_8888);
bitmapTest.eraseColor(Color.TRANSPARENT);
SurfaceHolder holder = getHolder();
holder.addCallback(this);
}
void doDraw(Canvas canvas) {
canvas.drawColor(Color.GREEN);
canvas.drawBitmap(bitmapTest, 50, 10, null);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
mythread = new MyThread(holder, context,this);
mythread.setRunning(true);
mythread.start();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
mythread.setRunning(false);
boolean retry = true;
while(retry) {
try {
mythread.join();
retry = false;
}
catch(Exception e) {
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.batechniks.surfaceviewtest.MainActivity">
<de.batechniks.surfaceviewtest.SurfacePanel
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="left">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bt_down"
android:src="#mipmap/ic_launcher"
android:layout_gravity="bottom"/>
</LinearLayout>
The result is: I can see the GLSurfaceView and i can see the view above on the bottom.
On the top, i only see the textview "Large Text". The SurfaceView is not shown.
When i uncomment setContentView(gameGlSurfaceView), the SurfaceView is shown.
The main problem is to show the SurfaceView, the second problem will be to get the SurfaceView transparent.
Thanks a lot
You need to call the method
this.setZOrderOnTop(true);
in your constructors.
It's recommended to do constructors in custom views like this
public SurfacePanel (Context context) {
super(context);
init();
}
public SurfacePanel (Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public SurfacePanel (Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
#TargetApi(21)
public SurfacePanel (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
if(!isInEditMode()) {
this.setZOrderOnTop(true);
}
}
We have one MainActivity.
On this Main I would like to display my SurfaceView, but only if a button was clicked.
So when the main is started and the user clicks one button the SurfaceView should appear and everytim it appears it should excecute the onCreate-Method.
But it did not work.
All of my code is in this constructor:
public GameView(Context context, AttributeSet attrs)
So this one should be usesd, but i don't know what to do :/
EDIT:
Code in XML:
<de.apex.viewer.view.GameView
android:id="#+id/gameView1"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:layout_marginLeft="94dp" />
Here is some Code from the main:
public void displayGameView(View view)
{ // here I need code to "recreate" my gameView
}
And here some Code from the GameView which should be displayed:
public class GameView extends SurfaceView {
public GameView(Context context, AttributeSet attrs)
{
super(context, attrs);
surfaceHolder = getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback()
{
public void surfaceDestroyed(SurfaceHolder holder)
{}
public void surfaceCreated(SurfaceHolder holder)
{
// here is my code I want to execute
Canvas theCanvas = surfaceHolder.lockCanvas(null);
HouseInRow = DefineRowCount(arraylength);
GesamtZahl = HouseInRow*2+HouseInColumn*2;
System.out.println("Houserow: " + HouseInRow);
public void surfaceChanged(SurfaceHolder holder, int format,int width, int height)
{}
});
}
I have simple layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/backgroundtimer"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/hello" />
<com.fmech.zenclock.surface.ZenClockSurface
android:id="#+id/zenClockSurface1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
And a have ZenClockSurface class
package com.fmech.zenclock.surface;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class ZenClockSurface extends SurfaceView implements SurfaceHolder.Callback{
private DrawClock drawClock;
public ZenClockSurface(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
getHolder().addCallback(this);
}
public ZenClockSurface(Context context) {
super(context);
this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
getHolder().addCallback(this);
}
public ZenClockSurface(Context context, AttributeSet attrs) {
super(context, attrs);
this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
getHolder().addCallback(this);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
//this.getHolder().setFormat(PixelFormat.TRANSPARENT);
drawClock = new DrawClock(getHolder(), getResources());
drawClock.setRunning(true);
drawClock.start();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
// завершаем работу потока
drawClock.setRunning(false);
while (retry) {
try {
drawClock.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
class DrawClock extends Thread{
private boolean runFlag = false;
private SurfaceHolder surfaceHolder;
private Bitmap picture;
private Matrix matrix;
private long prevTime;
private Paint painter;
public DrawClock(SurfaceHolder surfaceHolder, Resources resources){
this.surfaceHolder = surfaceHolder;
this.surfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
picture = BitmapFactory.decodeResource(resources, R.drawable.ic_launcher);
matrix = new Matrix();
this.painter=new Paint();
this.painter.setStyle(Paint.Style.FILL);
}
public void setRunning(boolean run) {
runFlag = run;
}
#Override
public void run() {
Canvas canvas;
while (runFlag) {
matrix.preRotate(1.0f, picture.getWidth() / 2, picture.getHeight() / 2);
canvas = null;
try {
//surfaceHolder.getSurface().setAlpha(0.5f);
canvas = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder) {
canvas.drawColor(Color.TRANSPARENT);
canvas.drawBitmap(picture, matrix, this.painter);
}
}
finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
}
And code of activity
package com.fmech.zenclock.surface;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;
public class ZenClockSurfaceActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.main);
}
}
I want what background color of android picture was tranparent but i get black background.
I have background on RelativeLayout with some picture bu SurfaceView rotate Android icon with no transparent.
How i can do transparent?
Yeah, i did it i solve problem
Activity code
package com.fmech.zenclock.surface;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class ZenClockSurfaceActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ZenClockSurface sfvTrack = (ZenClockSurface)findViewById(R.id.zenClockSurface1);
sfvTrack.setZOrderOnTop(true); // necessary
SurfaceHolder sfhTrack = sfvTrack.getHolder();
sfhTrack.setFormat(PixelFormat.TRANSLUCENT);
}
}
Surface Code
package com.fmech.zenclock.surface;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PixelFormat;
import android.graphics.Region;
import android.util.AttributeSet;
import android.view.Surface.OutOfResourcesException;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class ZenClockSurface extends SurfaceView implements
SurfaceHolder.Callback {
private DrawClock drawClock;
public ZenClockSurface(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
getHolder().addCallback(this);
}
public ZenClockSurface(Context context) {
super(context);
getHolder().addCallback(this);
}
public ZenClockSurface(Context context, AttributeSet attrs) {
super(context, attrs);
getHolder().addCallback(this);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
drawClock = new DrawClock(getHolder(), getResources());
drawClock.setRunning(true);
drawClock.start();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
drawClock.setRunning(false);
while (retry) {
try {
drawClock.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
class DrawClock extends Thread {
private boolean runFlag = false;
private SurfaceHolder surfaceHolder;
private Bitmap picture;
private Matrix matrix;
private Paint painter;
public DrawClock(SurfaceHolder surfaceHolder, Resources resources) {
this.surfaceHolder = surfaceHolder;
picture = BitmapFactory.decodeResource(resources,
R.drawable.ic_launcher);
matrix = new Matrix();
this.painter = new Paint();
this.painter.setStyle(Paint.Style.FILL);
this.painter.setAntiAlias(true);
this.painter.setFilterBitmap(true);
}
public void setRunning(boolean run) {
runFlag = run;
}
#Override
public void run() {
Canvas canvas;
while (runFlag) {
matrix.preRotate(1.0f, picture.getWidth() / 2,
picture.getHeight() / 2);
canvas = null;
try {
canvas = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder) {
canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
canvas.drawBitmap(picture, matrix, this.painter);
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
}
It's work.
In API 3 and 4, you cannot put anything behind a SurfaceView. Quoting Dianne Hackborn:
The surface view is actually BEHIND your window, and a hole punched in the window for you to see it. You thus can put things on top of it in your window, but nothing in your window can appear behind it.
http://groups.google.com/group/android-developers/browse_thread/thread/8d88ef9bb22da574
From API 5 onwards you can use setZOrderOnTop. The trick is that you have to do it in your constructor so it gets called before the view is attached to the window:
public ZenClockSurface(Context context, AttributeSet attrs) {
super(context, attrs);
setZOrderOnTop(true);
SurfaceHolder holder = getHolder();
holder.setFormat(PixelFormat.TRANSLUCENT);
}
My view is written as follow:
package com.mycompany;
import android.view.View;
import java.util.concurrent.TimeUnit;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.graphics.Paint;
import android.graphics.Point;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.*;
public class GameEngineView extends View implements SensorEventListener {
GameLoop gameloop;
String txt_acc;
float accY;
ArrayList<Point> bugPath;
private SensorManager sensorManager;
private class GameLoop extends Thread {
private volatile boolean running = true;
public void run() {
while (running) {
try {
TimeUnit.MILLISECONDS.sleep(1);
postInvalidate();
pause();
} catch (InterruptedException ex) {
running = false;
}
}
}
public void pause() {
running = false;
}
public void start() {
running = true;
run();
}
public void safeStop() {
running = false;
interrupt();
}
}
public void unload() {
gameloop.safeStop();
}
public GameEngineView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
init(context);
}
public GameEngineView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init(context);
}
public GameEngineView(Context context) {
super(context);
// TODO Auto-generated constructor stub
init(context);
}
private void init(Context context) {
txt_acc = "";
// Adding SENSOR
sensorManager=(SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
// add listener. The listener will be HelloAndroid (this) class
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
// Adding UI Elements : How ?
Button btn_camera = new Button(context);
btn_camera.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
btn_camera.setClickable(true);
btn_camera.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("clicked the camera.");
}
});
gameloop = new GameLoop();
gameloop.run();
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
//super.onMeasure(widthMeasureSpec, heightMeasureSpec);
System.out.println("Width " + widthMeasureSpec);
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
// super.onDraw(canvas);
Paint p = new Paint();
p.setColor(Color.WHITE);
p.setStyle(Paint.Style.FILL);
p.setAntiAlias(true);
p.setTextSize(30);
canvas.drawText("|[ " + txt_acc + " ]|", 50, 500, p);
gameloop.start();
}
public void onAccuracyChanged(Sensor sensor,int accuracy){
}
public void onSensorChanged(SensorEvent event){
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
//float x=event.values[0];
accY =event.values[1];
//float z=event.values[2];
txt_acc = "" + accY;
}
}
}
I would like to add a Button to the scene, but I don't know how to. Can anybody give me some lights?
UPDATE: Here is my Activity :
public class MyActivity extends Activity {
private GameEngineView gameEngine;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// add Game Engine
gameEngine = new GameEngineView(this);
setContentView(gameEngine);
gameEngine.requestFocus();
}
}
And I am following the this tutorial .
detailListView = (LinearLayout) findViewById(R.id.DetailsLinearLayout); // Find the layout where you want to add button
Button button = new Button(this);
button.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
,ViewGroup.LayoutParams.WRAP_CONTENT));
detailListView.addView(button);//add view to add