moving Image View from point to another - java

I would like to move an image view from one point to another when a particular button is clicked.
Here's my code:
int e = 0 ;
Button d = (Button)findViewById(R.id.button1);
d.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TranslateAnimation move = new TranslateAnimation(e, e + 1, e, e + 1);
move.setDuration(1000);
move.setFillAfter(true);
image.startAnimation(move);
}
});
But when I clicked the button nothing happened!
How do I make it work ?

Your animation probably works but it will only move the view by one pixel, which is not that noticeable. Try changing the values of your translate animation.

Related

How do I display a Random number generated in a TextView?

I am generating random flashes of a button and I want to know what exact number is being chosen each time so I can eventually add them to an ArrayList for a project I am undergoing.
TextView randomTextView;
Random r = new Random();
private void sequenceFunction() {
//Change Alpha from Fully Visible to Invisible
final Animation animation = new AlphaAnimation(1, 0);
//Duration - A Second
animation.setDuration(1000);
//Animation Rate
animation.setInterpolator(new LinearInterpolator());
animation.setStartOffset(250);
animation.setDuration(250);
//Repeat Animation
animation.setRepeatCount(r.nextInt(10));
// Reverse animation at the end so the button will fade back in
animation.setRepeatMode(Animation.REVERSE);
//Button 1 Flashes
final Button btn = (Button) findViewById(R.id.button);
btn.startAnimation(animation);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
view.clearAnimation();
}
});
}
I want to display the result of what ever random number has been generated via the randomTextView TextView. This part is critical so that I know that the random function is working as it is supposed to. I have already tried
randomTextView.setText(r.nextInt(10));
However it didn't like it. Any ideas on how to get the random number selected would be greatly appreciated ?
Hope this helps -
private void sequenceFunction() {
//Change Alpha from Fully Visible to Invisible
final Animation animation = new AlphaAnimation(1, 0);
//Duration - A Second
animation.setDuration(1000);
//Animation Rate
animation.setInterpolator(new LinearInterpolator());
animation.setStartOffset(250);
//Repeat Animation
int randomValue = r.nextInt();
// code to add value to array
animation.setRepeatCount(randomValue);
randomTextView.setText(String.valueOf(randomValue));
// Reverse animation at the end so the button will fade back in
animation.setRepeatMode(Animation.REVERSE);
//Button 1 Flashes
final Button btn = (Button) findViewById(R.id.button);
btn.startAnimation(animation);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
view.clearAnimation();
}
});
}
If you have look in the documentation of TextView, you can see that setText(int) actually expects a resource id: https://developer.android.com/reference/android/widget/TextView.html#setText(int)
So you have to turn your int to a CharSequence, you could simply do
randomTextView.setText("" + r.nextInt(10));
which converts your integer to a string.
TextView.setText can be used with either a resource ID (which is an integer) or with a CharSequence, for example a String.
There are many ways to do this, including
int random = r.nextInt(10);
randomTextView.setText(String.valueOf(random);
randomTextView.setText(Integer.toString(random);
randomTextView.setText(String.format("%d", random);
Do not use "" + r.nextInt(10). I know, it is short, it is handy, but it is simply inefficient and bad style.

Animate button move and set new position in Android

I've an ImageButton that I want to move when pressed and when animation finish I want that this button stops in the new position.
This is button code:
<ImageButton
android:id="#+id/move_button"
android:layout_width="120dp"
android:layout_height="35dp"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:scaleType="fitCenter"
android:background="#drawable/background_button"
android:src="#drawable/move_button"
android:onClick="MoveButton" />
I've found a code to do that in this site:
public void MoveButton(final View view) {
TranslateAnimation anim = new TranslateAnimation(0, 0, 100, 0);
anim.setDuration(300);
anim.setAnimationListener(new TranslateAnimation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationRepeat(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation)
{
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)view.getLayoutParams();
params.topMargin += -100;
view.setLayoutParams(params);
}
});
view.startAnimation(anim);
}
When button it's pressed it start the animation, but when animation is complete button return to initial position and application crashes.
What can be the problem?
This is work Definitely.
Button im= (Button) findViewById(R.id.button);
//set position TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta
final Animation animation = new TranslateAnimation(0,100,0,0);
// set Animation for 5 sec
animation.setDuration(5000);
//for button stops in the new position.
animation.setFillAfter(true);
im.startAnimation(animation);
Use anim.setFillAfter(true) to situated the View at the position where Animation ends.
One thing more you are animating your ImageButton from 100 to 0 in Y coordinates, thats why your ImageButton comes to intial position because 0 is its intial position.
Try below code in this code I used anim.setFillAfter(true) and animate the ImageButton from 0 to 100 in Y coordinates.
public void MoveButton(final View view) {
TranslateAnimation anim = new TranslateAnimation(0,0,0,100);
anim.setDuration(300);
anim.setFillAfter(true);
anim.setAnimationListener(new TranslateAnimation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
});
view.startAnimation(anim);
}
Let me know if this is helpful for you.
Use ObjectAnimator
ObjectAnimator animation = ObjectAnimator.ofFloat(YOUR_VIEW, "translationX", 100f);
animation.setDuration(2000);
animation.start();
This code will move the View 100 pixles to the right, over a period of 2 seconds.
If you need more information go to Developers Guide
Import the necessary libraries:
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
Get the button you need to use:
Button button = findViewById(R.id.button_id);
Now in order to animate button you will need to use the TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta) function which takes 4 float arguments.
Animation Example:
Animation your_animation = new TranslateAnimation(0,100,0,0);
The first argument specifies the start of the x position and the second one the end of the x position, Same thing for the other two but the y position instead.
You will also need to set your animation's duration in milliseconds using
your_animation.setDuration(1000);
Your button will get back to its previous position when the animation ends, in order to make the button stay to its new position, use this:
your_animation.setFillAfter(true)
For more information on animations https://developer.android.com/training/animation

