view.setOnClick() during animation not working - java

I'm new in android development and have now problem and can not understand why and solve it.
Sorry, here is my idea and hope it helps to understand my question:
I would like to develop an app for the kids. I have two car roads. On both roads drive different cars with a certain number (0-10). The cars drive from left to right with the help of animation. A question with a number is asked. The child has to click on a certain car with the number x.
Below I've imageview (imgCarUp1) and try to animate from left to right. During animation I want to make click-event on my image and should run some code.
`
imgCarUp1 = findViewById(R.id.imgCarUp_1);
imgCarUp1.setImageResource(vehicleUpStreetList.get(0).getResId());
imgCarUp1.setTag(vehicleUpStreetList.get(0).getResId());
imgNumberUp1.setOnClickListener(view -> {
Log.d(TAG, String.format("setOnClickListener Tag: %s", view.getTag()));
...
});
TranslateAnimation animation = new TranslateAnimation(0, width - 50, 0, 0);
Animation.AnimationListener animL = new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
LogUtils.i(TAG, "onAnimationEnd is stopped!");
}
};
animation.setAnimationListener(animL);
animation.setDuration(15000);
animation.setRepeatCount(5);
animation.setRepeatMode(1);
animation.setFillAfter(true);
imgNumberUp1.startAnimation(animation);
`
Aninmation is working well. But if I try to click on imageview (imgNumberUp1) nothing will work. But if I click on area where animation was started, setOnClickListener works. I should be able to make click during animation. What is here my problem? Can someone please help with code?
Thanks a lot for helping.
Tried to search in internet, without any solution yet.

You are using Translate animation. Translate Animation translates the views position to the animated positions and wont actually change the views original position.
So you will need to change X and Y value to change the original position and then handle clicks wherever the animation is currently positioned
you can do .
view.animate().X().y().start()
while x and y will have the params of where you want to animate

Related

Switching between HorizontalScrollView and BarChart scroll

I have a MPAndroidChart BarChart which is placed inside a HorizontalScrollView. Now, I have multiple bars in the chart view and also the chart width is fixed as per my requirement. Since, there is a default scroll behaviour of chart, I can see all the bars.
The only problem is the scroll behaviour is not smooth and it lags a lot.
My idea is to disable the HorizontalScrollView once I start scrolling the chart and enable it back when I touch outside the chart. Can someone please tell me how I can do it? I hope my question is clear enough and there is no need to share any XML for it. Thanks in advance.
I faced similar issue while using Mpchart inside horizontal scroll view.
I had a workaround with MpChart setOnChartGestureListener.
barChart.setOnChartGestureListener(new OnChartGestureListener() {
#Override
public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
horizontalScrollView.requestDisallowInterceptTouchEvent(true);
}
#Override
public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
horizontalScrollView.requestDisallowInterceptTouchEvent(false);
}
//.....//
#Override
public void onChartTranslate(MotionEvent me, float dX, float dY) {
Log.i("GESTURE", "onChartTranslate");
if(barChart.getLowestVisibleX() == barChart.getXAxis().getAxisMinimum() || barChart.getHighestVisibleX() == barChart.getXAxis().getAxisMaximum()) {
horizontalScrollView.requestDisallowInterceptTouchEvent(false);
} else {
horizontalScrollView.requestDisallowInterceptTouchEvent(true);
}
}
});
the horizontalScrollView object is where the chart reside.

libgdx how to access power button of android device?

I just want to check that the power button is pressed or not in LibGDX and if it is pressed then exit/close the game or something else like change screen.
I used this code:
#Override
public void render(SpriteBatch sb) {
sb.begin();
if(Gdx.input.isKeyPressed(Input.Keys.POWER)){
Gdx.app.exit();
}
sb.end();
}
but this is not working. After I press the button screen turns off and when I turn it back on the game resumes from exactly where I left it.
I wanted to keep the screen on on power button press but I didn't find any solution on that.
Now I at least want to access the button and then do something on button press.
the same code works for volume up/down button.
Code:
#Override
public void render(SpriteBatch sb) {
sb.begin();
if(Gdx.input.isKeyPressed(Input.Keys.VOLUME_UP)){
Gdx.app.exit();
}
sb.end();
}
Please describe if I'm doing anything wrong and what shall I do.
You can do all saves in pause method that is available in Screen and ApplicationListener classes. It will be called when you exit the app or block device.
#Override
public void pause () {
// save here
}

