├── settings.gradle
├── project.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── app
├── src
│ └── main
│ │ ├── res
│ │ ├── drawable-nodpi
│ │ │ ├── flag_uk.png
│ │ │ ├── flag_us.png
│ │ │ └── android1.jpg
│ │ ├── drawable-hdpi
│ │ │ ├── ic_warning.png
│ │ │ └── ic_launcher.png
│ │ ├── drawable-mdpi
│ │ │ ├── ic_warning.png
│ │ │ └── ic_launcher.png
│ │ ├── drawable-xhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_warning.png
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ └── attrs.xml
│ │ ├── drawable
│ │ │ └── background.xml
│ │ └── layout
│ │ │ ├── compound.xml
│ │ │ ├── double_image.xml
│ │ │ ├── box.xml
│ │ │ ├── aspect.xml
│ │ │ ├── entry_form.xml
│ │ │ └── box_small.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── example
│ │ └── customview
│ │ ├── BoxGridActivity.java
│ │ ├── AspectImageActivity.java
│ │ ├── DoubleImageActivity.java
│ │ ├── CompoundControlActivity.java
│ │ ├── MainActivity.java
│ │ └── widget
│ │ ├── EntryFormView.java
│ │ ├── AspectImageView.java
│ │ ├── BoxGridLayout.java
│ │ └── DoubleImageView.java
└── build.gradle
├── .gitignore
├── README.md
├── gradlew.bat
└── gradlew
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by IntelliJ IDEA
2 | # Project target.
3 | target=android-15
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/devunwired/custom-view-examples/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/drawable-nodpi/flag_uk.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/devunwired/custom-view-examples/HEAD/app/src/main/res/drawable-nodpi/flag_uk.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-nodpi/flag_us.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/devunwired/custom-view-examples/HEAD/app/src/main/res/drawable-nodpi/flag_us.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/ic_warning.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/devunwired/custom-view-examples/HEAD/app/src/main/res/drawable-hdpi/ic_warning.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-mdpi/ic_warning.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/devunwired/custom-view-examples/HEAD/app/src/main/res/drawable-mdpi/ic_warning.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-nodpi/android1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/devunwired/custom-view-examples/HEAD/app/src/main/res/drawable-nodpi/android1.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/devunwired/custom-view-examples/HEAD/app/src/main/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/devunwired/custom-view-examples/HEAD/app/src/main/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/devunwired/custom-view-examples/HEAD/app/src/main/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/ic_warning.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/devunwired/custom-view-examples/HEAD/app/src/main/res/drawable-xhdpi/ic_warning.png
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | CustomViewSamples
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Fri Mar 20 13:12:07 MDT 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'android'
2 |
3 | android {
4 | compileSdkVersion 15
5 | buildToolsVersion "20.0.0"
6 |
7 | defaultConfig {
8 | applicationId "com.example.customview"
9 | minSdkVersion 15
10 | targetSdkVersion 15
11 | }
12 |
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # built application files
2 | *.apk
3 | *.ap_
4 |
5 | # files for the dex VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # generated files
12 | bin/
13 | gen/
14 | out/
15 | build/
16 |
17 | # Local configuration file (sdk path, etc)
18 | local.properties
19 |
20 | # Eclipse project files
21 | .classpath
22 | .project
23 |
24 | # Proguard folder generated by Eclipse
25 | proguard/
26 |
27 | # Intellij project files
28 | *.iml
29 | *.ipr
30 | *.iws
31 | .idea/
32 | .gradle
--------------------------------------------------------------------------------
/app/src/main/res/drawable/background.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
8 |
11 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/compound.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
10 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/double_image.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
17 |
18 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/box.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | #Custom View Examples#
2 |
3 | This repository contains a series of sample code designed to highlight applications of creating custom Views and ViewGroups. Examples include:
4 |
5 | - Custom widget measurement
6 | - Compound control
7 | - Custom View
8 | - Custom ViewGroup
9 |
10 | ##License##
11 |
12 | **The code supplied here is covered under the MIT Open Source License:**
13 |
14 | Copyright (c) 2013 Wireless Designs, LLC
15 |
16 | Permission is hereby granted, free of charge, to any person obtaining
17 | a copy of this software and associated documentation files (the
18 | "Software"), to deal in the Software without restriction, including
19 | without limitation the rights to use, copy, modify, merge, publish,
20 | distribute, sublicense, and/or sell copies of the Software, and to
21 | permit persons to whom the Software is furnished to do so, subject to
22 | the following conditions:
23 |
24 | The above copyright notice and this permission notice shall be
25 | included in all copies or substantial portions of the Software.
26 |
27 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/aspect.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
9 |
10 |
15 |
21 |
27 |
33 |
34 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/customview/BoxGridActivity.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2013 Wireless Designs, LLC
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | */
23 | package com.example.customview;
24 |
25 | import android.app.Activity;
26 | import android.os.Bundle;
27 |
28 | public class BoxGridActivity extends Activity {
29 |
30 | public void onCreate(Bundle savedInstanceState) {
31 | super.onCreate(savedInstanceState);
32 | setContentView(R.layout.box);
33 | }
34 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/example/customview/AspectImageActivity.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2013 Wireless Designs, LLC
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | */
23 | package com.example.customview;
24 |
25 | import android.app.Activity;
26 | import android.os.Bundle;
27 |
28 | public class AspectImageActivity extends Activity {
29 |
30 | public void onCreate(Bundle savedInstanceState) {
31 | super.onCreate(savedInstanceState);
32 | setContentView(R.layout.aspect);
33 | }
34 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/example/customview/DoubleImageActivity.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2013 Wireless Designs, LLC
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | */
23 | package com.example.customview;
24 |
25 | import android.app.Activity;
26 | import android.os.Bundle;
27 |
28 | public class DoubleImageActivity extends Activity {
29 |
30 | public void onCreate(Bundle savedInstanceState) {
31 | super.onCreate(savedInstanceState);
32 | setContentView(R.layout.double_image);
33 | }
34 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/entry_form.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
12 |
19 |
20 |
29 |
37 |
38 |
46 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/customview/CompoundControlActivity.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2013 Wireless Designs, LLC
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | */
23 | package com.example.customview;
24 |
25 | import android.app.Activity;
26 | import android.os.Bundle;
27 | import android.widget.ArrayAdapter;
28 | import android.widget.ListView;
29 | import com.example.customview.widget.EntryFormView;
30 |
31 | public class CompoundControlActivity extends Activity implements EntryFormView.OnEntrySubmittedListener{
32 |
33 | private ArrayAdapter mAdapter;
34 |
35 | public void onCreate(Bundle savedInstanceState) {
36 | super.onCreate(savedInstanceState);
37 | setContentView(R.layout.compound);
38 |
39 | mAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1);
40 | ListView list = (ListView) findViewById(R.id.list);
41 | list.setAdapter(mAdapter);
42 |
43 | EntryFormView entryView = (EntryFormView) findViewById(R.id.entry_view);
44 | entryView.setOnEntrySubmittedListener(this);
45 | }
46 |
47 | @Override
48 | public void onEntrySubmitted(CharSequence name, CharSequence email) {
49 | mAdapter.add(name + ", " + email);
50 | mAdapter.notifyDataSetChanged();
51 | }
52 | }
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/box_small.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
15 |
16 |
20 |
21 |
25 |
26 |
30 |
31 |
35 |
36 |
40 |
41 |
45 |
46 |
50 |
51 |
55 |
56 |
60 |
61 |
65 |
66 |
70 |
71 |
75 |
76 |
80 |
81 |
85 |
86 |
90 |
91 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/customview/MainActivity.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2013 Wireless Designs, LLC
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | */
23 | package com.example.customview;
24 |
25 | import android.app.Activity;
26 | import android.content.Intent;
27 | import android.os.Bundle;
28 | import android.view.View;
29 | import android.widget.AdapterView;
30 | import android.widget.ArrayAdapter;
31 | import android.widget.ListView;
32 |
33 | public class MainActivity extends Activity implements AdapterView.OnItemClickListener {
34 |
35 | private static final String[] ITEMS = {"AspectImageView", "Compound Control", "DoubleImageView", "BoxGridLayout"};
36 |
37 | public void onCreate(Bundle savedInstanceState) {
38 | super.onCreate(savedInstanceState);
39 | ListView list = new ListView(this);
40 | ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, ITEMS);
41 |
42 | list.setAdapter(adapter);
43 | list.setOnItemClickListener(this);
44 |
45 | setContentView(list);
46 | }
47 |
48 | @Override
49 | public void onItemClick(AdapterView> parent, View view, int position, long id) {
50 | switch (position) {
51 | case 0: {
52 | Intent i = new Intent(this, AspectImageActivity.class);
53 | startActivity(i);
54 | break;
55 | }
56 | case 1: {
57 | Intent i = new Intent(this, CompoundControlActivity.class);
58 | startActivity(i);
59 | break;
60 | }
61 | case 2: {
62 | Intent i = new Intent(this, DoubleImageActivity.class);
63 | startActivity(i);
64 | break;
65 | }
66 | case 3: {
67 | Intent i = new Intent(this, BoxGridActivity.class);
68 | startActivity(i);
69 | break;
70 | }
71 | default:
72 | break;
73 | }
74 | }
75 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/example/customview/widget/EntryFormView.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2013 Wireless Designs, LLC
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | */
23 | package com.example.customview.widget;
24 |
25 | import android.content.Context;
26 | import android.util.AttributeSet;
27 | import android.view.LayoutInflater;
28 | import android.view.View;
29 | import android.view.inputmethod.InputMethodManager;
30 | import android.widget.EditText;
31 | import android.widget.RelativeLayout;
32 | import com.example.customview.R;
33 |
34 | public class EntryFormView extends RelativeLayout implements View.OnClickListener {
35 |
36 | public interface OnEntrySubmittedListener {
37 | public void onEntrySubmitted(CharSequence name, CharSequence email);
38 | }
39 |
40 | private EditText mNameText, mEmailText;
41 | private OnEntrySubmittedListener mListener;
42 |
43 | public EntryFormView(Context context) {
44 | this(context, null);
45 | }
46 |
47 | public EntryFormView(Context context, AttributeSet attrs) {
48 | this(context, attrs, 0);
49 | }
50 |
51 | public EntryFormView(Context context, AttributeSet attrs, int defStyle) {
52 | super(context, attrs, defStyle);
53 | //Inflate and attach the content
54 | LayoutInflater.from(context).inflate(R.layout.entry_form, this);
55 |
56 | setBackgroundResource(R.drawable.background);
57 |
58 | mNameText = (EditText) findViewById(R.id.name_text);
59 | mEmailText = (EditText) findViewById(R.id.email_text);
60 |
61 | findViewById(R.id.save_button).setOnClickListener(this);
62 | }
63 |
64 | public void setOnEntrySubmittedListener(OnEntrySubmittedListener listener) {
65 | mListener = listener;
66 | }
67 |
68 | @Override
69 | public void onClick(View v) {
70 | //Hide the keyboard
71 | InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
72 | imm.hideSoftInputFromWindow(mNameText.getWindowToken(), 0);
73 |
74 | //Notify the listener
75 | if (mListener != null) {
76 | mListener.onEntrySubmitted(mNameText.getText(), mEmailText.getText());
77 | }
78 |
79 | //Clear the fields
80 | mNameText.setText(null);
81 | mEmailText.setText(null);
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/customview/widget/AspectImageView.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2013 Wireless Designs, LLC
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | */
23 | package com.example.customview.widget;
24 |
25 | import android.content.Context;
26 | import android.graphics.drawable.Drawable;
27 | import android.util.AttributeSet;
28 | import android.view.View;
29 | import android.widget.ImageView;
30 |
31 | /**
32 | * ImageView that sizes itself to match the proper aspect ratio size of
33 | * the image content
34 | */
35 | public class AspectImageView extends ImageView {
36 |
37 | public AspectImageView(Context context) {
38 | super(context);
39 | }
40 |
41 | public AspectImageView(Context context, AttributeSet attrs) {
42 | super(context, attrs);
43 | }
44 |
45 | public AspectImageView(Context context, AttributeSet attrs, int defStyle) {
46 | super(context, attrs, defStyle);
47 | }
48 |
49 | @Override
50 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
51 | //Figure out the aspect ratio of the image content
52 | int desiredSize;
53 | float aspect;
54 | Drawable d = getDrawable();
55 | if (d == null) {
56 | desiredSize = 0;
57 | aspect = 1f;
58 | } else {
59 | desiredSize = d.getIntrinsicWidth();
60 | aspect = (float) d.getIntrinsicWidth() / (float) d.getIntrinsicHeight();
61 | }
62 | //Get the width based on the measure specs
63 | int widthSize = View.resolveSize(desiredSize, widthMeasureSpec);
64 |
65 | //Calculate height based on aspect
66 | int heightSize = (int)(widthSize / aspect);
67 |
68 | //Make sure the height we want is not too large
69 | int specMode = MeasureSpec.getMode(heightMeasureSpec);
70 | int specSize = MeasureSpec.getSize(heightMeasureSpec);
71 | if (specMode == MeasureSpec.AT_MOST || specMode == MeasureSpec.EXACTLY) {
72 | //If our measurement exceeds the max height, shrink back
73 | if (heightSize > specSize) {
74 | heightSize = specSize;
75 | widthSize = (int)(heightSize * aspect);
76 | }
77 | }
78 |
79 | //MUST do this to store the measurements
80 | setMeasuredDimension(widthSize, heightSize);
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/customview/widget/BoxGridLayout.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2013 Wireless Designs, LLC
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | */
23 | package com.example.customview.widget;
24 |
25 | import android.content.Context;
26 | import android.content.res.TypedArray;
27 | import android.graphics.Canvas;
28 | import android.graphics.Color;
29 | import android.graphics.Paint;
30 | import android.util.AttributeSet;
31 | import android.view.View;
32 | import android.view.ViewGroup;
33 | import com.example.customview.R;
34 |
35 | public class BoxGridLayout extends ViewGroup {
36 |
37 | private static final int DEFAULT_COUNT = 3;
38 |
39 | private Paint mGridPaint;
40 |
41 | private int mColumnCount;
42 | private int mMaxChildren;
43 |
44 | public BoxGridLayout(Context context) {
45 | this(context, null);
46 | }
47 |
48 | public BoxGridLayout(Context context, AttributeSet attrs) {
49 | this(context, attrs, 0);
50 | }
51 |
52 | public BoxGridLayout(Context context, AttributeSet attrs, int defStyle) {
53 | super(context, attrs, defStyle);
54 |
55 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BoxGridLayout, 0, defStyle);
56 |
57 | int strokeWidth = a.getDimensionPixelSize(R.styleable.BoxGridLayout_separatorWidth, 0);
58 | int strokeColor = a.getColor(R.styleable.BoxGridLayout_separatorColor, Color.WHITE);
59 | mColumnCount = a.getInteger(R.styleable.BoxGridLayout_numColumns, DEFAULT_COUNT);
60 | mMaxChildren = mColumnCount * mColumnCount;
61 |
62 | a.recycle();
63 |
64 | mGridPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
65 | mGridPaint.setStyle(Paint.Style.STROKE);
66 | mGridPaint.setColor(strokeColor);
67 | mGridPaint.setStrokeWidth(strokeWidth);
68 | }
69 |
70 | @Override
71 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
72 | int widthSize, heightSize;
73 |
74 | //Get the width based on the measure specs
75 | widthSize = getDefaultSize(0, widthMeasureSpec);
76 |
77 | //Get the height based on measure specs
78 | heightSize = getDefaultSize(0, heightMeasureSpec);
79 |
80 | int majorDimension = Math.min(widthSize, heightSize);
81 | //Measure all child views
82 | int blockDimension = majorDimension / mColumnCount;
83 | int blockSpec = MeasureSpec.makeMeasureSpec(blockDimension, MeasureSpec.EXACTLY);
84 | measureChildren(blockSpec, blockSpec);
85 |
86 | //MUST call this to save our own dimensions
87 | setMeasuredDimension(majorDimension, majorDimension);
88 | }
89 |
90 | @Override
91 | protected void onLayout(boolean changed, int l, int t, int r, int b) {
92 | int row, col, left, top;
93 | for (int i=0; i < getChildCount(); i++) {
94 | row = i / mColumnCount;
95 | col = i % mColumnCount;
96 | View child = getChildAt(i);
97 | left = col * child.getMeasuredWidth();
98 | top = row * child.getMeasuredHeight();
99 |
100 | child.layout(left, top, left + child.getMeasuredWidth(), top + child.getMeasuredHeight());
101 | }
102 | }
103 |
104 | @Override
105 | protected void dispatchDraw(Canvas canvas) {
106 | //Let the framework do its thing
107 | super.dispatchDraw(canvas);
108 |
109 | //Draw the grid lines
110 | for (int i=0; i <= getWidth(); i += (getWidth() / mColumnCount)) {
111 | canvas.drawLine(i, 0, i, getHeight(), mGridPaint);
112 | }
113 | for (int i=0; i <= getHeight(); i += (getHeight() / mColumnCount)) {
114 | canvas.drawLine(0, i, getWidth(), i, mGridPaint);
115 | }
116 | }
117 |
118 | @Override
119 | public void addView(View child) {
120 | if (getChildCount() > mMaxChildren-1) {
121 | throw new IllegalStateException("BoxGridLayout cannot have more than "+mMaxChildren+" direct children");
122 | }
123 |
124 | super.addView(child);
125 | }
126 |
127 | @Override
128 | public void addView(View child, int index) {
129 | if (getChildCount() > mMaxChildren-1) {
130 | throw new IllegalStateException("BoxGridLayout cannot have more than "+mMaxChildren+" direct children");
131 | }
132 |
133 | super.addView(child, index);
134 | }
135 |
136 | @Override
137 | public void addView(View child, int index, LayoutParams params) {
138 | if (getChildCount() > mMaxChildren-1) {
139 | throw new IllegalStateException("BoxGridLayout cannot have more than "+mMaxChildren+" direct children");
140 | }
141 |
142 | super.addView(child, index, params);
143 | }
144 |
145 | @Override
146 | public void addView(View child, LayoutParams params) {
147 | if (getChildCount() > mMaxChildren-1) {
148 | throw new IllegalStateException("BoxGridLayout cannot have more than "+mMaxChildren+" direct children");
149 | }
150 |
151 | super.addView(child, params);
152 | }
153 |
154 | @Override
155 | public void addView(View child, int width, int height) {
156 | if (getChildCount() > mMaxChildren-1) {
157 | throw new IllegalStateException("BoxGridLayout cannot have more than "+mMaxChildren+" direct children");
158 | }
159 |
160 | super.addView(child, width, height);
161 | }
162 | }
163 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/customview/widget/DoubleImageView.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2013 Wireless Designs, LLC
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | */
23 | package com.example.customview.widget;
24 |
25 | import android.content.Context;
26 | import android.content.res.TypedArray;
27 | import android.graphics.Canvas;
28 | import android.graphics.Paint;
29 | import android.graphics.Point;
30 | import android.graphics.drawable.Drawable;
31 | import android.text.Layout;
32 | import android.text.StaticLayout;
33 | import android.text.TextPaint;
34 | import android.text.TextUtils;
35 | import android.util.AttributeSet;
36 | import android.view.View;
37 | import com.example.customview.R;
38 |
39 | public class DoubleImageView extends View {
40 |
41 | /* Image Contents */
42 | private Drawable mLeftDrawable, mRightDrawable;
43 | /* Text Contents */
44 | private CharSequence mText;
45 | private StaticLayout mTextLayout;
46 | /* Text Drawing */
47 | private TextPaint mTextPaint;
48 | private Point mTextOrigin;
49 | private int mSpacing;
50 |
51 | public DoubleImageView(Context context) {
52 | this(context, null);
53 | }
54 |
55 | public DoubleImageView(Context context, AttributeSet attrs) {
56 | this(context, attrs, 0);
57 | }
58 |
59 | public DoubleImageView(Context context, AttributeSet attrs, int defStyle) {
60 | super(context, attrs, defStyle);
61 | mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
62 | mTextOrigin = new Point(0, 0);
63 |
64 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DoubleImageView, 0, defStyle);
65 |
66 | Drawable d = a.getDrawable(R.styleable.DoubleImageView_android_drawableLeft);
67 | if (d != null) {
68 | setLeftDrawable(d);
69 | }
70 |
71 | d = a.getDrawable(R.styleable.DoubleImageView_android_drawableRight);
72 | if (d != null) {
73 | setRightDrawable(d);
74 | }
75 |
76 | int spacing = a.getDimensionPixelSize(R.styleable.DoubleImageView_android_spacing, 0);
77 | setSpacing(spacing);
78 |
79 | int color = a.getColor(R.styleable.DoubleImageView_android_textColor, 0);
80 | mTextPaint.setColor(color);
81 |
82 | int rawSize = a.getDimensionPixelSize(R.styleable.DoubleImageView_android_textSize, 0);
83 | mTextPaint.setTextSize(rawSize);
84 |
85 | CharSequence text = a.getText(R.styleable.DoubleImageView_android_text);
86 | setText(text);
87 |
88 | a.recycle();
89 | }
90 |
91 | public void setLeftDrawableResource(int resId) {
92 | Drawable d = getResources().getDrawable(resId);
93 | setLeftDrawable(d);
94 | }
95 |
96 | public void setLeftDrawable(Drawable left) {
97 | mLeftDrawable = left;
98 | updateContentBounds();
99 | invalidate();
100 | }
101 |
102 | public void setRightDrawableResource(int resId) {
103 | Drawable d = getResources().getDrawable(resId);
104 | setRightDrawable(d);
105 | }
106 |
107 | public void setRightDrawable(Drawable right) {
108 | mRightDrawable = right;
109 | updateContentBounds();
110 | invalidate();
111 | }
112 |
113 | public void setText(int resId) {
114 | CharSequence text = getResources().getText(resId);
115 | setText(text);
116 | }
117 |
118 | public void setText(CharSequence text) {
119 | if (!TextUtils.equals(mText, text)) {
120 | mText = text;
121 | updateContentBounds();
122 | invalidate();
123 | }
124 | }
125 |
126 | public void setSpacing(int spacing) {
127 | mSpacing = spacing;
128 | updateContentBounds();
129 | invalidate();
130 | }
131 |
132 | @Override
133 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
134 | //Get the width measurement
135 | int widthSize = View.resolveSize(getDesiredWidth(), widthMeasureSpec);
136 |
137 | //Get the height measurement
138 | int heightSize = View.resolveSize(getDesiredHeight(), heightMeasureSpec);
139 |
140 | //MUST call this to store the measurements
141 | setMeasuredDimension(widthSize, heightSize);
142 | }
143 |
144 | private int getDesiredWidth() {
145 | int leftWidth;
146 | if (mLeftDrawable == null) {
147 | leftWidth = 0;
148 | } else {
149 | leftWidth = mLeftDrawable.getIntrinsicWidth();
150 | }
151 |
152 | int rightWidth;
153 | if (mRightDrawable == null) {
154 | rightWidth = 0;
155 | } else {
156 | rightWidth = mRightDrawable.getIntrinsicWidth();
157 | }
158 |
159 | int textWidth;
160 | if (mTextLayout == null) {
161 | textWidth = 0;
162 | } else {
163 | textWidth = mTextLayout.getWidth();
164 | }
165 |
166 | return (int)(leftWidth * 0.67f) + (int)(rightWidth * 0.67f) + mSpacing + textWidth;
167 | }
168 |
169 | private int getDesiredHeight() {
170 | int leftHeight;
171 | if (mLeftDrawable == null) {
172 | leftHeight = 0;
173 | } else {
174 | leftHeight = mLeftDrawable.getIntrinsicHeight();
175 | }
176 |
177 | int rightHeight;
178 | if (mRightDrawable == null) {
179 | rightHeight = 0;
180 | } else {
181 | rightHeight = mRightDrawable.getIntrinsicHeight();
182 | }
183 |
184 | return (int)(leftHeight * 0.67f) + (int)(rightHeight * 0.67f);
185 | }
186 |
187 | private void updateContentBounds() {
188 | if (mText == null) {
189 | mText = "";
190 | }
191 | float textWidth = mTextPaint.measureText(mText, 0, mText.length());
192 | mTextLayout = new StaticLayout(mText, mTextPaint, (int)textWidth,
193 | Layout.Alignment.ALIGN_CENTER, 1f, 0f, true);
194 |
195 | int left = (getWidth() - getDesiredWidth()) / 2;
196 | int top = (getHeight() - getDesiredHeight()) / 2;
197 |
198 | if (mLeftDrawable != null) {
199 | mLeftDrawable.setBounds(left, top,
200 | left + mLeftDrawable.getIntrinsicWidth(), top + mLeftDrawable.getIntrinsicHeight());
201 |
202 | left += (mLeftDrawable.getIntrinsicWidth() * 0.33f);
203 | top += (mLeftDrawable.getIntrinsicHeight() * 0.33f);
204 | }
205 |
206 | if (mRightDrawable != null) {
207 | mRightDrawable.setBounds(left, top,
208 | left + mRightDrawable.getIntrinsicWidth(), top + mRightDrawable.getIntrinsicHeight());
209 |
210 | left = mRightDrawable.getBounds().right + mSpacing;
211 | }
212 |
213 | if (mTextLayout != null) {
214 | top = (getHeight() - mTextLayout.getHeight()) / 2;
215 | mTextOrigin.set(left, top);
216 | }
217 | }
218 |
219 | @Override
220 | protected void onSizeChanged(int w, int h, int oldw, int oldh) {
221 | if (w != oldw || h != oldh) {
222 | updateContentBounds();
223 | }
224 | }
225 |
226 | @Override
227 | protected void onDraw(Canvas canvas) {
228 | if (mLeftDrawable != null) {
229 | mLeftDrawable.draw(canvas);
230 | }
231 |
232 | if (mTextLayout != null) {
233 | canvas.save();
234 | canvas.translate(mTextOrigin.x, mTextOrigin.y);
235 |
236 | mTextLayout.draw(canvas);
237 |
238 | canvas.restore();
239 | }
240 |
241 | if (mRightDrawable != null) {
242 | mRightDrawable.draw(canvas);
243 | }
244 | }
245 | }
--------------------------------------------------------------------------------