先看一下效果 再貼代碼
自定義屬性:
設(shè)置控件寬高:
左右移動算法:
完整代碼 :
private BitmapmBitmap; //生成位圖
private double startX =0;? ? //移動起始X坐標(biāo)
private int screenWidth;
public static StringTAG ="MoveImageView";
private Timertimer;
private final int orientation;//默認(rèn)向左
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毫秒循環(huán)一次土匀。
? ? }, 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);
}