public class CoolImageView extends ImageView {
private int mLeft = 0;
private int mTop = 0;
private Handler mHandler;
private Bitmap bitmap;
private Rect srcRect = new Rect();
private Rect dstRect = new Rect();
private int imgWidth;
private int imgHeight;
private boolean flag;
private boolean istart;
public CoolImageView(Context context) {
super(context);
}
public CoolImageView(Context context, AttributeSet attrs) {
super(context, attrs);
setUp(context, attrs);
}
public CoolImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setUp(context, attrs);
}
private void setUp(Context context, AttributeSet attrs) {
mHandler = new MoveHandler();
mHandler.sendEmptyMessageDelayed(1, 220L);
istart = true;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = getWidth();
int height = getHeight();
//获取图片资源
BitmapDrawable drawable = (BitmapDrawable) getDrawable();
bitmap = drawable.getBitmap();
dstRect.left = 0;
dstRect.top = 0;
dstRect.right = width;
dstRect.bottom = height;
if (bitmap != null) {
if (istart) {
// 获取图片的宽高
imgWidth = bitmap.getWidth();
imgHeight = bitmap.getHeight();
srcRect.left = 0 + mLeft;
srcRect.right = imgWidth - mLeft;
srcRect.top = 0 + mTop;
srcRect.bottom = imgHeight - mTop;
canvas.drawBitmap(bitmap, srcRect, dstRect, null);
} else {
canvas.drawBitmap(bitmap, null, dstRect, null);
}
}
}
private class MoveHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
if (imgHeight != 0) {
if (mTop == 0) {
mTop += 5;
mLeft += 5;
} else if (mTop == 120) {
mTop -= 5;
mLeft -= 5;
}
}
postInvalidate();
mHandler.sendEmptyMessageDelayed(1, 250);
break;
}
}
}
public void start() {
mTop = 0;
mLeft = 0;
istart = true;
mHandler.sendEmptyMessageDelayed(1, 220L);
}
public void stop() {
istart = false;
}
}
Glide.with(GoodsPagerActivity.this)
.load(sList.get(position).img)
.override(width, height)
.centerCrop()
.into(new SimpleTarget<GlideDrawable>() {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
imageView.setImageDrawable(resource);
}
});
public class CoolImageView extends ImageView {
private int mLeft = 0;
private int mTop = 0;
private Handler mHandler;
private Bitmap bitmap;
private Rect srcRect = new Rect();
private Rect dstRect = new Rect();
private int imgWidth;
private int imgHeight;
private boolean flag;
private boolean istart;
private int width;
private int height;
public CoolImageView(Context context) {
super(context);
}
public CoolImageView(Context context, AttributeSet attrs) {
super(context, attrs);
setUp(context, attrs);
}
public CoolImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setUp(context, attrs);
}
private void setUp(Context context, AttributeSet attrs) {
mHandler = new MoveHandler();
mHandler.sendEmptyMessageDelayed(1, 220L);
istart = true;
}
@Override
public void setImageDrawable(@Nullable Drawable drawable) {
super.setImageDrawable(drawable);
if (mHandler != null) {
mHandler.sendEmptyMessageDelayed(1, 220L);
} else {
mHandler = new MoveHandler();
mHandler.sendEmptyMessageDelayed(1, 220L);
istart = true;
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
width = getWidth();
height = getHeight();
GlideBitmapDrawable drawable = (GlideBitmapDrawable) getDrawable();
if (drawable != null) {
bitmap = drawable.getBitmap();
}
dstRect.left = 0;
dstRect.top = 0;
dstRect.right = width;
dstRect.bottom = height;
if (bitmap != null) {
if (istart) {
imgWidth = bitmap.getWidth();
imgHeight = bitmap.getHeight();
srcRect.left = 0 + mLeft;
srcRect.right = imgWidth - mLeft;
srcRect.top = 0 + mTop;
srcRect.bottom = imgHeight - mTop;
canvas.drawBitmap(bitmap, srcRect, dstRect, null);
} else {
canvas.drawBitmap(bitmap, null, dstRect, null);
}
}
}
private class MoveHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
if (imgHeight != 0) {
if (mTop == 0) {
flag = true;
} else if (mTop == 60) {
flag = false;
}
if (!flag) {
mTop -= 2;
mLeft -= 1;
} else {
mTop += 2;
mLeft += 1;
}
}
postInvalidate();
mHandler.sendEmptyMessageDelayed(1, 200);
break;
}
}
}
public void start() {
mTop = 0;
mLeft = 0;
istart = true;
mHandler.sendEmptyMessageDelayed(1, 220L);
}
public void stop() {
istart = false;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有