Is there away to put buttons/imagebuttons on top of the "game-surface"?
I'm experimenting with code from this blog:
https://www.simplifiedcoding.net/android-game-development-tutorial-1/
I need to change direction for objects on the screen with some buttons.
In the tutorial a ship changes direction if the touch screen is pressed anywhere.
I have tried this:
ImageButton b1 = (ImageButton) findViewById(R.id.turn_left);
in order to add the button later to the view with addView, but it returns null.
Maybe becaue setContentView(gameView); set the view of GameView and not
GameActivity?
Any suggestions on how to make this work?
public class GameActivity extends AppCompatActivity {
private GameView gameView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Getting display object
Display display = getWindowManager().getDefaultDisplay();
//Getting the screen resolution into point object
Point size = new Point();
display.getSize(size);
gameView = new GameView(this, size.x, size.y);
//adding it to contentview
setContentView(gameView);
}
GameView
public class GameView extends SurfaceView implements Runnable {
...code...
private void draw() {
//checking if surface is valid
if (surfaceHolder.getSurface().isValid()) {
//locking the canvas
canvas = surfaceHolder.lockCanvas();
//drawing a background color for canvas
canvas.drawColor(Color.BLACK);
//Drawing the player
canvas.drawBitmap(
...code...
draw);
//Unlocking the canvas
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
....code....
#Override
public boolean onTouchEvent(MotionEvent motionEvent) {
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
...code...
return true;
}
}
XML
xmlns:tools="http://schemas.android.com/tools"
tools:context="package.GameActivity">
<ImageButton
android:id="#+id/turn_left"
android:background="#drawable/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
You can create custom button class which draw bitmap through canvas.
Related
I'm trying for quite a long time to set OnTouchListener to multiple ImageViews using ArrayLists but my Application keeps on crushing down. This is my code:
MainActivity:
public class MainActivity extends AppCompatActivity {
private DragAndDrop dragAndDrop;
private InstantiateImages instantiateImages;
private ArrayList<ImageView> faces = new ArrayList<ImageView>();
private int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dragAndDrop = new DragAndDrop();
InstantiateImages instantiateImages = new InstantiateImages(this);
//Add elements to ArrayLists:
instantiateImages.Add();
//Get elements into ArrayLists:
faces = instantiateImages.getFaces();
for (i = 0; i< faces.size(); i++) {
faces.get(i).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
//Follow the touch on the ImageView and move the
//ImageView accordingly:
dragAndDrop.action(v, event, faces.get(i));
return true;
}
});
}
}
}
InstantiateImages Class:
public class InstantiateImages {
this.activity = activity;
private ArrayList<ImageView> faces = new ArrayList<ImageView>();
//Faces declarations:
private ImageView FacesNoFace;
InstantiateEmojis() {
//Faces instantiations:
FacesNoFace = (ImageView)this.activity.findViewById(R.id.faces_no_face);
}
public void Add() {
//Add faces to ArrayList faces:
faces.add(FacesNoFace);
}
//Getters:
//Get Faces:
public ArrayList<ImageView> getFaces() {
return faces;
}
}
XML of layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="myproject.myproject.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp"
tools:background="#ffffff">
<ImageView
android:id="#+id/faces_no_face"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:contentDescription=""
app:srcCompat="#drawable/faces_no_face"
tools:ignore="ContentDescription"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="16dp" />
</android.support.constraint.ConstraintLayout>
The DragAndDrop Class is a class that only has a method which follows the ImageView drag and drop (the inside/heart of the OnTouchListener) and before I tried to put the ImageView inside of the ArrayList "faces" which I created in the "InstantiateImages" Class I instentiate the OnTouchListener only on one ImageView (the one I added to the ArrayList now) and it worked totaly fine (I could drag and drop the ImageView arround the app screen). Now the Application is crushing after I Run it and it says: "MyProject keep stopping close app" or "MyProject Has stoped reopen app".
Any Help would be appreciated!!
I think you are overcomplicating parts of this. I think this will do what you're trying to do.
public class MainActivity extends AppCompatActivity {
private DragAndDrop dragAndDrop;
private final ArrayList<ImageView> faces = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dragAndDrop = new DragAndDrop();
faces.add((ImageView) findViewById(R.id.faces_no_face));
for (ImageView face : faces) {
face.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
//Follow the touch on the ImageView and move the
//ImageView accordingly:
dragAndDrop.action(v, event, (ImageView) v);
return false;
}
});
}
}
}
If you are planning on having a variable number of ImageViews that you want to listen to clicks on, you might want to look into RecyclerView and Lists.
I've been trying to draw a Canvas on an ImageView but after creating a bitmap I see no Image on my screen. The only thing I get is the small circle I've drawn in the onDraw Method. I want to have the Canvas on the ImageView.
I've got the following code:
public class MainActivity extends AppCompatActivity {
ImageView imageView;
Bitmap myBitmap;
Paint paint;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
}
public void onClickButton(View view) {
View v = new DrawCanvas(getApplicationContext());
Bitmap bitmap = Bitmap.createBitmap(48, 48, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
imageView = (ImageView) findViewById(R.id.testpicture);
imageView.setImageBitmap(bitmap);
}
}
and
public class DrawCanvas extends View {
Paint paint = new Paint();
public DrawCanvas(Context context) {
super(context);
}
public DrawCanvas(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.RED);
canvas.drawCircle(25, 25, 5, paint);
}
}
Thanks in advance!
First, you're overcomplicating things. You don't need, or want, a view to do what you're doing. If you want to draw a circle, do it directly to the canvas by using canvas.drawCircle in your onClick.
Second, alling setImageBitmap overwrites any previous bitmap you set. If you want this to show over the previous image, you have 2 options:
1)Use two image views on top of one another, and set the top image to the bitmap made on the canvas (to make this work you may need to make sure the background color on the bitmap is transparent).
2)Create a new view subclassed off ImageView that displays 2 bitmaps by overriding onDraw.
The first approach is probably easier.
I'm trying to make a bull's eye with random color, and instead of circles I will use squares.
But the thing is that when I run the app on the emulator and when he starts the new activity it stops responding.
This is the main activity, the one that starts the DrawActivity.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent coiso = new Intent(this, Draw.class);
startActivity(coiso);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And this is the Draw activity, the one that I want to start. (It doesn't have the things that I want to do. Because I can't, the problem is ahead)
public class Draw extends View {
public Draw(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
}
}
Can someone help me? Sorry for the english.
You have this
public class Draw extends View
Your class does not extend Activity
Instead you can do as below
Draw draw = new Draw(this);
setContentView(draw);
Or have a layout linear or relative and place it where you like add your Draw view to the layout after initializing.
setContentView(R.layout.activity_main);
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);
// linear layout or relative layout in activity_main.xml.
// place the layout ahere you want along with other views
Draw draw = new Draw(this);
ll.addView(draw);
// add your customview to linearlayout
Edit:
Remove this
Intent coiso = new Intent(this, Draw.class);
startActivity(coiso);
In your activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
// customize linear layout to your needs.
<LinearLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/linearLayout"
android:orientation="vertical" >
</LinearLayout>
// other widgets
</RelativeLayout>
In your onCreate
setContentView(R.layout.activity_main);
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);
Draw draw = new Draw(this);
ll.addView(draw);
startActivity requires an activity. I would suggest going through the docs
http://developer.android.com/reference/android/app/Activity.html#startActivity(android.content.Intent)
Your Draw class needs to extend Activity rather than View. As you want to start a new Activity, the Draw class, this means that this should extend Activity. Also, you need to Override onCreate() within the Draw class.
If your Draw class is a View, then I would suggest that you add the view to the Layout that you are using using the addView()
You need to make sure that you change Draw extends Activity
You cant intent to a new activity with no layout and no OnCreate As far as I know.
try creating a regular activity which extends Activity and implement your Draw there.
public class DrawActivity extends Activity {
#SuppressLint("ShowToast")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draw);
Toast.makeText(DrawActivity.this, "YO", Toast.LENGTH_LONG);
}
and from there implement your draw functions.
or create a JAVA class which implements your draw needs and use it in main screen.
I am trying to make part of my image clickable. In this case I have 24 port switch and I want when user click on a port to display the port number. I already have do the zooming and tried to insert rectangles over the image, but I am still new in the Android Development so I am not so sure how to accomplish the task.
Here is my code to create rectangles and put on the picture:
(the idea is that I have one class Rectangles that keeps the port number and some text so I can retrieve them)
public class MainActivity extends Activity{
ImageView DrawingImage;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DrawingImage = (ImageView) this.findViewById(R.id.image);
Bitmap bitmap2 = Bitmap.createBitmap((int) getWindowManager()
.getDefaultDisplay().getWidth(), (int) getWindowManager()
.getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap2);
DrawingImage.setImageBitmap(bitmap2);
// Draw Rectangle
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.FILL);
float left = 20;
float top = 20;
float right = 50;
float bottom = 100;
canvas.drawRect(left, top, right, bottom, paint);
Zoom image = (Zoom) findViewById(R.id.image);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
image.setImageBitmap(bitmap);
int posX=(int)image.getX();
int posY=(int)image.getY();
double height=image.getHeight();
double width=image.getWidth();
}
}
But when I run the App I cannot see the Rectangle. Even if i declare the picture before the rectangle I can see only the rectangle.
Any suggestions?
Any help will be appreciated.
Thank you in advance!
Best regards,
Dimtiar Georgiev
xml layout:
<FrameLayout 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">
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
onCreate Method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView DrawingImage = (ImageView) this.findViewById(R.id.imageView2);
Bitmap bitmap2 = Bitmap.createBitmap((int) getWindowManager()
.getDefaultDisplay().getWidth(), (int) getWindowManager()
.getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap2);
DrawingImage.setImageBitmap(bitmap2);
// Draw Rectangle
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.FILL);
float left = 20;
float top = 20;
float right = 50;
float bottom = 100;
canvas.drawRect(left, top, right, bottom, paint);
ImageView image = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
image.setImageBitmap(bitmap);
int posX=(int)image.getX();
int posY=(int)image.getY();
double height=image.getHeight();
double width=image.getWidth();
}
Its not about what you draw first now. It's about the xml layout. what to put first. ofcourse you may want to play with the height and width attributes for the image view to achieve what your really want. but this is working I test it.
This will not work. the way you did it.
you must have two image views in your design.
use a framelayout and put two image view inside it.
set the canvas for one of them and draw.
and set the image for the other.
it will not work because you set the first bitmap:
DrawingImage.setImageBitmap(bitmap2);
then you replace it:
image.setImageBitmap(bitmap);
you got the same image layout:
R.id.image
I'm a starter in Android Programming, and I'm developing a program by modifying some classes I found here. So far I have the DrawView class as follows:
public class DrawView extends View {
private Ball ball1;
private Button kapabut;
public DrawView(Context context) {
super(context);
setFocusable(true);
ball1 = new Ball(context,R.drawable.ortatop);
kapabut=new Button(context); //here, I cannot seem to add a button.
kapabut.setVisibility(VISIBLE);
kapabut.setText("xXx");
}
#Override protected void onDraw(Canvas canvas) {
// move the balls at every canvas draw
ball1.moveBall();
//draw the balls on the canvas
canvas.drawBitmap(ball1.getBitmap(), ball1.x, ball1.y, null);
// refresh the canvas
invalidate();
}
}
The ball is created and moves, but I cannot seem to get the button "kapabut" anywhere. How can I make this button appear, and add an onClick method too?
Any help would be appreciated, thanks.
P.S.: I tried and added a Button using an XML layout, but now I want to make it with this class, and setting setContentView(new DrawView(this)); in Main.java
You can not add other View objects in View's onDraw() method, their is no any addView() method in View class.
To get it work extend your DrawView class with ViewGroup now you can add other View in this. As addView() method belongs to ViewGroup Class.
Something like,
public class DrawView extends ViewGroup {