onDrawFrame not being called on Android - java

I am having a problem where the renderer object doesn't seem to get its onDrawFrame called at all. The debugger never hits a breakpoint inside the function. Thus, my square isn't drawing. Here is the the code, let me know if you need anything else:
public class renderer implements GLSurfaceView.Renderer {
Square square;
public void onDrawFrame(GL10 unused) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
square.Draw();
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
}
public void onSurfaceCreated(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
square = new Square(5, 5);
}
The main activity is:
public class gameActivity extends Activity {
/** Called when the activity is first created. */
private GLSurfaceView mGLView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
staticHolder.context = getApplicationContext();
mGLView = new GLSurface(this);
setContentView(mGLView);
}
#Override
protected void onPause() {
super.onPause();
// The following call pauses the rendering thread.
// If your OpenGL application is memory intensive,
// you should consider de-allocating objects that
// consume significant memory here.
mGLView.onPause();
}
#Override
protected void onResume() {
mGLView.onResume();
}
class GLSurface extends GLSurfaceView
{
renderer r = new renderer();
public GLSurface(Context context)
{
super(context);
setEGLContextClientVersion(2);
setRenderer(r);
setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
}
}
}
Currently the screen is just black. Is there any idea of why the openGL is not rendering properly?

Ok, this was really stupid but there was no problem to begin with. The issue is that Eclipse/Java don't care about ambiguity like C# and other languages do (correct me if im wrong though). The issue was that I managed to duplicate the same class in different locations, with one updated and the other not. The end result was that it was grabbing the first one it could find.
Lesson learned, look out for ambiguity yourself because the compiler/parser will not tell you!

Related

Invalidate() method on onDraw in android

I'm doing a project in android studio. I just want to do a flashing light point using onDraw() and invalidate() but something is wrong.
This is the first class
public class flashingPoint extends View {
private ShapeDrawable mParteDibujable;
public flashingPoint(Context context){
super(context);
final Handler bridge = new Handler();
Thread time = new Thread() {
public void run() {
bridge.postDelayed(this, 1000);
invalidate();
}
};
time.start();
}
#Override
protected void onDraw(Canvas canvas) {
mParteDibujable = new ShapeDrawable(new OvalShape());
mParteDibujable.getPaint().setColor(0xff74AC23);
mParteDibujable.setBounds(10, 20, 80, 80);
mParteDibujable.draw(canvas);
//invalidate();
}
And then the main class:
public class MainActivity extends AppCompatActivity {
private ShapeDrawable mParteDibujable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout mLinearLayout = new LinearLayout(this);
flashingPoint myView = new flashingpoint(this);
mLinearLayout.addView(myView);
setContentView(mLinearLayout);
}
}
If you have the timer, you neither need nor want the invalidate in onDraw. Invalidating in onDraw is both logically weird and will lead to poor results- it would either be ignored, or it would cause an immediate redraw. Neither is desired.
Also, you can't invalidate on a Thread, you'd need to use postInvalidate. And your thread is wrong- either don't use a thread, use a Runnable and post it to the Handler, or the thread should infinitely loop, not return. Preferably the first, there's no reason to have a thread here at all.

Android - How to call method in custom View from Main Activity?

I have created a custom View that will display a circle (the idea is that the user will be able to interact with this "ball" in various ways)
From my main activity class, I want to adjust some of the "ball's" properties, in this case change its color.
My problem is that nothing happens (no errors either, the app runs but doesn't do what I want) when I try to call the various methods from my MainActivity class, but if I do it from CircleView class, it works (for example changing the color upon touch)
Here is my custom View class (CircleView.java):
public class CircleView extends View {
private int circleColor = Color.GREEN;
private Paint paint;
public CircleView(Context context) {
super(context);
init(context, null);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_SCROLL:
this.circleColor = setRandomColor();
invalidate();
break;
case MotionEvent.ACTION_DOWN:
this.circleColor = setRandomColor();
invalidate();
break;
}
return super.onTouchEvent(event);
}
public CircleView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
paint = new Paint();
paint.setAntiAlias(true);
}
public void setCircleColor(int circleColor) {
this.circleColor = circleColor;
invalidate();
}
public int setRandomColor() {
Random random = new Random();
int randomColor = Color.argb(255, random.nextInt(), random.nextInt(), random.nextInt());
return randomColor;
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//...
//someXvalue, someYvalue, someRadius are being set here
//...
paint.setColor(circleColor);
canvas.drawCircle(someXvalue, someYvalue, someRadius, paint);
}
}
And here is my MainActivity.java class:
public class MainActivity extends Activity implements
GestureDetector.OnGestureListener {
private GestureDetectorCompat mDetector;
CircleView circle;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDetector = new GestureDetectorCompat(this, this);
circle = new CircleView(this);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
this.mDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
#Override
public boolean onDown(MotionEvent event) {
return true;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent event) {
circle.setCircleColor(circle.setRandomColor(0));
circle.invalidate();
return true;
}
}
I am new to Android development, and Java as well. I realize it could be something with the Context, which is something I have not fully understood yet. Could also be something with the TouchEvents. I am sure that someone out there can see my mistake. Any help is appreciated.
your circle view is not a part of activity's layout , it's just a object in memory which has no link to your activity screen so solutions
1.) Either set circle as Activity's view
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDetector = new GestureDetectorCompat(this, this);
circle = new CircleView(this);
setContentView(circle);
}
2.) you can create your <yourpackagename.CircleView ...attributes .../> tag in your activity_main.xml and then use findViewById to initialize it in your activity.
1)If all you want to do with gestures is on tap, just implement an onClickListener on your View instead.
2)You aren't actually using the GestureDetector anywhere. The way it works is you set an onTouchListener for the view you want to get gestures on, and send the events to the gesture detector. You aren't ever sending data for any view to the detector, so it will never do anything.
3)Not a bug just an oddness- why circle.setColor(circle.setRandomColor())? I would expect a function named setXXX to actually set XXX, rather than having to do it yourself later. Not following that convention will work, but make debugging and maintenance hard.
Edit: Also what #Pavneet_Singh said- your circle isn't in your layout anywhere, so it won't be on screen.

