Skip to content

Commit 5cc6bc6

Browse files
author
Ta Tonthongkam
committed
Add ImageRatioView
1 parent 924cf26 commit 5cc6bc6

File tree

26 files changed

+289
-6
lines changed

26 files changed

+289
-6
lines changed

settings.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
include ':workshop1'
1+
include ':workshop1', ':workshop2'

workshop1/src/main/kotlin/ta/com/workshop1/feature/common/TextWatcherAdapter.kt

-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ta.com.workshop1.feature.common
22

33
import android.text.Editable
4-
import android.databinding.ObservableField
54
import android.text.TextWatcher
65

76

@@ -10,10 +9,6 @@ class TextWatcherAdapter(private var field: (String) -> Unit) : TextWatcher {
109
private var isInEditMode = false
1110
var tmp = ""
1211

13-
init {
14-
15-
}
16-
1712
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
1813
}
1914

workshop2/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

workshop2/build.gradle

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-kapt'
4+
5+
android {
6+
compileSdkVersion 25
7+
buildToolsVersion "25.0.3"
8+
defaultConfig {
9+
applicationId "ta.com.workshop2"
10+
minSdkVersion 16
11+
targetSdkVersion 25
12+
versionCode 1
13+
versionName "1.0"
14+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15+
}
16+
buildTypes {
17+
release {
18+
minifyEnabled false
19+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20+
}
21+
}
22+
sourceSets {
23+
main.java.srcDirs += 'src/main/kotlin'
24+
main.jniLibs.srcDirs = ['libs']
25+
androidTest.java.srcDirs += 'src/androidTest/kotlin'
26+
test.java.srcDirs += 'src/test/kotlin'
27+
}
28+
dataBinding {
29+
dataBinding.enabled true
30+
}
31+
}
32+
33+
dependencies {
34+
compile fileTree(dir: 'libs', include: ['*.jar'])
35+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
36+
exclude group: 'com.android.support', module: 'support-annotations'
37+
})
38+
39+
//Standard library foe android
40+
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
41+
compile "com.android.support:appcompat-v7:$support_version"
42+
compile "com.android.support:design:$support_version"
43+
compile "com.android.support:support-vector-drawable:$support_version"
44+
compile "com.android.support:support-v4:$support_version"
45+
46+
//Android Architecture
47+
compile "android.arch.lifecycle:runtime:$aac_version"
48+
compile "android.arch.lifecycle:extensions:$aac_version"
49+
annotationProcessor "android.arch.lifecycle:compiler:$aac_version"
50+
51+
//Kotlin binding
52+
kapt 'com.android.databinding:compiler:2.3.2'
53+
54+
compile 'com.android.support.constraint:constraint-layout:1.0.2'
55+
testCompile 'junit:junit:4.12'
56+
}
57+
58+
repositories {
59+
mavenCentral()
60+
}
61+
62+
configurations.all {
63+
resolutionStrategy {
64+
force "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
65+
}
66+
}
67+
68+
afterEvaluate {
69+
android.sourceSets.all { sourceSet ->
70+
if (!sourceSet.name.startsWith('test') || !sourceSet.name.startsWith('androidTest')) {
71+
sourceSet.kotlin.setSrcDirs([])
72+
}
73+
}
74+
}
75+
76+
kapt {
77+
generateStubs = true
78+
}

