How can I animate multiple buttons simultaneously? - java

I am trying to do a Translate Animation on multiple ImageViews(Buttons) simultaneously. The TranslateAnimations however, vary for each ImageView, so I am putting them in an AnimationSet. The problem is, that somehow the animation is never even run... I don't know why. Here is my code:
ArrayList<TranslateAnimation> animlist = new ArrayList<TranslateAnimation>();
AnimationSet set = new AnimationSet(true);
//The following for-loop is actually running inside two other for loops... I'm skimming it down a little for you guys
for(int i = ROWS-1; i > row;i--){
if(!usedFields[column][i]){
ImageView iv = (ImageView)GameLayout.findViewWithTag(""+row+","+column);
TranslateAnimation transanim = new TranslateAnimation(0,0,-(i-row)*letterHeight,0);
transanim.setDuration(1000);
iv.setAnimation(transanim);
animlist.add(transanim);
break;
}
}
for (TranslateAnimation anim : animlist){
set.addAnimation(anim);
}
set.startNow();
Thank you, I appreciate it!

Related

CardView and LinearLayout with TextView children not displaying at all

I've been trying to write some dynamically generated layout code for a simple app I came up with. I want to display a vertical row of cards, each on containing an undefined number of vertically aligned text boxes.
I wrote the code to generate these and populate the text, but it doesn't appear to be working and I can't figure it out for the life of me.
I'm new to Android Studio, and Java is still relatively fresh to me as well, so I could well be missing something quite obvious here.
I've tried using a few different types of View in A. Studio, and so far most work by themselves, but none can be contained within a card which would be ideal for me. Dynamically creating and editing properties of textViews works fine, but once I include the card view they no longer appear using the exact same code.
//Define Params
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(left,top,right,bottom);
//Add a card for each ingredient
for (Ingredient ing : ingredients)
{
CardView card = new CardView(this);
CardView.LayoutParams cardParams = new CardView.LayoutParams(CardView.LayoutParams.WRAP_CONTENT, 200);
card.setLayoutParams(cardParams);
card.setRadius(15);
card.setPadding(25,25,25,25);
card.setElevation(10);
card.setMaxCardElevation(30);
card.setBackgroundColor(Color.DKGRAY);
//Make a grid for each card, text on the left, image on the right
LinearLayout linearLayoutInCard = new LinearLayout(card.getContext());
LinearLayout.LayoutParams layoutParamsInCard = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayoutInCard.setLayoutParams(layoutParamsInCard);
card.addView(linearLayoutInCard);
for(int x = 0; x < 3; x++)
{
TextView textView = new TextView(this);
textView.setLayoutParams(params);
textView.setPadding(left, top, right, bottom);
textView.setTextSize(15);
textView.setElevation(11);
textView.setTextColor(Color.WHITE);
linearLayoutInCard.addView(textView);
switch (x)
{
case 0:
textView.setText(ing.name);
break;
case 1:
textView.setText(ing.price);
break;
case 2:
//textView.setText(ing.calories);
break;
}
}
I'm expecting a vertical row of cards with text boxes vertically aligned withing them, each with their own content (this whole script will only make one card for now, but that's a data driven thing) yet when I run the application, I get nothing but a blank screen.
Before venturing much further...this could be an example of the XY Problem.
Maybe have a look at the RecyclerView option?
RecyclerViews are entirely designed to manage the UI look and responsiveness of changing/scrolling data sets.
They can be a bit "what the heck" at first...but once writing them a few times, your UI look and code base is a lot more efficient and clean.
RecyclerView example Youtube

Perform action for certain amount of time JavaFX

I am making a game and I would like to play an intro picture for 3 seconds and then call the methods to start the game. I have tried using Thread.sleep but I know thats probably not a great way and it didnt work. Anyone know a good way to do this?
Here is my code:
ImageView intro = new ImageView(new Image("/Resources/Intro.png"));
root.getChildren().add(intro);
if (startGame) {
InitializeGame.InitGame();
InitializeGame.StartGame();
}
}
Thanks!
Use a PauseTransition. It's not clear from your code what parts you want to execute after the pause, but something like:
ImageView intro = new ImageView(new Image("/Resources/Intro.png"));
root.getChildren().add(intro);
PauseTransition pause = new PauseTransition(Duration.seconds(3));
pause.setOnFinished(e -> {
InitializeGame.InitGame();
InitializeGame.StartGame();
});
pause.play();

