Android自定义ImageView实现自动放大缩小动画

Android自定义ImageView实现自动放大缩小动画

转载 :https://www.jb51.net/article/116326.htm

 

这篇文章主要为大家详细介绍了Android自定义ImageView实现自动放大缩小动画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

这篇讲的是如何生成一个自定义的ImageView,实现自动放大缩小动画。 

为什么实现这个功能呢?因为我想在ViewPager实现图片放大缩小的动画,但是ViewPager几个页面的动画会一起动,而且放大全屏图片的话会相互覆盖,很诡异。于是上网搜demo,一无所获。迫于无奈。。。 
废话不多说,直接贴代码。

1.配置文件直接添加

当直接在布局文件中添加图片的话,可以在自定义View代码中用getDrawable()获取图片资源,然后通过DrawBitmap绘制图片。通过不断绘制图片的位置,达到放大缩小的功能。 
第一种情况实在XML布局文件中直接添加的:

 

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

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的缓存中加载图片。

?

1

2

3

4

5

6

7

8

9

10

Glide.with(GoodsPagerActivity.this)

            .load(sList.get(position).img)

            .override(width, height)

            .centerCrop()

            .into(new SimpleTarget() {

              @Override

              public void onResourceReady(GlideDrawable resource, GlideAnimationsuper GlideDrawable> glideAnimation) {

                imageView.setImageDrawable(resource);

              }

            });

CoolImageView.java:
唯一不同的是获取图片的方式;
```java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

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)