So I am making a scrolling platformer game, and I want the player to jump when the screen is tapped. However, I have tried many many answers on Stack overflow such as android:onClick,
ImageButton imgButton;
imgButton = (ImageButton)findViewById(R.id.imageButton);
imgButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Gameview", "clicked");
}
});
and I suspect it does not work due to some sort of interaction between the gameView hiding the base screen and being displayed over it. How can I detect taps in my gameView or in general for game purposes?
Any help is much appreciated :D pulling my hair out over this
Related
I am making an app which uses recyclerview, database and cardview to show the elements. I want to add a feature which allows the user to select the colour of each card they add. For instance, there red or green. Since I am just a beginner I do not know how to implement this feature. Any help would be appreciated! Thank you!
add below code in onBindViewHolder method
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (holder.itemView.getBackgroundTintList().equals(ColorStateList.valueOf(Color.RED))){
holder.itemView.setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
}else {
holder.itemView.setBackgroundTintList(ColorStateList.valueOf(Color.RED));
}
}
});
this code change the color of each cardView by clicking.
I know this question has been asked many times before and there has been no solution. But with the new Camera2 API, I think so this is possible to do and I did not find any sample code/solution.
I am trying to capture the images from two cameras on Huawei P20. It has a total of 4 cameras (3 rear, 1 front). I am using one normal camera and one wide angle one. I am able to capture pictures from two cameras using the activity switch i.e. have a button to switch to another activity and then open the camera using a different ID there and then capture the picture and then use the button and shift to main activity and so on.
I have two activities one for wide angle and one for the normal camera. I have two buttons for each activity. One for capturing the picture and one for switching between activities.
So, this is how it looks like on MainActivitiy.java.
textureView = findViewById(R.id.texture);
clickBtn = findViewById(R.id.captureBtn);
nextBtn = findViewById(R.id.depthBtn);
clickBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
takePicture();
}
});
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
refreshCamera();
Intent i = new Intent(MainActivity.this, WideCamera.class);
startActivity(i);
}
});
The refreshCamera() is for releasing the camera resource. This works fine. However, I am unable to show the preview side-by-side. Is there any way I can show the preview side-by-side and capture images without having to switch activities? Is it even possible?
Thanks.
I'm new to java and i'm struggling with this for a long time. I'm making a soundboard and whenever a cardview is pressed i want it to play a sound and change the background color of the cardview for the duration of the audio. How can i make this happen? Playing the sound and changing the color are already working but i don't know how to set a duration or something. Can anyone help me out?
final MediaPlayer bingoMediaPlayer = MediaPlayer.create(this, R.raw.bingo);
final CardView bingo = (CardView) this.findViewById(R.id.play_bingo);
bingo.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
bingoMediaPlayer.start();
bingo.setCardBackgroundColor(Color.parseColor("#FF6F00"));
Toast.makeText(MainActivity.this,"kekkkk", Toast.LENGTH_SHORT).show();
}
});
Reset the color when the audio is completed. For this you could use the OnCompletionListener.
bingoMedia.setOnCompletionListener {
bingo.setCardBackgroundColor(Color.parseColor("your_default_color"))
}
You can find the original documentation here: MediaPlayer.OnCompletionListener
I am creating a basic camera app as a small project I'm doing to get started with Android development.
When I click on the button to take a picture, there is about a 1-second delay in which the preview freezes before unfreezing again. There is no issue with crashing - just the freezing issue. Why is this happening and how can I fix it?
Below is the method where the camera is instantiated, as well as my SurfaceView class.
private void startCamera() {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
cameraPreviewLayout = (FrameLayout) findViewById(R.id.camera_preview);
camera = checkDeviceCamera();
camera.setDisplayOrientation(90);
mImageSurfaceView = new ImageSurfaceView(MainActivity.this, camera);
cameraPreviewLayout.addView(mImageSurfaceView);
ImageButton captureButton = (ImageButton)findViewById(R.id.imageButton);
captureButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
camera.takePicture(null, null, pictureCallback);
camera.stopPreview();
camera.startPreview();
}
});
}
public class ImageSurfaceView extends SurfaceView implements
SurfaceHolder.Callback {
private Camera camera;
private SurfaceHolder surfaceHolder;
public ImageSurfaceView(Context context, Camera camera) {
super(context);
this.camera = camera;
this.surfaceHolder = getHolder();
this.surfaceHolder.addCallback(this);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
this.camera.setPreviewDisplay(holder);
this.camera.startPreview();
this.camera.setDisplayOrientation(90);
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
EDIT: There is currently nothing in the pictureCallback.
Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
}
You don't need to call stopPreview() after takePicture(). And you don't need startPreview() on the next line. You do need startPreview() inside your onPictureTaken() callback (not in onClick() as in the posted code!!) if you want live preview to restart after the picture is captured into a Jpeg stream.
To keep your UI responsive while using camera, you should do all work with the camera on a background thread. But it is not enough to call Camera.open() or Camera.close() on some background thread. You must create a Handler thread and use it for Camera.open(). The same Looper will be used for all camera callbacks, including PictureCallback.onPictureTaken(). See my detailed walkthrough about the use of HandlerThread.
As I explained elsewhere, you can achieve even better performance if you use the new camera2 API on devices that fully support this API (but better use the old API with devices that provide only LEGACY level of camera2 support).
But if you want to get maximum from the camera ISP, this kind of freeze may be inevitable (this depends on many hardware and firmware design choices, made by the manufacturer). Some clever UI tweaks may help to conceal this effect.
You’ll need to enable access to the hidden “Developer options” menu on
your Android phone. To do that, simply tap the “About phone” option in
Settings. Then tap “Build number” seven times and you’re done. Now you
can just back out to the main Settings menu and you’ll find Developer
options somewhere near the bottom of the list.
==>Now that you’re done with that part, let the real fun begins. Tap the new Developer options menu you just enabled and scroll until you
see the following three settings (note that they may be located within
an “Advanced” subsection):
Window animation scale Transition animation scale Animator animation
scale
==>Did you see them? By default, each of those three options is set to “1x” but tapping them and changing them to “.5x” will dramatically
speed up your phone. This harmless tweak forces the device to speed up
all transition animations, and the entire user experience is faster
and smoother as a result
I have this app where when it navigates to another activity it does a transition animation. Now I have already got that working properly but, to get it to work i have to turn the animation setting on my phone on. I have been searching on Google and I can"t seem to find solution to my problem. So my problem is this,is it possible to turn this setting on from within the app, with a code, instead of having to manually do it?
Here is the code but as i have said above this works perfectly and I'm using a Telstra next G Huawei phone, just want to no if i can code it so i can turn the animation setting on if i decide to put it on a different phone.
//Button to restart from beginning:
//Declaring the button for OnClickListener and EditText to send data:
private void Restart() {
Button Next = (Button)findViewById(R.id.restart3);
Next.setOnClickListener(new View.OnClickListener() {
//The action the button takes when clicked:
#Override
public void onClick(View v) {
//Goes back to the start of app:
Intent i = new Intent(Height.this, Page_1.class);
//clears the stack of all Activities that were open before this one so that they don't stack up:
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
//The transition animation when going onto next page:
overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
// to end the current activity:
finish();
}
});
}