2013-08-14 26 views
20

मैं एक एंड्रॉइड ऐप बना रहा हूं, और मेरे पास एक बटन है जो मैसेजिंग स्थान की ओर जाता है। बटन के साथ गतिविधि पर, मैं जांचता हूं कि कोई अपठित संदेश है या नहीं, और यदि ऐसा है तो मैं बटन को कुछ करने के लिए चाहता हूं ताकि उपयोगकर्ता को यह पता चल सके कि कुछ अपठित है।एंड्रॉइड में बटन एनिमेट कैसे करें?

मैं बटन सॉर्टा को क्षैतिज रूप से कंपन करने की सोच रहा था जैसे 3 हर 2 या 3 सेकंड हिलाता है।

मुझे पता है कि पृष्ठभूमि में धागा कैसे चलाया जाए जो प्रत्येक एक्स मिलीसेकंड करता है। लेकिन मुझे नहीं पता कि क्या करना है इसे क्षैतिज रूप से 3 बार हिलाएं।

क्या कोई इससे मदद कर सकता है?

मैं एनीमेशन के लिए पाप फ़ंक्शन का उपयोग करने के बारे में सोच रहा था, मैं ऊपर और नीचे जाने वाले मान प्राप्त करने के लिए पाप फ़ंक्शन से आउटपुट का उपयोग कर सकता हूं, जिसे मैं बटन की क्षैतिज स्थिति सेट कर सकता हूं ... लेकिन ऐसा लगता है बहुत चरम, क्या एक बेहतर तरीका है?

धन्यवाद।

+0

यू चाहते एनीमेशन या बटन प्रेस प्रभाव करना ?? – Prachi

उत्तर

10

इस ट्यूटोरियल को देखें। उन्होंने कहा कि अलग अलग एनिमेशन

के तरीके हैं

http://android-er.blogspot.com/2012/02/apply-animation-on-button.html

+0

मैं देखता हूं कि इसे कैसे स्थानांतरित किया जाए, लेकिन मैं संक्रमणों का अनुक्रम कैसे बना सकता हूं? अभी बटन सही हो जाता है, फिर सामान्य पर वापस चला जाता है, फिर दोहराता है। लेकिन मैं इसे फिर से बाएं स्थानांतरित करना चाहता हूं। – omega

+0

@omega मुझे खेद है। मुझे अभी तक इस बारे में निश्चित नहीं है। यदि मैं इसे समझ सकता हूं तो मैं वापस आऊंगा। – Deepu

22

anim फ़ोल्डर में anim फ़ोल्डर

<?xml version="1.0" encoding="utf-8"?> 
<translate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromXDelta="0" 
     android:toXDelta="10" 
      android:duration="1000" 
       android:interpolator="@anim/cycle" /> 

और cycle.xml में shake.xml

<?xml version="1.0" encoding="utf-8"?> 
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" 
    android:cycles="4" /> 

अब अपनी कोड

पर एनीमेशन जोड़ने बनाने
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake); 
anyview.startAnimation(shake); 

आप खड़ी एनीमेशन चाहते हैं, fromYdelta और toYdelta मूल्य के लिए fromXdelta और toXdelta मूल्य बदल

+3

यह सिर्फ 'दाएं' को स्थानांतरित करता है, फिर तुरंत मूल स्थिति पर 4 बार वापस जाता है, लेकिन मैं इसे 'दाएं' और फिर 'बाएं' स्थानांतरित करने के लिए कैसे प्राप्त करूं, फिर 'दाएं' फिर 'बाएं' को मूल स्थिति में वापस ले जाएं प्रभाव हिलाओ? – omega

+0

यह एनीमेशन लगातार कैसे बनें? – CraZyDroiD

+0

यह उत्तर एंड्रॉइड में एनीमेशन की तरह शेक को लागू करने के लिए मेरे लिए उपयोगी है। –

21

मैं @ ओमेगा की टिप्पणी पर टिप्पणी नहीं कर सकता क्योंकि मैं पर्याप्त प्रतिष्ठा नहीं है, लेकिन इस प्रश्न का उत्तर की तरह कुछ किया जाना चाहिए:

shake.xml

<?xml version="1.0" encoding="utf-8"?> 
<rotate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="100"   <!-- how long the animation lasts --> 
    android:fromDegrees="-5"  <!-- how far to swing left --> 
    android:pivotX="50%"   <!-- pivot from horizontal center --> 
    android:pivotY="50%"   <!-- pivot from vertical center --> 
    android:repeatCount="10"  <!-- how many times to swing back and forth --> 
    android:repeatMode="reverse" <!-- to make the animation go the other way --> 
    android:toDegrees="5" />  <!-- how far to swing right --> 

Class.java

Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake); 
view.startAnimation(shake); 

यह वही करने का एक तरीका है जो आप चाहते हैं, वहां बेहतर तरीके हो सकते हैं।

+0

मैं क्षैतिज रूप से इसे कैसे हिला सकता हूं? –

0
import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.Transformation; 

public class HeightAnimation extends Animation { 
    protected final int originalHeight; 
    protected final View view; 
    protected float perValue; 

    public HeightAnimation(View view, int fromHeight, int toHeight) { 
     this.view = view; 
     this.originalHeight = fromHeight; 
     this.perValue = (toHeight - fromHeight); 
    } 

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     view.getLayoutParams().height = (int) (originalHeight + perValue * interpolatedTime); 
     view.requestLayout(); 
    } 

    @Override 
    public boolean willChangeBounds() { 
     return true; 
    } 
} 

यूएसएस रहे हैं:

HeightAnimation heightAnim = new HeightAnimation(view, view.getHeight(), viewPager.getHeight() - otherView.getHeight()); 
heightAnim.setDuration(1000); 
view.startAnimation(heightAnim); 
5


क्लास।जावा

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.layout_with_the_button); 

    final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.milkshake); 
    Button myButton = (Button) findViewById(R.id.new_game_btn); 
    myButton.setAnimation(myAnim); 
} 

लिए onClick बटन

myButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     v.startAnimation(myAnim); 
    } 
}); 

रेस में anim फ़ोल्डर बनाएँ निर्देशिका के

ठीक है, पर क्लिक करें रेस -> नई -> निर्देशिका

नाम नई निर्देशिका anim

एक नया xml फ़ाइल नाम बनाने यह खीर


milkshake.xml

<?xml version="1.0" encoding="utf-8"?> 
    <rotate xmlns:android="http://schemas.android.com/apk/res/android" 
     android:duration="100" 
     android:fromDegrees="-5" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:repeatCount="10" 
     android:repeatMode="reverse" 
     android:toDegrees="5" /> 

संबंधित मुद्दे