DPSlidesImageView-Android

DPSlidesImageView-Android

自定义Imageview,实现定时切换,类似幻灯片效果

自定义ViewPager实现幻灯片可以查看android-tv-guide-viewpager

使用方法

  mTimerImageView = (TimerImageView)mView.findViewById(R.id.timerimageview);
  mTimerImageAdapter = new TimerImageAdapter();
  mTimerImageView.setAdapter(mTimerImageAdapter);
  mTimerImageAdapter.setData(mList);//数据

xml引入

   <com.whiskeyfei.widget.TimerImageView
        android:id="@+id/timerimageview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

关键类

主要实现类包含TimerImageAdapter,TimerImageView,ImageChangeListener,AnimationUtils使功能封装再控件里

ImageChangeListener

定义接口

public interface ImageChangeListener {
    public void updateImage(int id,int position);
}
TimerImageView

1、主要功能为刷新图片和过渡动画效果

2、绑定TimerImageAdapter

3、点击事件处理(next image)

//...
   private void init(Context context){
        mContext = context;
        setScaleType(ScaleType.FIT_CENTER);
        setOnClickListener(this);
    }


    public void setAdapter(TimerImageAdapter adapter){
        mTimerImageAdapter = adapter;
        mTimerImageAdapter.setImageChangeListener(this);
    }

    @Override
    public void updateImage(int id, int position) {
        Toast.makeText(mContext,"updateImage position:" + position,Toast.LENGTH_SHORT).show();
        setImageResource(id);
        AnimationUtils.alphaAnimation(this, 0.0f, 1.0f, 500);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mTimerImageAdapter!= null) mTimerImageAdapter.setImageChangeListener(null);
    }

//...
TimerImageAdapter

1、绑定功能数据

2、维护图片数据队列

3、封装开始、停止、切换等操作

部分代码

    private Handler mHandler = new Handler(Looper.getMainLooper()) {

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case CHANGE_FLAG:
                    switchImage();
                    break;
            }
        }
    };


    public void setData(List<ImageModel> mList){
        this.mList = mList;
// startTimer();
    }

     public void startTimer() {
        if (isStop){
            isStop = false;
        }
        mHandler.removeMessages(CHANGE_FLAG);
        mHandler.sendEmptyMessageDelayed(CHANGE_FLAG, 50);
    }

    /** * 停止切换 */
    public void stopSwitchImage(){
        isStop = true;
        mHandler.removeMessages(CHANGE_FLAG);
    }

    /** * 切换下一张 */
    public void switchNext(){
        mHandler.removeMessages(CHANGE_FLAG);
        mHandler.sendEmptyMessageDelayed(CHANGE_FLAG, 50);
    }

动画

AnimationUtils

目前只有一种动画效果,以后会添加

   public static void alphaAnimation(final View view, float start, float end, long time) {
        view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        ObjectAnimator oa = ObjectAnimator.ofFloat(view, "alpha", start, end);
        oa.setDuration(time);
        oa.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                view.setLayerType(View.LAYER_TYPE_NONE, null);
            }
        });
        oa.start();
    }

=====================
更多内容请关注我的github

你可能感兴趣的:(android,imageview)