2323import android .graphics .Canvas ;
2424import android .graphics .drawable .BitmapDrawable ;
2525import android .util .AttributeSet ;
26- import android .view .animation .Animation ;
27- import android .view .animation .LinearInterpolator ;
28- import android .view .animation .Transformation ;
2926import android .widget .ImageView ;
3027
3128/**
@@ -36,12 +33,33 @@ public class SlideImageView extends ImageView {
3633 private int width ;
3734 private int height ;
3835
39- private int duration ;
36+ private float rate ;
4037 private Bitmap bitmap ;
41- private boolean isSlowdown ;
4238
4339 private float bitmapX ;
44- private float slideWidth ;
40+ private float bitmapY ;
41+ private int slideSize ;
42+
43+ private Axis axis ;
44+
45+ public enum Axis {
46+ HORIZONTAL ,
47+ VERTICAL
48+ }
49+
50+ private HorizontalDirection horizontalDirection = HorizontalDirection .LEFT_TO_RIGHT ;
51+
52+ private enum HorizontalDirection {
53+ LEFT_TO_RIGHT ,
54+ RIGHT_TO_LEFT
55+ }
56+
57+ private VerticalDirection verticalDirection = VerticalDirection .TOP_TO_BOTTOM ;
58+
59+ private enum VerticalDirection {
60+ TOP_TO_BOTTOM ,
61+ BOTTOM_TO_TOP
62+ }
4563
4664 public SlideImageView (Context context ) {
4765 this (context , null );
@@ -57,30 +75,41 @@ public SlideImageView(Context context, AttributeSet attrs, int defStyleAttr) {
5775 TypedArray typedArray = context .obtainStyledAttributes (attrs , R .styleable .SlidedImageView );
5876
5977 try {
60- this .duration = typedArray .getInteger (R .styleable .SlidedImageView_duration , 30000 );
61- this .isSlowdown = typedArray .getBoolean (R .styleable .SlidedImageView_slowdown , true );
62-
6378 final int sourceId = typedArray .getResourceId (R .styleable .SlidedImageView_source , 0 );
6479 if (sourceId != 0 ) {
6580 setSource (sourceId );
6681 }
82+
83+ final float rate = typedArray .getFloat (R .styleable .SlidedImageView_rate , 0.3f );
84+ setRate (rate );
85+
86+ final int axis = typedArray .getInteger (R .styleable .SlidedImageView_axis , 0 );
87+ setAxis (axis );
6788 } finally {
6889 typedArray .recycle ();
6990 }
7091
7192 setWillNotDraw (false );
7293 }
7394
74- public void setDuration ( int duration ) {
75- this .duration = duration ;
95+ public void setRate ( float rate ) {
96+ this .rate = rate * (- 1 ) ;
7697 }
7798
78- public void setSource ( int sourceId ) {
79- this .bitmap = (( BitmapDrawable ) getResources (). getDrawable ( sourceId )). getBitmap () ;
99+ public void setAxis ( Axis axis ) {
100+ this .axis = axis ;
80101 }
81102
82- public void setSlowdown (boolean isSlowdown ) {
83- this .isSlowdown = isSlowdown ;
103+ private void setAxis (int axis ) {
104+ if (axis == 0 ) {
105+ this .axis = Axis .HORIZONTAL ;
106+ } else {
107+ this .axis = Axis .VERTICAL ;
108+ }
109+ }
110+
111+ public void setSource (int sourceId ) {
112+ this .bitmap = ((BitmapDrawable ) getResources ().getDrawable (sourceId )).getBitmap ();
84113 }
85114
86115 @ Override
@@ -89,14 +118,21 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
89118 this .height = h ;
90119
91120 if (this .bitmap != null ) {
92- this .bitmap = Bitmap .createScaledBitmap (
93- this .bitmap ,
94- this .height * this .bitmap .getWidth () / this .bitmap .getHeight (),
95- this .height ,
96- false );
97- this .slideWidth = this .bitmap .getWidth () - this .width ;
98-
99- startAnimation (new SlideAnimation ());
121+ if (this .axis == Axis .HORIZONTAL ) {
122+ this .bitmap = Bitmap .createScaledBitmap (
123+ this .bitmap ,
124+ this .height * this .bitmap .getWidth () / this .bitmap .getHeight (),
125+ this .height ,
126+ false );
127+ this .slideSize = (this .bitmap .getWidth () - this .width ) * -1 ;
128+ } else {
129+ this .bitmap = Bitmap .createScaledBitmap (
130+ this .bitmap ,
131+ this .width ,
132+ this .width * this .bitmap .getHeight () / this .bitmap .getWidth (),
133+ false );
134+ this .slideSize = (this .bitmap .getHeight () - this .height ) * -1 ;
135+ }
100136 } else {
101137 throw new NullPointerException (getContext ().getString (R .string .source_error ));
102138 }
@@ -108,26 +144,39 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
108144 protected void onDraw (Canvas canvas ) {
109145 super .onDraw (canvas );
110146
111- canvas .drawBitmap (this .bitmap , this .bitmapX , 0 , null );
112- }
113-
114- public class SlideAnimation extends Animation {
147+ if (this .axis == Axis .HORIZONTAL ) {
148+ if (this .horizontalDirection == HorizontalDirection .LEFT_TO_RIGHT ) {
149+ if (Math .round (this .bitmapX ) == this .slideSize ) {
150+ this .rate = -this .rate ;
151+ this .horizontalDirection = HorizontalDirection .RIGHT_TO_LEFT ;
152+ }
153+ } else {
154+ if (Math .round (this .bitmapX ) == 0 ) {
155+ this .rate = -this .rate ;
156+ this .horizontalDirection = HorizontalDirection .LEFT_TO_RIGHT ;
157+ }
158+ }
115159
116- public SlideAnimation () {
117- if (!isSlowdown ) {
118- setInterpolator (new LinearInterpolator ());
160+ this .bitmapX += this .rate ;
161+ this .bitmapY = 0 ;
162+ } else {
163+ if (this .verticalDirection == VerticalDirection .TOP_TO_BOTTOM ) {
164+ if (Math .round (this .bitmapY ) == this .slideSize ) {
165+ this .rate = -this .rate ;
166+ this .verticalDirection = VerticalDirection .BOTTOM_TO_TOP ;
167+ }
168+ } else {
169+ if (Math .round (this .bitmapY ) == 0 ) {
170+ this .rate = -this .rate ;
171+ this .verticalDirection = VerticalDirection .TOP_TO_BOTTOM ;
172+ }
119173 }
120174
121- setDuration (duration );
122- setRepeatCount (INFINITE );
123- setRepeatMode (REVERSE );
175+ this .bitmapX = 0 ;
176+ this .bitmapY += this .rate ;
124177 }
125178
126- @ Override
127- protected void applyTransformation (float interpolatedTime , Transformation t ) {
128- bitmapX = -interpolatedTime * slideWidth ;
129-
130- postInvalidate ();
131- }
179+ canvas .drawBitmap (this .bitmap , this .bitmapX , this .bitmapY , null );
180+ postInvalidate ();
132181 }
133182}
0 commit comments