ImageView图片无限循环移动

先看一下效果 再贴代码




自定义属性:



设置控件宽高:

左右移动算法:


完整代码 :

private BitmapmBitmap; //生成位图

private double startX =0;    //移动起始X坐标

private int screenWidth;

public static StringTAG ="MoveImageView";

private Timertimer;

private final int orientation;//默认向左

private double speed;

public MoveImageView(Context context, AttributeSet attrs) {

super(context, attrs);

    if (contextinstanceof AppCompatActivity) {

((AppCompatActivity) (context)).getLifecycle().addObserver(this);

    }

TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MoveItemView);

    orientation = ta.getInt(R.styleable.MoveItemView_orientation, 0);

    speed = ta.getFloat(R.styleable.MoveItemView_speed, 0.1f);

    Drawable drawable = ta.getDrawable(R.styleable.MoveItemView_img);

    if (drawable !=null) {

Bitmap bitmap = drawableToBitamp(drawable);

        setmBitmap(bitmap);

    }

ta.recycle();

    DisplayMetrics dm = context.getResources().getDisplayMetrics();

    screenWidth = dm.widthPixels;

    start();

}

private BitmapdrawableToBitamp(Drawable drawable) {

BitmapDrawable bd = (BitmapDrawable) drawable;

    Bitmap bitmap = bd.getBitmap();

    return bitmap;

}

public void setmBitmap(int res) {

mBitmap = BitmapFactory.decodeResource(getContext().getResources(), res);

}

private void setmBitmap(Bitmap bitmap) {

mBitmap = bitmap;

}

Handlerhandler;

public void start() {

handler =new Handler(Looper.getMainLooper()) {

public void handleMessage(Message msg) {

if (msg.what ==1) {

if (orientation ==0) {

startX -=speed;

                }else {

startX +=speed;

                }

}

invalidate();

        }

};

    timer =new Timer();

    timer.schedule(new TimerTask() {

@Override

        public void run() {

handler.removeCallbacksAndMessages(null);

            handler.sendEmptyMessage(1);

        }

//无延迟,10毫秒循环一次。

    }, 0, 2);

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    if (mBitmap !=null) {

setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());

    }

}

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)

public void onResume() {

Log.d(TAG, "---onResume---");

    if (handler !=null) {

handler.removeCallbacksAndMessages(null);

        handler.sendEmptyMessage(1);

    }

}

@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)

public void onPause() {

Log.d(TAG, "---onPause---");

    if (handler !=null) {

handler.removeCallbacksAndMessages(null);

    }

}

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)

public void onDestroy() {

Log.d(TAG, "---onDestroy---");

    if (timer !=null) {

timer.cancel();

    }

if (handler !=null) {

handler.removeCallbacksAndMessages(null);

    }

}

@Override

public void onDraw(Canvas canvas) {

if (mBitmap !=null) {

if (orientation ==0) {

moveLeft(canvas);

        }else {

moveRight(canvas);

        }

}

}

private void moveLeft(Canvas canvas) {

int page = (int) (Math.abs(startX) +screenWidth) /mBitmap.getWidth();

    Log.d(TAG, "size=" + page +" startX=" +startX);

    canvas.drawBitmap(mBitmap, (float)startX + (page -1) *mBitmap.getWidth(), 0, null);

    canvas.drawBitmap(mBitmap, (float) (startX +mBitmap.getWidth() * page), 0, null);

}

private void moveRight(Canvas canvas) {

int page = (int) (startX /mBitmap.getWidth());

    canvas.drawBitmap(mBitmap, (float)startX -mBitmap.getWidth() * page, 0, null);

    canvas.drawBitmap(mBitmap, (float)startX -mBitmap.getWidth() * (page +1), 0, null);

}

你可能感兴趣的:(ImageView图片无限循环移动)