How to connect button actions with surfaceview canvas drawables? - android

My problem is that I don't know how to change position of a canvas drawable on click of
my button that it's located in the main activity and not to surfaceview so the code to work should be on surfaceview here is what I've got
public class JumpOnClick extends Activity implements OnClickListener{
DrawingSurface ds;
FrameLayout frm;
Button btnC;
int color=0xfff00000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ds=new DrawingSurface(this);
setContentView(R.layout.activity_jump_on_click);
frm=(FrameLayout)findViewById(R.id.frameLayout);
frm.addView(ds);
btnC=(Button)findViewById(R.id.buttonColor);
btnC.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// how to connect actions in here for my sprites on my surfaceview
// for example AnimatedSprite a = new AnimatedSprite();
// if(b == null)
//b=BitmapFactory.decodeStream(res.openRawResource(R.drawable.explosion));
// a.Initialize(b, 120, 159, 7, 20, true);
// mSprites.add(a);
}
}

if I understand correctly,
define two static float variables in your surfaceview class respectively x and y, and when you do your drawing in your surfaceview just use this variables as position parameters in your surfaceview's draw method. And then back in your activity in your onClick method of the button just change these x,y variables, as you can access them without any need to create object.

I believe that you need to add layout rules to your button to place it in the Surfaceview (Actually a button cant be added to a SurfaceView, you need to create an illusion with a Layout in your Activity and place the button to this Layout).
You can check this simple solution at this video https://www.youtube.com/watch?v=8qE9TQjQ5no&t=31s
that is placing buttons to a SurfaceView at the top and bottom using a Relativelayout. My opinion is with that technique you wont need the ontouchevent method neither the x,y position, you will need that if you create a button in the ondraw method (then wont be a button but a bitmap image)...My opinion is to stick with the first solution if the position is the problem so it can act as a button with the onclicklistener.

Related

Draw a circle partially inside on a button dynamically android

My class Triangle extends from View and its method onDraw consists of:
... onDraw(){
...
drawCircle(anyx, anyy, anyradius, anypaint);
}
And the Activity in which I want to create the Button and the Circle, both created dynamically.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newexample);
RelativeLayout viewGroup = (RelativeLayout)findViewById(R.id.visiont);
Button b1 = new Button(this);
viewGroup.addView(b1);
b1.setX(140); // Position of the button
b1.setY(140);
ViewGroup.LayoutParams params = b1.getLayoutParams();
params.height=80; // Size of the button
params.width=80;
b1.setLayoutParams(params); // Deleting this does not change the behaviour of what I am getting (only changes the height and width)
Circle c = new Circle(this);
c.setColor(Color.DKGRAY);
c.setCircle(150,130,80);
viewGroup.addView(c);
}
An example of what is the desired result, the rectangle is the button.
What I am getting now is the opposite, the button is over the circle.
I tried to switch Button to ImageButton since I wanted to have a custom image in the button (Actually I wanted a clickable component)
It appears that, with only switching Button to ImageButton, works as desired, so, for me it's a suitable solution.

CustomView not showing inside ScrollView

New Android programmer here.
I'm trying to display an .png image as a bitmap on Android. The only way I have been able to display the converted image at all is with a custom class that extends View. However, my image is too large to be displayed entirely on the screen and I would like to be able to scroll with it. But when I define a ScrollView and put the Canvas with the Bitmap into it, I get a blank screen. I had no luck setting this up with the layout file, so it is all done in the Activity class.
This is where the Activity is created:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView scroll = new ScrollView(this);
scroll.addView(new CustomView(this));
setContentView(scroll);
}
And this is my CustomView class:
private class CustomView extends View{
public CustomView(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas){
Bitmap bitmapMap = BitmapFactory.decodeResource(getResources(),R.drawable.resourceimage);
canvas.drawBitmap(bitmapMap,0,0,null);
this.setWillNotDraw(false);
}
}
And if I replace this line of code in my Activity: setContentView(scroll)
with this line: setContentView(new CustomView(this)), I can see the image, albeit not the entire image. So, is there a way to set this up in the layout files instead? Or is there something I'm missing that I need to declare in the ScrollView class?
EDIT: I would prefer not to use ImageView, because I would like to change the image in specific locations, and using bitmap seemed to be the easiest way to accomplish that, via x and y coordinates.
Your custom view needs to override the onMeasure method and properly set the measured width and height so that the parent views (in this case the ScrollView) can know how much space to allocate for the child.

Can I do addView in onDraw of MyLayout?

I am new for android. I want to add image button in onDraw. I wonder if I can do like this. I don't have compile error. But simulator says, "Unfortunately MyApp has stopped."
My second question is how can I add button at x, y location in screen?
There is no location parameters in addView.
protected class MyLayout extends LinearLayout {
ImageButton button;
public MyLayout(Context context) {
super(context);
setWillNotDraw(false);
button = new ImageButton(context);
button.setImageBitmap(buttonBitmap); // buttonBitmap is loaded in onCreate
}
public void onDraw(Canvas canvas) {
addView(button);
}
}
I would not be adding a button to a custom linearlayout in the onDraw() override function. onDraw is called periodically (for example when the element is resized). So you will be adding the same button EACH time onDraw() is called. And you will get an error if you add the same item more than once.
What exactly are you trying to do? If you want to just add a button to a linear layout, you can do so without extending a layout.

Need assistance in OpenGL in android

