Skip to content

Commit c6eaa18

Browse files
author
Wannes Ghielens
committed
#34 fixed setBounds not working
1 parent fd0ce0d commit c6eaa18

File tree

9 files changed

+83
-67
lines changed

9 files changed

+83
-67
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Previews work inside layout files, menu resource files sadly do not support prev
3333

3434
```groovy
3535
dependencies {
36-
compile 'net.steamcrafted:materialiconlib:1.1.2'
36+
compile 'net.steamcrafted:materialiconlib:1.1.3'
3737
}
3838
```
3939

app/app.iml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex" />
8383
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.1.1/jars" />
8484
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/23.1.1/jars" />
85-
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/net.steamcrafted/materialiconlib/1.1.2/jars" />
8685
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
8786
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
8887
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
@@ -101,8 +100,8 @@
101100
<orderEntry type="jdk" jdkName="Android API 25 Platform" jdkType="Android SDK" />
102101
<orderEntry type="sourceFolder" forTests="false" />
103102
<orderEntry type="library" exported="" name="support-v4-23.1.1" level="project" />
104-
<orderEntry type="library" exported="" name="materialiconlib-1.1.2" level="project" />
105103
<orderEntry type="library" exported="" name="support-annotations-23.1.1" level="project" />
106104
<orderEntry type="library" exported="" name="appcompat-v7-23.1.1" level="project" />
105+
<orderEntry type="module" module-name="materialiconlib" exported="" />
107106
</component>
108107
</module>

