Skip to content

Commit

Permalink
#34 fixed setBounds not working
Browse files Browse the repository at this point in the history
  • Loading branch information
Wannes Ghielens committed Feb 22, 2017
1 parent fd0ce0d commit c6eaa18
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 67 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Previews work inside layout files, menu resource files sadly do not support prev

```groovy
dependencies {
compile 'net.steamcrafted:materialiconlib:1.1.2'
compile 'net.steamcrafted:materialiconlib:1.1.3'
}
```

Expand Down
3 changes: 1 addition & 2 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.1.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/23.1.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/net.steamcrafted/materialiconlib/1.1.2/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
Expand All @@ -101,8 +100,8 @@
<orderEntry type="jdk" jdkName="Android API 25 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="support-v4-23.1.1" level="project" />
<orderEntry type="library" exported="" name="materialiconlib-1.1.2" level="project" />
<orderEntry type="library" exported="" name="support-annotations-23.1.1" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-23.1.1" level="project" />
<orderEntry type="module" module-name="materialiconlib" exported="" />
</component>
</module>
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@ protected void onCreate(Bundle savedInstanceState) {
MaterialMenuInflater.with(toolbar1.getContext(), new SupportMenuInflater(toolbar1.getContext()))
.inflate(R.menu.menu_nocolor, toolbar1.getMenu());

Toolbar toolbar2 = (Toolbar) findViewById(R.id.toolbar_2);
MaterialMenuInflater.with(this)
.inflate(R.menu.menu_nocolor, toolbar2.getMenu());

Toolbar toolbar3 = (Toolbar) findViewById(R.id.toolbar_3);
MaterialMenuInflater.with(this)
.inflate(R.menu.menu_nocolor, toolbar3.getMenu());


// Activity Theme materialIconColor attribute (lowest priority default color)
// View specific Theme e.g. app:theme="..." (2nd lowest in priority)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package net.steamcrafted.materialiconview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import net.steamcrafted.materialiconlib.MaterialDrawableBuilder;

/**
* TODO: document your custom view class.
*/
public class SetBoundsTest extends View {

private Drawable mDrawable;
private Drawable mDrawable2;

public SetBoundsTest(Context context) {
super(context);
init(null, 0);
}

public SetBoundsTest(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, 0);
}

public SetBoundsTest(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs, defStyle);
}

private void init(final AttributeSet attrs, final int defStyle) {
mDrawable = MaterialDrawableBuilder.with(getContext())
.setIcon(MaterialDrawableBuilder.IconValue.NUMERIC_7_BOX)
.setColor(Color.RED)
.setSizeDp(1)
.build();

mDrawable2 = getResources().getDrawable(R.mipmap.ic_launcher);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

Paint p = new Paint();
p.setColor(Color.BLACK);

Rect bounds = new Rect(0, 100, getMeasuredHeight(), getMeasuredHeight());

canvas.drawRect(bounds, p);
mDrawable.setBounds(bounds);
mDrawable.draw(canvas);
}
}
14 changes: 2 additions & 12 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,9 @@
app:title="Toolbar context"
app:theme="@style/ToolbarTheme" />

<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:id="@+id/toolbar_2"
app:title="Activity context"
app:theme="@style/ToolbarTheme" />

<android.support.v7.widget.Toolbar
<net.steamcrafted.materialiconview.SetBoundsTest
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:id="@+id/toolbar_3"
app:title="Default color"
app:theme="@style/ToolbarTheme" />
android:layout_height="144dp" />

<net.steamcrafted.materialiconlib.MaterialIconView
android:layout_width="match_parent"
Expand Down
29 changes: 0 additions & 29 deletions app/src/main/res/layout/example.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,14 @@
android:textColor="#fff"
android:padding="12dp"
android:gravity="center"
android:id="@+id/icon"
/>

<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="12dp"
android:background="@android:color/darker_gray"
android:layout_below="@id/icon"
android:layout_marginTop="8dp"
android:id="@+id/image_icon"/>

<net.steamcrafted.materialiconlib.MaterialIconView
android:layout_width="104dp"
android:layout_height="104dp"
app:materialIcon="emoticon_devil"
app:materialIconSize="48dp"
android:scaleType="center"
android:textColor="#333"

android:gravity="center"
android:background="#33EE2222"
android:layout_toRightOf="@id/icon"
android:layout_marginLeft="8dp"
android:id="@+id/icon_1"
android:layout_marginBottom="8dp"
/>

<net.steamcrafted.materialiconlib.MaterialIconView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
app:materialIcon="settings"
app:materialIconColor="#F60000"
app:materialIconSize="150dp"
android:scaleType="center"
android:layout_centerInParent="true"
/>

</RelativeLayout>
2 changes: 1 addition & 1 deletion materialiconlib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'com.android.library'
ext {
PUBLISH_GROUP_ID = 'net.steamcrafted'
PUBLISH_ARTIFACT_ID = 'materialiconlib'
PUBLISH_VERSION = '1.1.2'
PUBLISH_VERSION = '1.1.3'
}

android {
Expand Down
19 changes: 8 additions & 11 deletions materialiconlib/materialiconlib.iml
Original file line number Diff line number Diff line change
Expand Up @@ -62,37 +62,34 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/docs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/mockable-android-25.jar" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/ivy.xml" />
<excludeFolder url="file://$MODULE_DIR$/build/libs" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/poms" />
<excludeFolder url="file://$MODULE_DIR$/build/release" />
<excludeFolder url="file://$MODULE_DIR$/build/release-1.1.2.zip" />
<excludeFolder url="file://$MODULE_DIR$/build/release-1.1.3.zip" />
<excludeFolder url="file://$MODULE_DIR$/build/reports" />
<excludeFolder url="file://$MODULE_DIR$/build/test-results" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,17 @@ public int getIntrinsicWidth() {
private final Rect mCachedRect = new Rect();

This comment has been minimized.

Copy link
@code-mc

code-mc Feb 22, 2017

Owner

Fix for #34

@Override
public void draw(Canvas canvas) {
paint.setTextSize(getBounds().height());
// Center drawable within available bounds
int boundsWidth = getBounds().width();
int boundsHeight = getBounds().height();
int dimen = Math.min(boundsWidth, boundsHeight);

paint.setTextSize(dimen);
String textValue = MaterialIconUtils.getIconString(icon.ordinal());
paint.getTextBounds(textValue, 0, 1, mCachedRect);
float textBottom = (getBounds().height() - mCachedRect.height()) / 2f + mCachedRect.height() - mCachedRect.bottom;
float textBottom = getBounds().top + (boundsHeight - mCachedRect.height()) / 2f + mCachedRect.height() - mCachedRect.bottom;

canvas.drawText(textValue, getBounds().width() / 2f, textBottom, paint);
canvas.drawText(textValue, getBounds().left + boundsWidth / 2f, textBottom, paint);
}

@Override
Expand Down

0 comments on commit c6eaa18

Please sign in to comment.