I've created 3 Java classes.
one that has a glsurfaceview object and this calls the renderer class.
this is the renderer class and this calls the cube class.
this is the cube class.
If I run the app then the screen shows a rotating cube (did rotation in the rendering class) which is fine. But I want to control the direction of rotation of the cube and for that I've set 2 buttons. This is where I need help because I don't know to to make the buttons control the movement of the cube. I'm new to Android so if you could leave some code for me to examine then that would be just great.
Your Activity class (or your class that extends Activity) should look like this:
public class stackoverflowTest extends Activity {
GLSurfaceView glSurface;
MyRenderer myRenderer;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myRenderer = new MyRenderer(this);
//Create an instance of the Renderer with this Activity
glSurface = (GLSurfaceView) findViewById(R.id.graphics_glsurfaceview1);
//Set our own Renderer and hand the renderer this Activity Context
glSurface.setEGLConfigChooser(true);
glSurface.setRenderer(myRenderer);
//Set the GLSurface as View to this Activity
}
/**
* this is the method the button click calls
*/
public void changeRotationDirection(View v){
myRenderer.changeRotationDirection();
}
}
Then in your renderer:
public class MyRenderer implements Renderer {
private float rotationDirection = 1.0f;
public MyRenderer(Context context){
this.context = context;
}
public void setRotationDirection(){
if(rotationDirection==1.0f){
rotationDirection=-1.0f;
} else {
rotationDirection=1.0f;
}
}
#Override
public void onDrawFrame(GL10 gl) {
// GL calls
gl.glRotatef(angle, rotateDirection, 0.0f, 0.0f);
// draw cube
gl.glDrawArrays( etc );
}
}
Basically, you use glRotatef to rotate the cube just before you draw it. Use -ve vales for either the angle parameter (the first) or the x,y,z amount parameters to rotate in the opposite direction. Use method calls to the Renderer to communicate with it and update the scene. Use this approach with caution as the Renderer thread and Main/UI thread (from where the button call is made) can have synchronisation issues
To make a button call the changeRotationDirection method, simply add android:onClick="changeRotationDirection" into the XML layout (of any view. Doesn't have to be just a button view). Any button methods declared in the XML layout have to be of the form public void [methodname](View [paramname]) and have to be in the Activity class from where the button is pressed
For more advanced touch control, check as Erik suggested and also check out OnTouchListeners
(Note: if you're using openGL-ES 2.0 or above (android 2.2+) then use GLES20.glRotatef())
You might want to check out the TouchRotateActivity example from the sdk. If I'm not mistaken its located in the samples/platform/api-demos folder.

How to add view components dynamically from activity

I am attempting to create a user interface dynamically. I have successfully create a view and loaded my background image. I have created two additional small view items to display on the background. My problem is that I have not been able to find any advice/instruction that tells me how to draw the small views. It seems that it should be a trivial exercise and I am guessing it is just finding the correct referencing. Hope someone out there can point me in the right direction.
Here is my Activity:
public class GhostActivity extends Activity implements OnTouchListener
{
private DrawView ghostView;
public Card mCard1, mCard2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
// ToDo add your GUI initialization code here
super.onCreate(savedInstanceState);
// requesting to turn the title OFF
requestWindowFeature(Window.FEATURE_NO_TITLE);
// making it full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
ghostView = new DrawView(this);
setContentView(ghostView);
//get the window size
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Context context = getApplicationContext();
//create view items with initial positions
Point startPoint;
startPoint = new Point();
startPoint.x = 5;
startPoint.y = 3;
mCard1 = new Card(context, 1, R.drawable.bol_geel, startPoint);
startPoint.x = 5;
startPoint.y = 43;
mCard2 = new Card(context, 2, R.drawable.bol_rood, startPoint);
//now display them on the ghostView *****************HOW?
// set the callbacks
ghostView.setOnTouchListener(this);
mCard1.setOnTouchListener(this);
mCard2.setOnTouchListener(this);
}
and here is the View;
public class DrawView extends View
{
Drawable bg ;
public DrawView(Context context) {
super(context);
//setFocusable(true);
Drawable bg = this.getResources().getDrawable(R.drawable.bubbleblue480x800);
setBackgroundDrawable(bg);
}
#Override protected void onDraw(Canvas canvas) {
// canvas.drawColor(0x0000000); //if you want another background color
//draw on the canvas
}
}
edit: I believe my problem is needing to pass a pointer to the ghostView canvas. what makes me think that is if I create the children within ghostView then call their .draw method they appear exactly as I would expect.
#Override protected void onDraw(Canvas canvas) {
canvas.drawColor(0x0000000); //if you want another background color
//draw the cards on the canvas
mCard1.draw(canvas);
mCard2.draw(canvas);
}
so at this point I am wondering how to get a reference pointer to the ghostView canvas.
To be honest I am finding the whole Activity - View relationship confusing.
Edit: I have taken a different approach based on detail in this tutorial
http://www.kellbot.com/2009/06/android-hello-circle/
It uses a FrameLayout and it seems I can achieve my objective.
To add view dynamically to view your class must extends from ViewGroup or LinearLayout class then you will able to call method addView.
Inside your ghost view first add a layout e.g Linear or Relative. Then only you could able to add views inside that layout you cant simply add a view to a xml file.
Or you can create a dynamic layout then only u can add view inside that layout.
RelativeLayout relative= new RelativeLayout(findViewById(R.id.your relativeLayoutID));
relative.addView(child);
child could be anything button textview and widget.

Categories

Resources