app/src/main/java/net/steamcrafted/materialiconview/MainActivity.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ protected void onCreate(Bundle savedInstanceState) {
3737
MaterialMenuInflater.with(toolbar1.getContext(), new SupportMenuInflater(toolbar1.getContext()))
3838
.inflate(R.menu.menu_nocolor, toolbar1.getMenu());
3939

40-
Toolbar toolbar2 = (Toolbar) findViewById(R.id.toolbar_2);
41-
MaterialMenuInflater.with(this)
42-
.inflate(R.menu.menu_nocolor, toolbar2.getMenu());
43-
44-
Toolbar toolbar3 = (Toolbar) findViewById(R.id.toolbar_3);
45-
MaterialMenuInflater.with(this)
46-
.inflate(R.menu.menu_nocolor, toolbar3.getMenu());
47-
4840

4941
// Activity Theme materialIconColor attribute (lowest priority default color)
5042
// View specific Theme e.g. app:theme="..." (2nd lowest in priority)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package net.steamcrafted.materialiconview;
2+
3+
import android.content.Context;
4+
import android.content.res.TypedArray;
5+
import android.graphics.Canvas;
6+
import android.graphics.Color;
7+
import android.graphics.Paint;
8+
import android.graphics.Rect;
9+
import android.graphics.drawable.Drawable;
10+
import android.text.TextPaint;
11+
import android.util.AttributeSet;
12+
import android.view.View;
13+
14+
import net.steamcrafted.materialiconlib.MaterialDrawableBuilder;
15+
16+
/**
17+
* TODO: document your custom view class.
18+
*/
19+
public class SetBoundsTest extends View {
20+
21+
private Drawable mDrawable;
22+
private Drawable mDrawable2;
23+
24+
public SetBoundsTest(Context context) {
25+
super(context);
26+
init(null, 0);
27+
}
28+
29+
public SetBoundsTest(Context context, AttributeSet attrs) {
30+
super(context, attrs);
31+
init(attrs, 0);
32+
}
33+
34+
public SetBoundsTest(Context context, AttributeSet attrs, int defStyle) {
35+
super(context, attrs, defStyle);
36+
init(attrs, defStyle);
37+
}
38+
39+
private void init(final AttributeSet attrs, final int defStyle) {
40+
mDrawable = MaterialDrawableBuilder.with(getContext())
41+
.setIcon(MaterialDrawableBuilder.IconValue.NUMERIC_7_BOX)
42+
.setColor(Color.RED)
43+
.setSizeDp(1)
44+
.build();
45+
46+
mDrawable2 = getResources().getDrawable(R.mipmap.ic_launcher);
47+
}
48+
49+
@Override
50+
protected void onDraw(Canvas canvas) {
51+
super.onDraw(canvas);
52+
53+
Paint p = new Paint();
54+
p.setColor(Color.BLACK);
55+
56+
Rect bounds = new Rect(0, 100, getMeasuredHeight(), getMeasuredHeight());
57+
58+
canvas.drawRect(bounds, p);
59+
mDrawable.setBounds(bounds);
60+
mDrawable.draw(canvas);
61+
}
62+
}

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,9 @@
8686
app:title="Toolbar context"
8787
app:theme="@style/ToolbarTheme" />
8888

89-
<android.support.v7.widget.Toolbar
90-
android:layout_width="match_parent"
91-
android:layout_height="?actionBarSize"
92-
android:id="@+id/toolbar_2"
93-
app:title="Activity context"
94-
app:theme="@style/ToolbarTheme" />
95-
96-
<android.support.v7.widget.Toolbar
89+
<net.steamcrafted.materialiconview.SetBoundsTest
9790
android:layout_width="match_parent"
98-
android:layout_height="?actionBarSize"
99-
android:id="@+id/toolbar_3"
100-
app:title="Default color"
101-
app:theme="@style/ToolbarTheme" />
91+
android:layout_height="144dp" />
10292

10393
<net.steamcrafted.materialiconlib.MaterialIconView
10494
android:layout_width="match_parent"

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

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,14 @@
1414
android:textColor="#fff"
1515
android:padding="12dp"
1616
android:gravity="center"
17-
android:id="@+id/icon"
1817
/>
1918

2019
<ImageView
2120
android:layout_width="48dp"
2221
android:layout_height="48dp"
2322
android:padding="12dp"
2423
android:background="@android:color/darker_gray"
25-
android:layout_below="@id/icon"
2624
android:layout_marginTop="8dp"
2725
android:id="@+id/image_icon"/>
2826

29-
<net.steamcrafted.materialiconlib.MaterialIconView
30-
android:layout_width="104dp"
31-
android:layout_height="104dp"
32-
app:materialIcon="emoticon_devil"
33-
app:materialIconSize="48dp"
34-
android:scaleType="center"
35-
android:textColor="#333"
36-
37-
android:gravity="center"
38-
android:background="#33EE2222"
39-
android:layout_toRightOf="@id/icon"
40-
android:layout_marginLeft="8dp"
41-
android:id="@+id/icon_1"
42-
android:layout_marginBottom="8dp"
43-
/>
44-
45-
<net.steamcrafted.materialiconlib.MaterialIconView
46-
android:layout_width="wrap_content"
47-
android:layout_height="wrap_content"
48-
android:padding="12dp"
49-
app:materialIcon="settings"
50-
app:materialIconColor="#F60000"
51-
app:materialIconSize="150dp"
52-
android:scaleType="center"
53-
android:layout_centerInParent="true"
54-
/>
55-
5627
</RelativeLayout>

materialiconlib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ apply plugin: 'com.android.library'
44
ext {
55
PUBLISH_GROUP_ID = 'net.steamcrafted'
66
PUBLISH_ARTIFACT_ID = 'materialiconlib'
7-
PUBLISH_VERSION = '1.1.2'
7+
PUBLISH_VERSION = '1.1.3'
88
}
99

1010
android {

materialiconlib/materialiconlib.iml

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,37 +62,34 @@
6262
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
6363
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
6464
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
65-
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
66-
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
67-
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
68-
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
69-
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
70-
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
71-
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
7265
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
7366
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
7467
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
7568
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
7669
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
7770
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
7871
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
72+
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
73+
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
74+
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
75+
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
76+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
77+
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
78+
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
7979
<excludeFolder url="file://$MODULE_DIR$/build/docs" />
80-
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
8180
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
8281
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
8382
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
8483
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
8584
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
8685
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/mockable-android-25.jar" />
87-
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
8886
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
89-
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
9087
<excludeFolder url="file://$MODULE_DIR$/build/ivy.xml" />
9188
<excludeFolder url="file://$MODULE_DIR$/build/libs" />
9289
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
9390
<excludeFolder url="file://$MODULE_DIR$/build/poms" />
9491
<excludeFolder url="file://$MODULE_DIR$/build/release" />
95-
<excludeFolder url="file://$MODULE_DIR$/build/release-1.1.2.zip" />
92+
<excludeFolder url="file://$MODULE_DIR$/build/release-1.1.3.zip" />
9693
<excludeFolder url="file://$MODULE_DIR$/build/reports" />
9794
<excludeFolder url="file://$MODULE_DIR$/build/test-results" />
9895
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />

materialiconlib/src/main/java/net/steamcrafted/materialiconlib/MaterialDrawableBuilder.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,17 @@ public int getIntrinsicWidth() {
254254
private final Rect mCachedRect = new Rect();
255255
@Override
256256
public void draw(Canvas canvas) {
257-
paint.setTextSize(getBounds().height());
257+
// Center drawable within available bounds
258+
int boundsWidth = getBounds().width();
259+
int boundsHeight = getBounds().height();
260+
int dimen = Math.min(boundsWidth, boundsHeight);
261+
262+
paint.setTextSize(dimen);
258263
String textValue = MaterialIconUtils.getIconString(icon.ordinal());
259264
paint.getTextBounds(textValue, 0, 1, mCachedRect);
260-
float textBottom = (getBounds().height() - mCachedRect.height()) / 2f + mCachedRect.height() - mCachedRect.bottom;
265+
float textBottom = getBounds().top + (boundsHeight - mCachedRect.height()) / 2f + mCachedRect.height() - mCachedRect.bottom;
261266

262-
canvas.drawText(textValue, getBounds().width() / 2f, textBottom, paint);
267+
canvas.drawText(textValue, getBounds().left + boundsWidth / 2f, textBottom, paint);
263268
}
264269

265270
@Override

0 commit comments

Comments
 (0)