Shake Icons on Longpress [EDIT MODE] - android

I have an Android application with some ImageButtons, just like icons.
I would like to know how I could implement a function to "shake" icons after user long-presses them. Like iPhone icons on edit mode, you know?
After editing, icons stop shaking.
Is it possible to do that in Android?

use this to start the shaking animation on your icons upon the long press event of your button
public void onClick(View v) {
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
findViewById(R.id.pw).startAnimation(shake);
}
This snippet is taking from the android API Demo here http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/Animation1.html

Try to use TranslateAnimation (And/Or AnimationSet) on the button.
something like this:
private void onYourButtonLongPress()
{
TranslateAnimation animation = new TranslateAnimation(0, -5, 0, 0);
animation.setDuration(100):
yourButton.startAnimation(animation);
}
notice that this example is only for shake to left

Related

How to achieve animations just like gmail app?

When click on button_next then linear_layout_email slide left and linear_layout_password will come from right. example https://imgur.com/a/b7b66
Here my java code paste below. please find some solution on it.
mLinearLayoutEmail.animate().translationX(-1000);
mLinearLayoutPassword.animate().translationX(-80);
To me its better to read all the animation calculate them and then go for making Xml anim for each of the anim and use it as you need using AnimationUtils!
here its done in step by step! there is a builtin method for animation.
follow the steps in the code!
FADE IN RIGHT
[![FADE IN RIGHT animation in xml anim file][1]][1]
slide left
[![ANim xml code for slide and fade left][2]][2]
using ANimationUtils.loadAnimation(context,ANimFileResource) to read the anim file
e.g
Animation FadeAnim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fading_anim);
use StartAnimation method over the linearLayout in the ClickListener of the button you want to use for loading the anim for example
fabPostTimeLine.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mGetSwipedLeftLinLay.startAnimation(FadeAnim);
}
});

Android animation only works once?

So i'm trying to do 2 animations simultaneously, one to move a textview, and one to show a linearlayout (also 2 animations to hide them). I have another animation working as intended to show/hide a seperate layout. When i execute showing the view with 2 animations, it works once, it hides fine, but then doesn't work again. Then when i show the other view, it plays all 3 animations (not intended). I can't figure out why this is happening? When i try to show the 2 animations it does nothing, but then when i try the other show view its like it was added to a queue and it shows all 3.
My code for initiating the two animations:
LinearLayout layoutMsgs = (LinearLayout)findViewById(R.id.layoutMsgs);
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.msgs_show);
anim.setAnimationListener(new AnimListener(layoutMsgs, View.VISIBLE)); // sets visibility on animation end
layoutMsgs.startAnimation(anim);
TextView tvMsgs = (TextView)findViewById(R.id.tvMsgs);
Animation tvAnim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.tvmsgs_show);
tvMsgs.startAnimation(tvAnim);
My code for hiding the two animations:
LinearLayout layoutMsgs = (LinearLayout)findViewById(R.id.layoutMsgs);
Animation animLayout = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.msgs_hide);
animLayout.setAnimationListener(new AnimListener(layoutMsgs, View.INVISIBLE));
layoutMsgs.startAnimation(animLayout);
TextView tvMsgs = (TextView)findViewById(R.id.tvMsgs);
Animation animMsgs = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.tvmsgs_hide);
tvMsgs.startAnimation(animMsgs);
Then this is the other animation that is working fine, it's only one animation, no textView, just a layout
LinearLayout pokeLayout = (LinearLayout)findViewById(R.id.layoutPokes);
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.poke_show);
anim.setAnimationListener(new AnimListener(pokeLayout, View.VISIBLE));
pokeLayout.startAnimation(anim);
So how can i fix this? Sorry if my explanation is bad i'm finding it difficult to explain all the details, please ask for any missing information.
solved by using solution in this post Android: Using ObjectAnimator to translate a View with fractional values of the View's dimension
wasn't pretty as I had to create seperate translate X and translate Y res files for each view I need to move.. ended up with like 15 anim files. Would still like to know if theres a better solution

