I am trying to overlay a drawing on top of a camera preview. I made two custom class extended from SurfaceView: one for the overlay, and one for the cam.
Here is my logcat (only the "Caused by" statements) :
04-11 19:58:06.549: W/dalvikvm(867): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-11 19:58:06.609: E/AndroidRuntime(867): Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class com.example.gui_v9.Activity1.CamView
04-11 19:58:06.609: E/AndroidRuntime(867): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.gui_v9.Activity1.CamView" on path: /data/app/com.example.gui_v9-2.apk
The second error where it cannot find the class, is because it did not inflate.
Eclipse Graphical Layout successfully compiles the xml code and gives a decent preview. Here is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.gui_v9.Activity1.CamView
android:id="#+id/camview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<com.example.gui_v9.Activity1.OverlayView
android:id="#+id/overlay"
android:layout_width="200dp"
android:layout_height="200dp"/>
</FrameLayout>
I tried to change the flags from
<com.example.gui_v9.Activity1.OverlayView .../>
to
<com.example.gui_v9.Activity1.OverlayView ...></com.example.gui_v9.Activity1.OverlayView>
or to
<SurfaceView class="com.example.gui_v9.Activity1$OverlayView" ... />
or even
<SurfaceView class="com.example.gui_v9.Activity1$OverlayView" ... ></SurfaceView>
without luck. In the latter case, I have a casting problem (CamView cannot be casted in SurfaceView) and Eclipse's XML Graphical Layout fails to render, so I m guessing that the first one takes me further in the compiling process.
You may also take a look at my main activity:
package com.example.gui_v9;
import java.io.IOException;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.hardware.Camera;
public class Activity1 extends Activity {
CamView camview = null;
OverlayView overlay = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
camview = (CamView) findViewById(R.id.camview);
overlay = (OverlayView) findViewById(R.id.overlay);
overlay.getHolder().setFormat(PixelFormat.TRANSLUCENT);
}
public static class CamView extends SurfaceView implements SurfaceHolder.Callback {
Camera cam = null;
SurfaceHolder camviewholder = null;
public CamView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CamView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public CamView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
if (cam!=null) {
try {
cam.setPreviewDisplay(camviewholder);
}
catch (IOException e) {
}
cam.startPreview();
}
}
#Override
public void surfaceCreated(SurfaceHolder arg0) {
cam = Camera.open();
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
cam.stopPreview();
cam.release();
}
}
public static class OverlayView extends SurfaceView implements SurfaceHolder.Callback {
private Paint paint = new Paint();
OverlayThread overlaythread = null;
public OverlayView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public OverlayView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public OverlayView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
#Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.BLUE);
canvas.drawCircle(100, 100, 100, paint);
}
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
#Override
public void surfaceCreated(SurfaceHolder arg0) {
setWillNotDraw(false); //Allows us to use invalidate() to call onDraw()
overlaythread = new OverlayThread(getHolder(), this);
overlaythread.setRunning(true);
overlaythread.start();
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
try {
overlaythread.setRunning(false);
overlaythread.join();
}
catch (InterruptedException e) {
}
}
}
public static class OverlayThread extends Thread {
private OverlayView overlay;
private SurfaceHolder overlayholder;
private boolean _run = false;
public OverlayThread(SurfaceHolder _overlayholder, OverlayView _overlay) {
overlayholder = _overlayholder;
overlay = _overlay;
}
public void setRunning(boolean run) {
_run = run;
}
#Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
c = overlayholder.lockCanvas(null);
synchronized (overlayholder) {
overlay.invalidate();
}
}
finally {
if (c != null) {
overlayholder.unlockCanvasAndPost(c);
}
}
}
}
}
}
As you can see, I have the three constructors for each classes (with 1,2 and 3 arguments) which is a common error in inflating class failure.
I got this to work when I implemented CamView and OverlayView in two separate classes (CamView.java and OverlayView.java), respectively. So I don't understand why importing classes makes it work. Maybe something with the static workspace?
Thank yall for your help!
Related
I'm new to Android and I have been struggling to draw on canvas a rectangle and text where the size of the rectangle, the text and the color depend on values selected from a MySql database.
I managed to get data selected inside my Activity, but I just can't figure it out how to pass the MySql data to the ondraw() method so I can draw the rectangle and text using the data.
Any help will be greatly appreciated.
public class MyTankActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int useridInt = intent.getIntExtra("userid", -1);
String userid = useridInt+"";
Response.Listener<String> responseListener = new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
int success = jsonResponse.getInt("success");
JSONArray tank_data = jsonResponse.getJSONArray("tank_data");
if (success == 1) {
int i;
for(i=0;i<tank_data.length();i++){
// Log.v("Result--", "" + tank_data.getString(i));
JSONObject tankObj = tank_data.getJSONObject(0);
String location = (String) tankObj.getString("Location");
String color = (String) tankObj.getString("Color");
String Level = (String) tankObj.getString("Level");
}
} else {
// No records found in database
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
MyTankRequest myTankRequest = new MyTankRequest(userid, responseListener);
RequestQueue queue = Volley.newRequestQueue(MyTankActivity.this);
queue.add(myTankRequest);
setContentView(new TankView(this));
}
}
XML layout:
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.nivelsonic.nivelsonic.MyTankActivity"
android:background="#AEECFF">
<com.nivelsonic.nivelsonic.TankView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
TankView class:
package com.nivelsonic.nivelsonic;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class TankView extends View {
private Paint _paintTank = new Paint();
private Path _path = new Path();
public TankView(Context context) {
super(context);
// TODO Auto-generated constructor stub
init(null, 0);
}
public TankView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
init(attrs, 0);
}
public TankView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init(attrs, 0);
}
private void init(AttributeSet attrs, int defStyle) {
_paintTank.setColor(Color.RED);
_paintTank.setAntiAlias(true);
_paintTank.setStyle(Paint.Style.STROKE);
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
// canvas.drawText();
// canvas.rect();
}
}
One suggestion:
Move JSON data to global variables
public class MyTankActivity extends AppCompatActivity {
String location;
String color;
String Level;
//....
And change setContentView with different constructor of TankView that allows to pass the obtained data:
setContentView(new TankView(this, location, color, Level));
Modify the constructor of TankView accordingly, also add some variables to TankView to store this values:
public TankView(Context context, String loc, String color, String level) {
super(context);
init(null, 0);
this.location = loc;// ... do the same with color/level
...
}
Modify the onDraw after you get the data
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
//using this.Location, color, text
// canvas.drawText();.
// canvas.rect();
}
I've created a custom XML layout and I’m trying to draw the map background and the pawn player( the bitmap) over it.
Instead, its painting the pawn player over a white background without the map background that I put as the background on the XML file.
MyViev Class:
package com.example.alpha;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
Bitmap playerW;
float changingY;
float changingX;
public MyView(Context context) {
super(context);
playerW = BitmapFactory
.decodeResource(getResources(), R.drawable.black);
changingY=0;
// TODO Auto-generated constructor stub
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(playerW,4+changingX, (canvas.getHeight())-288-changingY, null);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
}
my MainActivity Class:
package com.example.alpha;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
MyView ourView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ourView = new MyView(this);
setContentView(ourView);
}
}
My XML file:
<?xml version="1.0" encoding="utf-8"?>
<view class="com.example.alpha.MyView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/mapeasy"
/>
It's because you're not actually setting it to use your XML layout. Instead, you've set the content View to be a new instance of MyView, which doesn't have the XML background property set.
Therefor you have 2 options:
Option 1: Call ourView.setBackgroundDrawable(R.drawable.mapeasy); after you created a new instance of MyView.
or
Option 2: Set the content View to be your actual layout file and then find your MyView by using findViewById(int).
Eg.
public class MainActivity extends ActionBarActivity {
MyView ourView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout_file);
ourView = (MyView)findViewById(R.id.myView);
}
}
My app consist on moving a picture when the user speaks. I have done this, but what I want to do now is that I want to set a image as background for my app. I am working with canvas, as you can see in the class I have included below. So how can I set a background using canvas and not influencing the movement of my picture. Or is there any possibility to connect this class with a xml file where I can define the background?
Thanks in advance
Here is the class:
package com.example.prova1;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MoveBalloon extends Activity {
Bitmap balloon;
DrawBalloon myView;
float x,y,sensorX, sensorY;
SensorManager sm;
Microphone mic;
public class DrawBalloon extends SurfaceView implements Runnable {
SurfaceHolder ourHolder ;
Thread ourThread = null;
boolean isRunning=true;
public DrawBalloon(Context context) {
super(context);
ourHolder= getHolder();
}
public void pause() {
isRunning=false;
while(true){
try{
ourThread.join();
} catch (InterruptedException e){
e.printStackTrace();
}
break;
}
ourThread=null;
}
public void resume(){
isRunning=true;
ourThread = new Thread(this);
ourThread.start();
}
#Override
public void run (){
while(isRunning){
if(!ourHolder.getSurface().isValid())
continue;
Canvas canvas = ourHolder.lockCanvas();
updateMic();
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(balloon, sensorX, sensorY,null);
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
/*sm= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if(sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size()!=0){
Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(o);
sm.registerListener(this,s ,SensorManager.SENSOR_DELAY_NORMAL);
}*/
mic = new Microphone();
balloon = BitmapFactory.decodeResource(getResources(), R.drawable.images);
sensorX=150;
sensorY=350;
//x=y=sensorX=sensorY=0;
myView= new DrawBalloon (this);
myView.resume();
setContentView(myView);
}
/*#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
#Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sensorX=event.values[0];
sensorY=event.values[1];
}
*/
public void updateMic(){
int level = mic.getLevel();
sensorY-=level;
}
#Override
public void onBackPressed() {
finish();
}
}
You can use an xml layout instead of setting content view with a java object :
setContentView(R.layout.move_balloon);
and add your custom view (DrawBalloon) in the xml layout by specifying it's package location, and change the background by setting an image to the background of the root element :
<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=".MoveBalloon"
android:background="#drawable/ic_launcher"
>
<com.example.prova1.MoveBalloon.DrawBalloon
android:id="#+id/drawBalloon1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="182dp" />
</RelativeLayout>
but for this to work , your custom view must have a special constructor in able to be inflated from xml layout file :
public DrawBalloon(Context context, AttributeSet attrs) {
super(context, attrs);
ourHolder= getHolder();
}
this here is driving me nuts...
I want to pass some Parameters with a Button Constructor
I did create myButton extending Button:
package com.canbluetoothinterface.utilities;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class myActivityStartButton extends Button implements OnClickListener {
protected EditText[] Array;
private String Name;
private String BufferName;
private Activity activityinstance;
public String[] sValues;
Class<?> cls;
private Method m;
public myActivityStartButton(String Name, Class<?>clsin, Context context) {
super(context);
cls = clsin;
activityinstance = (Activity) context;
setId(mygetId());
this.Name = Name;
init();
}
private void init(){
setOnClickListener(this);
}
public myActivityStartButton(String Name, Class<?>clsin, Context context, Method min) {
super(context);
this.cls = clsin;
this.Name = Name;
this.m = min;
activityinstance = (Activity) context;
setId(mygetId());
setTag(findViewById(mygetId()));
init();
}
#Override
public void setId(int id) {
super.setId(id);
}
private int mygetId() {
int id = 0;
BufferName = Name;
id = activityinstance.getResources().getIdentifier(BufferName, "id", activityinstance.getPackageName());
return id;
}
#Override
public void onClick(View v) {
try {
m.invoke(null, (Object)null);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent(activityinstance, cls );
activityinstance.startActivity(intent);
}
}
In my Activity I call:
Start = new myActivityStartButton("act_testdriveconfiguration_btn_start", DeviceListActivity.class, this, mstartbutton);
But my OnClick is never called...
What am I doing wrong?
Thank you
1.double check your init() is called.
The following code works for me.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.main_container);
SomeButton btn = new SomeButton(this);
layout.addView(btn);
}
private class SomeButton extends Button implements OnClickListener {
public SomeButton(Context context) {
super(context);
init();
}
private void init() {
setOnClickListener(this);
}
#Override
public void onClick(View v) {
Log.w("log", "click");
}
}
NOTE:
if your button is created in xml, you need these constructors.
public setParams(String Name, Class<?>clsin,etc params)
{
//saving params
}
public SomeButton(Context context) {
super(context);
init();
}
public SomeButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public SomeButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
I'm trying to build this simple game, and I keep encountering a ClassCastException when trying to cast my my SurfaceView into a PuzzleSurfaceView (which extends SurfaceView).
package com.scf.android.CAPuzzle;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class CAPuzzleActivity extends Activity {
PuzzleSurfaceView puzzleSurfaceView;
View.OnClickListener puzzleClickListener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
puzzleSurfaceView = (PuzzleSurfaceView)findViewById(R.id.puzzleSurfaceView);
puzzleClickListener = new View.OnClickListener() {
public void onClick(View v) {
}
};
}
}
And the PuzzleSurfaceView class:
package com.scf.android.CAPuzzle;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
class PuzzleSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
Bitmap live;
Bitmap dead;
public PuzzleSurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
#Override
public void onDraw(Canvas c) {
c.drawBitmap(live, 0, 0, null);
}
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
live = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
}
Please advise... I've been banging my head against the wall for hours now...
My main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<SurfaceView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/puzzleSurfaceView"></SurfaceView>
<SurfaceView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="#+id/solutionSurfaceView"></SurfaceView>
</LinearLayout>
You will need to instantiate your class and add it to the layout. You can't simply cast. Casting only works when casting to the same or parent classes or interfaces. You could also make it a custom component as detailed here and use your PuzzleSurfaceView in your main.xml
You must replace one row in main.xml to following:
<com.scf.android.CAPuzzle.PuzzleSurfaceView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/puzzleSurfaceView"></com.scf.android.CAPuzzle.PuzzleSurfaceView>
and add constructor
PuzzleSurfaceView(Context context, AttributeSet attrs)
at your PuzzleSurfaceView class.