网站建设资讯

NEWS

网站建设资讯

怎么在Android中利用ImageView实现一个放大缩小动画

这期内容当中小编将会给大家带来有关怎么在Android中利用ImageView实现一个放大缩小动画,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都做网站、网站设计、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的彭泽网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

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() {
              @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;
  }
}

上述就是小编为大家分享的怎么在Android中利用ImageView实现一个放大缩小动画了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


当前题目:怎么在Android中利用ImageView实现一个放大缩小动画
文章路径:http://njwzjz.com/article/gpiodj.html