How can I dynamically achieve a multiple image layering effect?

Here's a picture of what I want to do:
where all the pictures are different and I could have between 2-10.
The code I have now is:
(flag is # of pictures)
(picUris[] is my array of Uri's)
RelativeLayout imgLayout = (RelativeLayout) findViewById(R.id.RelativeLayoutPictures);
for (int i = 0; i < flag; i++)
{
ImageView iv = new ImageView(this);
iv.setImageURI(picUris[i]);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
lp.setMargins(50*i, 50*i, 0, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
imgLayout.addView(iv, lp);
}
and the result:
The problems are that it's getting resized and doesn't get cut off by the screen like I want, and there's something weird going on at the bottom.
Why not using the Stackview? basically it will achieve the exact smae result without making you creating the view progrimatically....you don't have to re-invent the wheel...
where all the pictures are different and I could have between 2-10
I think you should use LayerDrawable and offsetting each layer by for example setLayerInset. Or creating all of the background in xml and set it to your viewgroup. I do not recommend you use FrameLayout and RelativeLayout because they both use much RAM in your situation.

Java Android - Making Array of images (same image)

So I've tried my best but I have not found anything close to what I would like. I would like to know if this is possible without making 50 instances in my xml file. What I think i've come close to though:
ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();
for(int i=0;i<50;i++){
bitmapArray.add(Bitmap.createBitmap(Bmp1,(int) image.getX(),(int) image.getY(), bullpick.getWidth(), bullpick.getHeight()));
bulletArray[i].setImageBitmap(bitmapArray.get(i));
}
I've also tried making an ImageView array of one imageView but that would only leave me with one imageview. Any help is much appreciated!
Edit: For anyone who doesn't know what I'm talking about, i'm trying to make an array of 50 imageviews of the same image, but anything that would end up with an array of 50 images would be what I'm after. Thanks!
Put your image in drawable folder.
Then create a simple loop:
ArrayList<ImageView> imageViews= new ArrayList<ImageView>();
for (int i = 0; i < 50; i++) {
ImageView imageView = new ImageView(getApplicationContext());
imageView.setImageResource(R.drawable.yourimage);
imageViews.add(imageView);
}
This would create an arraylist of 50 imageviews.

Android ui element animation

I'm trying to animate UI elements. I would like to move an editText and a Button from the middle to the top of the screen and display results of an http call below them in a table. It would be great if anyone could point me in the right direction, at this point I don't know wether I should use Java or XML for this.
Thanks in advance.
Use Translation framework to achieve this, this works as:
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
So you need to write your code for moving view in y-axis direction, as follows:
mAnimation = new TranslateAnimation(0, 0, 0, 599);
mAnimation.setDuration(10000);
mAnimation.setFillAfter(true);
mAnimation.setRepeatCount(-1);
mAnimation.setRepeatMode(Animation.REVERSE);
view.setAnimation(mAnimation);
Here view may be anything, textview, imageView etc.
accepted answer caused an error inside my code, code snippet below almost identical to accepted answer & worked without causing errors, to slide an object off the screen. i needed gestures tied to keyPad to also 'slide away' , and switched from TranslateAnimation to ObjectAnimator (second block of code below).
final LinearLayout keyPad = (LinearLayout)findViewById(R.id.keyPad);
moveKeyPadUp(keyPad);
private void moveKeyPadUp(LinearLayout keyPad){
Animation animation = new TranslateAnimation(0,0,0,-500);
animation.setDuration(1000);
animation.setFillAfter(true);
keyPad.startAnimation(animation);
}
private void moveKeyPadUpdated(LinearLayout keyPad){
ObjectAnimator mover = ObjectAnimator.ofFloat(keyPad,"translationY",0,-500);
mover.setDuration(300);
mover.start();
}

Categories