workshop2/proguard-rules.pro

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/Ta/Library/Android/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
18+
19+
# Uncomment this to preserve the line number information for
20+
# debugging stack traces.
21+
#-keepattributes SourceFile,LineNumberTable
22+
23+
# If you keep the line number information, uncomment this to
24+
# hide the original source file name.
25+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ta.com.workshop2;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumentation test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("ta.com.workshop2", appContext.getPackageName());
25+
}
26+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ta.com.workshop2">
3+
4+
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
5+
android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
6+
android:supportsRtl="true" android:theme="@style/AppTheme">
7+
<activity android:name=".MainActivity">
8+
<intent-filter>
9+
<action android:name="android.intent.action.MAIN" />
10+
11+
<category android:name="android.intent.category.LAUNCHER" />
12+
</intent-filter>
13+
</activity>
14+
</application>
15+
16+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package ta.com.workshop2
2+
3+
import android.content.Context
4+
import android.support.annotation.Nullable
5+
import android.util.AttributeSet
6+
import android.view.ViewGroup
7+
import android.widget.ImageView
8+
9+
10+
11+
class ImageRatioView : ImageView {
12+
13+
var imageOrientation: Int? = 0
14+
var imageRatio: Int? = 0
15+
16+
constructor(context: Context) : super(context)
17+
18+
constructor(context: Context, @Nullable attrs: AttributeSet) : this(context, attrs, 0)
19+
20+
constructor(context: Context, @Nullable attrs: AttributeSet, defStyleAttr: Int) : this(context, attrs, defStyleAttr, 0)
21+
22+
constructor(context: Context, @Nullable attrs: AttributeSet, defStyleAttr: Int,
23+
defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
24+
val a = context.obtainStyledAttributes(attrs, R.styleable.ImageRatioView, defStyleAttr, defStyleRes)
25+
imageOrientation = a.getInt(R.styleable.ImageRatioView_imageOrientation, 0)
26+
imageRatio = a.getInt(R.styleable.ImageRatioView_imageRatio, 0)
27+
28+
a.recycle()
29+
}
30+
31+
32+
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
33+
34+
var width = MeasureSpec.getSize(widthMeasureSpec)
35+
var height = MeasureSpec.getSize(heightMeasureSpec)
36+
37+
when(imageOrientation) {
38+
0 -> {
39+
height = calculateRatio(width)
40+
}
41+
1 -> {
42+
width = calculateRatio(height)
43+
}
44+
}
45+
46+
this.setMeasuredDimension(width, height)
47+
this.layoutParams.width = width
48+
this.layoutParams.height = height
49+
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
50+
}
51+
52+
53+
private fun calculateRatio(measureSpec: Int) : Int {
54+
return when (imageRatio) {
55+
0 -> measureSpec
56+
1 -> measureSpec * (1/2)
57+
else -> {
58+
0
59+
}
60+
}
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ta.com.workshop2
2+
3+
import android.support.v7.app.AppCompatActivity
4+
import android.os.Bundle
5+
6+
class MainActivity : AppCompatActivity() {
7+
8+
override fun onCreate(savedInstanceState: Bundle?) {
9+
super.onCreate(savedInstanceState)
10+
setContentView(R.layout.activity_main)
11+
}
12+
}
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
6+
android:layout_height="match_parent" tools:context="ta.com.workshop2.MainActivity">
7+
8+
<ta.com.workshop2.ImageRatioView
9+
android:src="@drawable/menu"
10+
android:background="@color/colorAccent"
11+
android:layout_width="match_parent"
12+
android:layout_height="0dp"
13+
app:imageOrientation="landscape"
14+
app:imageRatio="W2L1" />
15+
</LinearLayout>
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<attr name="imageRatio" format="enum">
4+
<enum name="square" value="0" />
5+
<enum name="W2L1" value="1" />
6+
</attr>
7+
<attr name="imageOrientation" format="enum">
8+
<enum name="landscape" value="0"/>
9+
<enum name="portrait" value="1"/>
10+
</attr>
11+
12+
<declare-styleable name="ImageRatioView">
13+
<attr name="imageRatio" />
14+
<attr name="imageOrientation" />
15+
</declare-styleable>
16+
</resources>
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<color name="colorPrimary">#3F51B5</color>
4+
<color name="colorPrimaryDark">#303F9F</color>
5+
<color name="colorAccent">#FF4081</color>
6+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">Workshop2</string>
3+
</resources>
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<resources>
2+
3+
<!-- Base application theme. -->
4+
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
5+
<!-- Customize your theme here. -->
6+
<item name="colorPrimary">@color/colorPrimary</item>
7+
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
8+
<item name="colorAccent">@color/colorAccent</item>
9+
</style>
10+
11+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ta.com.workshop2;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
/**
8+
* Example local unit test, which will execute on the development machine (host).
9+
*
10+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
11+
*/
12+
public class ExampleUnitTest {
13+
@Test
14+
public void addition_isCorrect() throws Exception {
15+
assertEquals(4, 2 + 2);
16+
}
17+
}

0 commit comments

Comments
 (0)