Flash image on button click over the button

As I am working on keyboard, we know on default keyboard on any android device when we click any button larger image is flashed above button, I don't know exactly this effect, ut I have tried using below code.
Button which is clicked in my Keyboard.xml:
<Button android:id="#+id/xBack"
android:background="#drawable/back_high"/>
Above back_high is my xml file.
back_high.xml file is,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/back_click"
android:state_pressed="true" />
<item android:drawable="#drawable/back"
android:state_focused="true" />
<item android:drawable="#drawable/back" />
</selector>
Its working successfully, but image is flashed at same place where I clicked, but I need this image is displayed on above button, as happening on android default keyboard.
your setting the background of that specific button, so if you want it to appear above the button make sure you use a frame layout as the overall layout of the keyboard and then have a single extra imageview that you switch location and resource when you click on a button. you won't need the selector anymore.
Frame layout's will let you put multiple views on top of each other.
This Link :
Why not make this programmatically and not in UI mode?
There are several, depending on what kind of flashing you mean.
You can, for example, use alpha animation and start it as your button first appears. And when the user clicks button, in your OnClickListener just do clearAnimation().
Example:
public void onCreate(Bundle savedInstanceState) {
final Animation animation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
animation.setDuration(500); // duration - half a second
animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
animation.setRepeatCount(Animation.INFINITE); // Repeat animation infinitely
animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the end so the button will fade back in
final Button btn = (Button) findViewById(R.id.your_btn);
btn.startAnimation(animation);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View view) {
view.clearAnimation();
}
});
}
Also This Answer.

Android animation moves the Layout, but not the buttons

Say I have a "main screen" Layout which has to move up, to reveal a "history bar".
if ( HistoryBar == false )
{
HistoryBar = true;
TranslateAnimation MainScreenPullUp = new TranslateAnimation(0, 0, 0, -200 );
MainScreenPullUp.setDuration(350);
MainScreenPullUp.setFillAfter(true);
LinLayMain.startAnimation(MainScreenPullUp);
}
Right now I am doing it like that. But the problem is, that while the main screen layout itself moves up, all the button places keep the same, i.e. the button picture moves, but the click-spot remains in place. Any ideas how to fix that?
I tried useing an animationListener, instead setFillAfter(true) , which would TanslateY the main screen layout, once the animation stops, but it makes the layout jump and flicker. Are there any other ways?
edit:
Thanks, Vineet.
Got it working with:
ObjectAnimator pullUp = ObjectAnimator.ofFloat(LinLayMain, "translationY", 0f, -200f);
pullUp.setDuration(350);
pullUp.start();
I faced the same problem. From 3.0 android has property animation. Below 3.0 I tackled this issue with the help of view's method offsetLeftAndRight (int offset) and offsetTopAndBottom (int offset). You can refer this link for more details.
For Property animation refer this link.

Adding a button to the android emulator skin

I'm trying to add a button to one of the default emulator skins. I used the layout file to get the button to appear but I'm not sure how to map it so that it actually does something.
You want to grab the button from the layout XML with something like this in onCreate:
Button xButton = (Button)findViewById(R.id.buttonX); //buttonX is the id you gave the button in the layout
Then you could set a click listener for that button in your onCreate like this:
xButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// do stuff here
}});
Very late but here is what I've found:
Create your skin as normal with the button images in place (rendered)
Create a button overlay image which is going to be drawn over the button when you mouse move over it with the mouse (can be the same for all buttons given it is only meant to be a focus indicator)
In your skin define the button(s) in the layout (see below)
For each button you can link it to a specific qemu code (see https://android.googlesource.com/platform/external/qemu.git/+/8b9887163ce94928aec159956d1a61fc93bb949d/android/skin/file.c#122)
layout:
parts {
device {
...
}
portrait {
...
buttons {
search { // see qemu codes
image xxxx.png // the overlay image to show on mouse move over
x 00000 // the top-left coordinate to show the image
y 00000 //
}
}
}
}
PS: I am pretty sure the button size is dependant on the button image dimensions.

Categories

Resources