您好,登錄后才能下訂單哦!
本來項目是用的viewpager實現的輪播滾動,但是客戶覺得輪播的效果太大眾化了,于是就要我們改成漸變切換的效果。聽到這需求,我最先想到是給viewpager設置切換動畫,但是無論怎么設置動畫,都要手動切換的時候才有效果。于是我就自定義了一個控件,利用淡入淡出動畫實現了這效果,還是先上效果圖,沒效果圖說再多也沒用。
public class Gradient extends RelativeLayout { private List<ImageView> imageViews; private List<Animation> outAnim;//淡出動畫 private List<Animation> inAnim;//淡入動畫 private Context mContext; private Handler handler = new Handler(Looper.getMainLooper()); private int couot; private int currentIndex;//當前的頁面 private LinearLayout linearLayout; private onClickListner listner; private long time=3000;//動畫間隔時間 public Gradient(Context context) { this(context, null); } public Gradient(Context context, AttributeSet attrs) { super(context, attrs); this.mContext = context; } /** * 畫點 */ public void cratePoint() { if (null != imageViews && imageViews.size() > 0) { int size = imageViews.size(); linearLayout = new LinearLayout(mContext); linearLayout.setOrientation(LinearLayout.HORIZONTAL); linearLayout.setGravity(Gravity.CENTER); // 添加圖片 for (int i = 0; i < size; i++) { // 設置圓點 View viewPoint = new View(mContext); viewPoint.setBackgroundResource(R.drawable.point_background); int weight = dip2px(mContext, 5); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(weight, weight); lp.leftMargin = weight; viewPoint.setLayoutParams(lp); viewPoint.setEnabled(false); linearLayout.addView(viewPoint); } View childAt = linearLayout.getChildAt(0); if (null != childAt) { childAt.setEnabled(true); } //添加到圖片的下邊 RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(-1,-2); rlp.bottomMargin = dip2px(mContext, 5); rlp.addRule(ALIGN_PARENT_BOTTOM); this.addView(linearLayout, rlp); } } /** * 根據手機的分辨率從 dip 的單位 轉成為 px(像素) */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 設置圖片 * @param imageViews */ public void setImageViews(List<ImageView> imageViews) { this.imageViews = imageViews; for (int i = 0; i < imageViews.size(); i++) { RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(-1,-1); addView(imageViews.get(i),layoutParams); } setonClick(); cratePoint(); createAnim(); start(); } /** * 開啟動畫 */ private void start() { final int size = imageViews.size(); handler.post(new Runnable() { @Override public void run() { final int i = couot % size; //解決點擊事件的沖突 for (int j = 0; j < size; j++) { if (j == i) { imageViews.get(i).setClickable(true); } else { imageViews.get(i).setClickable(false); } } if (couot < size) { if (i == size - 1) { ImageView imageView = imageViews.get(0); imageView.startAnimation(outAnim.get(0)); ImageView imageView2 = imageViews.get(size - 1); imageView2.startAnimation(inAnim.get(size - 1)); } else { //當前的淡出,下一張淡入 ImageView imageView = imageViews.get(size - 1 - i); imageView.startAnimation(outAnim.get(size - 1 - i)); } } else { if (i == size - 1) { //當顯示到最后一張的時候,要跳到第一張 ImageView imageView = imageViews.get(0); imageView.startAnimation(outAnim.get(0)); ImageView imageView2 = imageViews.get(size - 1); imageView2.startAnimation(inAnim.get(size - 1)); } else { //當前的淡出,下一張淡入 ImageView imageView = imageViews.get(size - 1 - i); imageView.startAnimation(outAnim.get(size - 1 - i)); ImageView imageView2 = imageViews.get(size - 2 - i); imageView2.startAnimation(inAnim.get(size - 2 - i)); } } currentIndex = i; linearLayout.getChildAt(currentIndex % size).setEnabled(false); currentIndex++; linearLayout.getChildAt(currentIndex % size).setEnabled(true); couot++; handler.postDelayed(this, time); } }); } /** * 創建動畫 */ private void createAnim() { outAnim = new ArrayList<>(); inAnim = new ArrayList<>(); for (int i = 0; i < imageViews.size(); i++) { Animation zoomOutAwayAnim = createZoomOutAwayAnim(); zoomOutAwayAnim.setFillAfter(true); outAnim.add(zoomOutAwayAnim); Animation zoomOutNearAnim = createZoomOutNearAnim(); zoomOutNearAnim.setFillAfter(true); inAnim.add(zoomOutNearAnim); } } /** * 設置點擊事件 */ public void setonClick() { for (int i = 0; i < imageViews.size(); i++) { imageViews.get(i).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (listner != null) { listner.setonClick((currentIndex) % imageViews.size()); } } }); } } public interface onClickListner{ void setonClick(int position); } /** * 設置動畫播放和handler延遲時間 * @param time */ public void setTime(long time) { this.time = time; } public void setListner(onClickListner listner) { this.listner = listner; } /** 創建一個淡出縮小的動畫 */ public Animation createZoomOutAwayAnim() { AnimationSet ret; Animation anim; ret = new AnimationSet(false); // 創建一個淡出的動畫 anim = new AlphaAnimation(1f, 0f); anim.setDuration(time); anim.setInterpolator(new DecelerateInterpolator()); ret.addAnimation(anim); // 創建一個縮小的動畫 /*anim = new ScaleAnimation(1, 0, 1, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); anim.setDuration(MEDIUM); anim.setInterpolator(new DecelerateInterpolator()); ret.addAnimation(anim);*/ return ret; } /** 創建一個淡入縮小的動畫 */ public Animation createZoomOutNearAnim() { AnimationSet ret; Animation anim; ret = new AnimationSet(false); // 創建一個淡入的動畫 anim = new AlphaAnimation(0f, 1f); anim.setDuration(time); anim.setInterpolator(new LinearInterpolator()); ret.addAnimation(anim); // 創建一個縮小的動畫 /*anim = new ScaleAnimation(3, 1, 3, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); anim.setDuration(MEDIUM); anim.setInterpolator(new DecelerateInterpolator()); ret.addAnimation(anim);*/ return ret; } }
這個控件的使用非常簡單只要在布局文件中使用我們自定義的控件,然后調用setTime設置動畫切換的時間,setListener設置圖片的點擊事件,setImagevies設置圖片就可以實現效果.考慮到內存泄漏的問題,只要在ondestry方法里面調用stop方法即可,點擊下載Demo
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。