Add image to location where user click on Layout

I am trying to add image to the point (X,Y) on Layout i have created dynamicaly.
I want to add the imageview on exact user clicked location. But the image is not placed on correct location when clicked.
here is my code
final LinearLayout layoutColumnBoxes = new LinearLayout(getParent());
layoutColumnBoxes.setBackgroundColor(Color.GREEN);
layoutColumnBoxes.setId(counterIdForBoxes);
layoutColumnBoxes.setLayoutParams(layoutParamsColumns);
layoutColumnBoxes.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(getParent(),"Event="+event.getX()+"Event Y = "+event.getY(),Toast.LENGTH_SHORT).show();
return false;
}
});
layoutColumnBoxes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ImageView imageView = new ImageView(getParent());
imageView.setImageResource(R.drawable.crack);
LinearLayout lay = (LinearLayout) view.findViewById(v.getId());
lay.addView(imageView);
// Toast.makeText(getParent(),"Clicked View Id is="+v.getId(),Toast.LENGTH_SHORT).show();
}
});
Please help.
See I know what you're trying to say cause I too had this problem a few months back when developing an application. When you click a point on the screen the position of the imageview is not close to the way you clicked it right?
the things I did were
1) remove the excess space around your image.
This is optional. The only reason I stated this was because it depends on the type of image in your image view cause later you will be placing the image based on the top left corner of the imageview.
2) if your loading the images from the drawable folder make sure that you have the right size image in the right folder. This is IMPORTANT. if you screw up the sizes, android is going to alter the size and location of your images based on when your touch is based on the screen.
3) you need to set the margins of your image view properly. After hours of googling I came up with the following first you'll need to get half the height and width of your imageview, use imageview.getMeasuredHeight() / 2 and imageview.getMeasuredWidth() / 2 (Call these imgH and imgW respectively for explanation purposes)
the later in your OnTouchListener you'll have to set the top left margin of your imageview (the methods vary on the type of layout you use). This is done by setting the top margin at "event.getY - imgH" and the left margin at "event.getX - imgW"
I have an app in the playstore which uses this feature, you can check it here .
I hope this has solved your problem :) if it hasn't then don't hesitate to speak your doubts.
this code work for me:
public class MainActivity extends ActionBarActivity {
float x, y;
RelativeLayout rl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().hide();
rl = (RelativeLayout) findViewById(R.id.root_view);
rl.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
// rl.removeView(iv);
x = motionEvent.getX();
y = motionEvent.getY();
setImageLocation(x, y);
//rl.addView(iv);
}
return false;
}
});
}
public void setImageLocation(float x, float y) {
ImageView newImage;
RelativeLayout.LayoutParams newparams;
newparams = new RelativeLayout.LayoutParams(20, 20);
newparams.setMargins((int) (x-rl.getPaddingLeft()), (int) (y-rl.getPaddingTop()), 0, 0);
newImage = new ImageView(this);
newImage.setLayoutParams(newparams);
newImage.setImageResource(R.drawable.clock);
rl.addView(newImage);
}
}
activity_main.xml layout only has one relativeLayout that has full screen size(match parent)

Android: Button On Click listener: Single and constant when held down

I am unsure how to word this right but I want a button / on click listener to do this
1 touch = 1 tap = generate 1 row
Hold button down = generate 1 row per seconds or x per second
You know in games like clash of clans when u recruit soldier, u can touch it for 1 soldier or hold it down for more. I want a similar implementation but for a button
Thank you
For one single tap to generate 1 row, use:-
Button b = (Button)findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//put your code here for single tap event
}
});
For long press use the following:-
b.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//your code here for long press event
}
});

Android Button Not moving onclick

I have an up button which I want to move up onClick but when I click on it it moves way to far and gets to the end of the screen and then shrinks down untill you cant see it. It moves up too much, but why? I only increase it by one unit?
final Button upbutton = (Button)findViewById(R.id.upbutton);
upbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams)
upbutton.getLayoutParams();
mParams.bottomMargin += 1;
upbutton.setLayoutParams(mParams);
}
});
}
Because you're not assigning mParams to your buttons params.
mParams = upbutton.getLayoutParams();
You seem to be increasing parameters for your RelativeLayout then assigning those to your button. So the button gets confused. Try looking for a set margin option or something on the actual button view.

Categories