2015-07-16 2 views
6

मेरे पास है FloatingActionButton के साथ। जब मैं एफएबी दबाता हूं, तो AlertDialog दिखाया जाता है। मैं Android के Material Design से प्रभाव या घुमावदार गति प्रकट करने जैसे कुछ का उपयोग कर अपनी उपस्थिति को एनिमेट करना चाहता हूं। documentation में केवल मौजूदा विचारों की दृश्यता बदलने के लिए एक उदाहरण है।सामग्री डिजाइन से प्रकट प्रभाव का उपयोग कर एंड्रॉइड पर एक संवाद कैसे दिखाएं?

मैं इसे AlertDialog के लिए कैसे प्राप्त कर सकता हूं?

उत्तर

2

आप एक कस्टम दृश्य है (एक एक्सएमएल में परिभाषित) आप इस कोशिश कर सकते हैं: फोन करके

View v = <yourAlertDialog>.findViewById(R.layout.example); 

// get the center for the clipping circle 
int cx = (v.getLeft() + v.getRight())/2; 
int cy = (v.getTop() + v.getBottom())/2; 

// get the initial radius for the clipping circle 
int initialRadius = v.getWidth(); 

// create the animation (the final radius is zero) 
Animator anim = ViewAnimationUtils.createCircularReveal(v, cx, cy, initialRadius, 0); 

// make the view invisible when the animation is done 
anim.addListener(new AnimatorListenerAdapter() { 
    @Override 
    public void onAnimationEnd(Animator animation) { 
     super.onAnimationEnd(animation); 
     v.setVisibility(View.INVISIBLE); 
    } 
}); 

// start the animation 
anim.start(); 
+0

आप एपीआई स्तर के लिए एक समान एनीमेशन के लिए क्या सुझाव है < 21? –

2

मैं ऐसी ही कुछ किया:

AlertDialog a = new AlertDialog.Builder(this)...blablabla; 
View v = a.findViewById(R.layout.example); 

// get the center for the clipping circle 
int cx = (v.getLeft() + v.getRight())/2; 
int cy = (v.getTop() + v.getBottom())/2; 

// get the final radius for the clipping circle 
int finalRadius = Math.max(v.getWidth(), v.getHeight()); 

// create the animator for this view (the start radius is zero) 
Animator anim = ViewAnimationUtils.createCircularReveal(v, cx, cy, 0, finalRadius); 

// make the view visible and start the animation 
v.setVisibility(View.VISIBLE); 
anim.start(); 

रिवर्स एनीमेशन का उपयोग कर इसे छुपाने के लिए संवाद दिखाने से पहले संवाद setOnShowListener। मैं अभी भी एनीमेशन परिष्कृत करने की आवश्यकता है, लेकिन यह एक शुरुआत है: प्रकट

dialogToAnimate.setOnShowListener(new OnShowListener() { 
    @Override public void onShow(DialogInterface dialog) { 
     // Remember that ViewAnimationUtils will not work until API 21. 
     final View view = dialogToAnimate.getWindow().getDecorView(); 
     final int centerX = view.getWidth()/2; 
     final int centerY = view.getHeight()/2; 
     // TODO Get startRadius from FAB 
     // TODO Also translate animate FAB to center of screen? 
     float startRadius = 20; 
     float endRadius = view.getHeight(); 
     Animator animator = ViewAnimationUtils.createCircularReveal(view, centerX, centerY, startRadius, endRadius); 
     animator.setDuration(1000); 
     animator.start(); 
    } 
}); 
+0

मैंने संवाद पर एक मौजूदा एपीआई का उपयोग करने के लिए अपना जवाब अपडेट किया। –

1

चेतावनी के साथ संवाद ऊपर एपीआई के लिए प्रभाव 21 =>

public final void customAlertDialog(final Activity mActivity) 
{ 
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mActivity); 
     final View view = LayoutInflater.from(mActivity).inflate(R.layout.alert_dialog_simple_msg,null); 
     alertDialogBuilder.setView(view); 
     final AlertDialog alertDialog = alertDialogBuilder.create(); 
     alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 
     TextView tvOk= (TextView) view.findViewById(R.id.tvOk); 
     final Animator[] animHide = {null}; 
     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { 
      alertDialog.show(); 
      view.post(new Runnable() { 
       @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
       @Override 
       public void run() { 
        int cx = (int)view.getWidth()/2; 
        int cy = (int) view.getHeight()/2; 
        float finalRadius = (float) Math.hypot(cx, cy); 
        Animator animVisible = ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, finalRadius); 
        animHide[0] = ViewAnimationUtils.createCircularReveal(view, cx, cy, finalRadius, 0); 
        animVisible.start(); 
       } 
      }); 
     } 
     tvOk.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { 
        if (animHide[0]!=null) 
        { 
         animHide[0].addListener(new AnimatorListenerAdapter() { 
          @Override 
          public void onAnimationEnd(Animator animation) { 
           super.onAnimationEnd(animation); 
           alertDialog.dismiss(); 
          } 
         }); 
         animHide[0].start(); 
        } 
        else 
        { 
         alertDialog.dismiss(); 
        } 
       } 
       else 
       { 
        alertDialog.dismiss(); 
       } 
      } 
     }); 
     alertDialog.setCancelable(false); 
    } 
संबंधित मुद्दे