Skip to content

Commit 861bca3

Browse files
committed
Update 1.1.0
1 parent b13ac62 commit 861bca3

10 files changed

Lines changed: 135 additions & 69 deletions

File tree

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ Or use Gradle jCenter:
2525

2626
Or Gradle Maven Central:
2727

28-
compile 'com.github.gigamole.slideimageview:library:1.0.0'
28+
compile 'com.github.gigamole.slideimageview:library:1.1.0'
2929

3030
Or Maven:
3131

3232
<dependency>
3333
<groupId>com.github.gigamole.slideimageview</groupId>
3434
<artifactId>library</artifactId>
35-
<version>1.0.0</version>
35+
<version>1.1.0</version>
3636
<type>aar</type>
3737
</dependency>
3838

@@ -46,25 +46,25 @@ Sample
4646
You can set such parameters as:
4747

4848
- source
49-
- duration
50-
- slowdown
49+
- axis (HORIZONTAL | VERTICAL)
50+
- rate
5151

5252
Check out in code init:
5353

54-
SlideImageView slidedImageView = (SlideImageView) findViewById(R.id.img_slide);
55-
slidedImageView.setDuration(50000);
56-
slidedImageView.setSource(R.drawable.background);
57-
slidedImageView.setSlowdown(false);
54+
SlideImageView slideImageView = (SlideImageView) findViewById(R.id.img_horizontal_slide);
55+
slideImageView.setSource(R.drawable.wide_background);
56+
slideImageView.setRate(0.3f);
57+
slideImageView.setAxis(SlideImageView.Axis.HORIZONTAL);
5858

5959
And XML init:
6060

6161
<com.gigamole.slideimageview.lib.SlideImageView
62-
android:id="@+id/img_slide"
62+
android:id="@+id/img_vertical_slide"
6363
android:layout_width="match_parent"
64-
android:layout_height="match_parent"
65-
slide:source="@drawable/background"
66-
slide:duration="50000"
67-
slide:slowdown="false"/>
64+
android:layout_height="0dp"
65+
android:layout_weight="1"
66+
slide:source="@drawable/long_background"
67+
slide:axis="vertical"/>
6868

6969
Getting Help
7070
======

app/src/main/java/com/gigamole/slideimageview/MainActivity.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ protected void onCreate(Bundle savedInstanceState) {
2929
super.onCreate(savedInstanceState);
3030
setContentView(R.layout.activity_main);
3131

32-
// SlideImageView slidedImageView = (SlideImageView) findViewById(R.id.img_slide);
33-
// slidedImageView.setDuration(50000);
34-
// slidedImageView.setSource(R.drawable.background);
35-
// slidedImageView.setSlowdown(false);
32+
// SlideImageView slidedImageView = (SlideImageView) findViewById(R.id.img_horizontal_slide);
33+
// slidedImageView.setSource(R.drawable.wide_background);
34+
// slidedImageView.setRate(0.3f);
35+
// slidedImageView.setAxis(SlideImageView.Axis.HORIZONTAL);
3636
}
3737

3838

-247 KB
Binary file not shown.
121 KB
Loading
File renamed without changes.

app/src/main/res/layout/activity_main.xml

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,37 @@ limitations under the License.
1818
android:layout_height="match_parent"
1919
tools:context=".MainActivity">
2020

21-
<com.gigamole.slideimageview.lib.SlideImageView
22-
android:id="@+id/img_slide"
21+
<LinearLayout
2322
android:layout_width="match_parent"
2423
android:layout_height="match_parent"
25-
android:layout_centerHorizontal="true"
26-
android:layout_centerVertical="true"
27-
slide:source="@drawable/background"
28-
slide:duration="20000"
29-
slide:slowdown="false"/>
24+
android:weightSum="2"
25+
android:orientation="vertical">
26+
27+
<com.gigamole.slideimageview.lib.SlideImageView
28+
android:id="@+id/img_vertical_slide"
29+
android:layout_width="match_parent"
30+
android:layout_height="0dp"
31+
android:layout_weight="1"
32+
slide:source="@drawable/long_background"
33+
slide:axis="vertical"/>
34+
35+
<com.gigamole.slideimageview.lib.SlideImageView
36+
android:id="@+id/img_horizontal_slide"
37+
android:layout_width="match_parent"
38+
android:layout_height="0dp"
39+
android:layout_weight="1"
40+
slide:source="@drawable/wide_background"
41+
slide:axis="horizontal"/>
42+
43+
</LinearLayout>
3044

3145
<ImageView
3246
android:layout_width="match_parent"
3347
android:layout_height="wrap_content"
3448
android:layout_centerVertical="true"
3549
android:adjustViewBounds="true"
3650
android:alpha="0.75"
37-
android:padding="100dp"
51+
android:padding="75dp"
3852
android:src="@drawable/logo" />
3953

4054
</RelativeLayout>

library/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22
apply plugin: 'com.github.dcendents.android-maven'
33
apply plugin: "com.jfrog.bintray"
44

5-
version = "1.0.0"
5+
version = "1.1.0"
66

77
android {
88
compileSdkVersion 21
@@ -12,7 +12,7 @@ android {
1212
minSdkVersion 10
1313
targetSdkVersion 21
1414
versionCode 1
15-
versionName "1.0.0"
15+
versionName "1.1.0"
1616
}
1717
buildTypes {
1818
release {

library/library.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.github.gigamole.slideimageview" external.system.module.version="1.0.0" type="JAVA_MODULE" version="4">
2+
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.github.gigamole.slideimageview" external.system.module.version="1.1.0" type="JAVA_MODULE" version="4">
33
<component name="FacetManager">
44
<facet type="android-gradle" name="Android-Gradle">
55
<configuration>

library/src/main/java/com/gigamole/slideimageview/lib/SlideImageView.java

Lines changed: 88 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
import android.graphics.Canvas;
2424
import android.graphics.drawable.BitmapDrawable;
2525
import android.util.AttributeSet;
26-
import android.view.animation.Animation;
27-
import android.view.animation.LinearInterpolator;
28-
import android.view.animation.Transformation;
2926
import 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
}

library/src/main/res/values/attrs.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ limitations under the License.
1414

1515
<resources>
1616
<declare-styleable name="SlidedImageView">
17-
<attr name="duration" format="integer"/>
17+
<attr name="rate" format="float"/>
1818
<attr name="source" format="reference"/>
19-
<attr name="slowdown" format="boolean"/>
19+
<attr name="axis" format="enum">
20+
<enum name="horizontal" value="0"/>
21+
<enum name="vertical" value="1"/>
22+
</attr>
2023
</declare-styleable>
2124
</resources>

0 commit comments

Comments
 (0)