Delay when changing screen in libGDX

I'm new member in libGDX . i want delay screen when changing screen
Here my code
myStage.addAction(Actions.sequence(Actions.delay(1), Actions.run(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
game.setScreen(new GameOverScreen(game,MYSCORE));
}
})));
But the game not change the screen which i want. It stop at current screen . sorry about my bad languge . How can I fix it. thanks all.
Depending on when you want it to be done, you need to call:
myStage.act();
And an example to get the output your looking for would be:
if(player.isDead()) {
myStage.act();
}
But note, this is not an effective way to transition screens.

libgdx clickable image not working

I have a menu screen in libgdx and I had a text button that started a new game like this.
textButton.addListener(new ChangeListener() {
public void changed (ChangeEvent event, Actor actor) {
g.setScreen( new level1(g));
}
});
It looked like crap so I changed it to an image.
playbuttontexture = new Texture(Gdx.files.internal("data/playbutton.png"));
playbuttontexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion playbuttonregion = new TextureRegion(playbuttontexture, 0, 0, 512, 256);//powers of 2
playbutton = new Image(playbuttonregion);
playbutton.setSize(512,256);
playbutton.setBounds(width/2-playbutton.getWidth()/2, height/2-playbutton.getHeight()/2, 512, 256);
//playbutton.setOrigin(playbutton.getWidth()/2, playbutton.getHeight()/2);
playbutton.setPosition(width/2-playbutton.getWidth()/2, height/2-playbutton.getHeight()/2);
and
playbutton.addListener(new ChangeListener() {
public void changed (ChangeEvent event, Actor actor) {
g.setScreen( new level1(g));
}
});
Now when I click it nothing happens? What am I doing wrong?
The problem here is that Image does not fire a changed(...) event anymore. This event is only fired by the TextButton you've used before when the status changes from clicked to not-clicked and the other way around. It can also be fired in other cases, since it is kind of a "generic" event, as the JavaDoc states, but that varies from actor to actor.
Change it to a ClickListener and use the clicked(...) method instead. This event should be fired by all actors in the scene2d.ui package.
for me this is what it looks like when I implemented the code ( I was having a similar issue. noone did a great job at pointing me to the right places to look. )
playbutton.addListener( new ClickListener(){
#Override
public void clicked (InputEvent event, float x, float y) {
//your code to do stuff when the button is clicked
}
});

many focused pictures with one focusing in android?

I'm making an Android application that should take pictures with an interval of one second. The problem is that if you focus the picture before each picture, intervals get too long. Camera.autoFocus() seems to take a little over a second at best and over three seconds at worst, depending on lighting and other things. As far as I know there isn't a way to control focus distance manually in Android and FOCUS_MODE_FIXED doesn't fit my application because I'm taking pictures at a very short distance.
Is there a way to focus the picture once and then take many pictures with the same focus?
I have been trying to do it but the focus always disappears after a picture is taken. developer.android.com says:
"Stopping preview with stopPreview(), or triggering still image
capture with takePicture(Camera.ShutterCallback,
Camera.PictureCallback, Camera.PictureCallback), will not change the
the focus position. Applications must call cancelAutoFocus to reset
the focus."
Here is how I take pictures now. I removed some unrelevant lines from it. This is all working, but too slow. I have Sony Ericsson Xperia Mini and Android 2.3.4.
AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback(){
public void onAutoFocus(boolean arg0, Camera arg1) {
try {
arg1.takePicture(null, null, mPicture);
amountOfPictures++;
arg1.startPreview();
} catch (Exception e) {}
}
};
private PictureCallback mPicture = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
mCamera.autoFocus(myAutoFocusCallback);
//write picture to file
//leave cameraActivity when enough pictures written
}};
//this is run before the first picture
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
mCamera.autoFocus(myAutoFocusCallback);
Thread.sleep(500);
} catch (Exception e){}
}
}
edit:
It seems that using FOCUS_MODE_CONTINUOUS_VIDEO solved the problem for now. Means that it's focusing all the time. I wonder how it didn't come to be before. But I think the focusing isn't as good as when using FOCUS_MODE_AUTO or FOCUS_MODE_MACRO.
EDOF and CONTINUOUS_PICTURE are not options because this phone doesn't support them.
I would still be happy if someone knew answer to the original question.

Categories

Resources