TransitionDrawable一個能漸變顯示的Drawable
如果我想在ImageView顯示圖片的時候有個漸變的動畫效果,那么你可能會想到使用animation動畫來實現,但這這種方式看起來不會自然,因為在調用statAnimation的時候因為圖片已經顯示了,這樣再硬生生的播放一次動畫其實會導致細微的閃爍效果。
使用TransitionDrawable可以完美解決這個問題。
使用示例:
final TransitionDrawable td = new TransitionDrawable(new Drawable[] { new ColorDrawable(0xfffcfcfc), new BitmapDrawable(mContext.getResources(), bitmap) }); imageView.setImageDrawable(td); td.startTransition(300);
其中300是漸變持續的時間,TransitionDrawable
的第一個參數是漸變開始時的圖像,第二個參數是最終要顯示的圖像。
也可以將TransitionDrawable用xml表示:
<transition xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/image_expand" /> <item android:drawable="@drawable/image_collapse" /> </transition>
java代碼:
Resources res = getResources(); TransitionDrawable transition = (TransitionDrawable)res.getDrawable(R.drawable.expand_collapse); ImageView image = (ImageView) findViewById(R.id.toggle_image); image.setImageDrawable(transition); transition.startTransition(1000);
本文由用戶 jhse4105 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!