Android自定义ImageView实现自动放大缩小动画
程序员文章站
2023-10-19 22:39:06
这篇讲的是如何生成一个自定义的imageview,实现自动放大缩小动画。
为什么实现这个功能呢?因为我想在viewpager实现图片放大缩小的动画,但是viewpa...
这篇讲的是如何生成一个自定义的imageview,实现自动放大缩小动画。
为什么实现这个功能呢?因为我想在viewpager实现图片放大缩小的动画,但是viewpager几个页面的动画会一起动,而且放大全屏图片的话会相互覆盖,很诡异。于是上网搜demo,一无所获。迫于无奈。。。
废话不多说,直接贴代码。
1.配置文件直接添加
当直接在布局文件中添加图片的话,可以在自定义view代码中用getdrawable()获取图片资源,然后通过drawbitmap绘制图片。通过不断绘制图片的位置,达到放大缩小的功能。
第一种情况实在xml布局文件中直接添加的:
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; } }
2 .通过glide加载图片的方式
通过glide加载图片的话,不能直接用getdrawable获取图片资源。glide加载图片的方式也需要改变。废话不多说,直接上代码。
coolimageview直接从glide的缓存中加载图片。
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); } });
coolimageview.java:
唯一不同的是获取图片的方式;
```java
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; } }
如果感觉动画不够流畅可以缩小线程等待时间。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: SAI快捷键怎么使用?