Using onSizeChanged in Drawable class

I have created a custom class which extends Drawable. I'm trying to get the dimensions of the Drawable by using onSizeChanged() as follow
public class Circle extends Drawable {
...
#Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld) {
super.onSizeChanged(xNew, yNew, xOld, yOld);
}
}
But I'm getting an error saying that "Method does not override from the superclass". What do I do to fix it?
Thanks a lot to Michael Spitsin for this answer. To get the dimensions of the layout which the drawable is attached to, use the following code
#Override
protected void onBoundsChange(Rect bounds) {
mHeight = bounds.height();
mWidth = bounds.width();
}
If we go to View source code and in Drawable source code, we will see next things:
In View.setBackgroundDrawable(Drawable) if we pass not null, than field mBackground (is is responsible to store background drawable) is updated.
If we will try to find usages of method Drawable.onBoundsChanged() then we will see that it is mainly used in Drawable.setBounds method. And if we will find usages of it, we will see next snippet in View.class:
private void drawBackground(Canvas canvas) {
final Drawable background = mBackground;
if (background == null) {
return;
}
if (mBackgroundSizeChanged) {
background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
mBackgroundSizeChanged = false;
rebuildOutline();
}
//draw background through background.draw(canvas)
}
Thus, using of onBoundsChanged callback is accepted for your task.

Once using the override method and once more using without the override method and two different result

The answer for this question was useful for me. But I have a new question about that. I read the Java grammar but I have problems in some part of the Java grammar and for this reason, I ask my questions here. I changed two part of the code link as the following:
In Draw.java:
I changed the public void onDraw(Canvas canvas) To the public void d(Canvas canvas)
And I added the following in MainActivity.java
public class MainActivity extends Activity {
Draw draw;
Cal cal;
TextView textView;
RelativeLayout linearLayout;
Canvas canvas;
public void onCreate(Bundle s) {
super.onCreate(s);
setContentView(R.layout.activity_main);
linearLayout = (RelativeLayout) findViewById(R.id.t);
cal = new Cal(this);
cal.cal();
textView = new TextView(getApplicationContext());
textView.setText("" + cal.result);
textView.setTextColor(Color.RED);
draw = new Draw(this);
draw.d(canvas);
linearLayout.addView(textView);
linearLayout.addView(draw);
}}
the code can compile and install successfully.But only it can run for a short time in my device and also it can not run in AVD manager.
I'm sure the grammar used is correct.But I do not know what is the cause of the collision in the code that I can not see the output Code correctly.
UPDATE, Draw.java
public class Draw extends View {
Paint paint = new Paint();
Draw(Context context) {
super(context);
}
public void d(Canvas canvas) {
paint.setColor(Color.BLUE);
canvas.drawCircle(120,120,40,paint);
}
}
I don't try it, maybe it's stupid but try to add public front of the Draw constructor:
**public** Draw(Context context) {
super(context);
}
And in d() method, there should be one less parenthesis.

How to animate a Drawable in Android?

I have a Drawable object in my application which is moving on the screen:
public class MyDrawable extends Drawable {
#Override
public void draw(Canvas canvas) {
// ...
}
// ...
}
It worked fine so far but now I have to animate this Drawable. I checked out the documentation and it looks like that the best option for me is using AnimationDrawable.
I did something like this:
public void addAnimation() {
animation = new AnimationDrawable();
animation.addFrame(resources.getDrawable(R.drawable.anim_0), 100);
animation.addFrame(resources.getDrawable(R.drawable.anim_1), 100);
animation.addFrame(resources.getDrawable(R.drawable.anim_2), 100);
animation.addFrame(resources.getDrawable(R.drawable.anim_3), 100);
animation.setOneShot(false);
refreshAnimationBounds();
animation.start();
}
#Override
public void draw(Canvas canvas) {
refreshAnimationBounds();
animation.draw(canvas);
}
What happens is that my original Drawable is moving as intended but I only see the first frame. What could be the problem?
refreshAnimationBounds(); is just refreshing the bounds of my Drawable object (repositions it).
Edit:
The common:
public void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus) {
animation.start();
} else {
animation.stop();
}
}
solution is not working.
In general Animation package is quite buggy. I recall I had same problem with animation not starting and the solution I came with was to start it not in onCreate() as I formerly did but onWindowFocusChanged() once all view stuff was set. I wasn't extending AnimationDrawable myself but it may be the same origin in your case.

Categories