ImageView圖片無限循環(huán)移動

先看一下效果 再貼代碼




自定義屬性:



設(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);

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容