Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ android {
}

dependencies {
compile 'com.github.raveeshbhalla:proteus:1.0.2'
compile project(":proteus")
}
25 changes: 13 additions & 12 deletions demo/src/main/java/in/raveesh/proteusdemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.Random;

import in.raveesh.proteus.Button;
import in.raveesh.proteus.ImageView;


Expand All @@ -17,35 +18,35 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final ImageView imageView = (ImageView)findViewById(R.id.imageView);
final TextView textView = (TextView)findViewById(R.id.textView);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
final ImageView imageView = (ImageView) findViewById(R.id.imageView);
final TextView textView = (TextView) findViewById(R.id.textView);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Random random = new Random();
int s = random.nextInt(4 - 0);
int color;
switch (s) {
case 0:
imageView.setPaintedDrawableFromResource(R.drawable.ic_action_thumbs_up,
android.R.color.holo_blue_bright);
color = android.R.color.holo_blue_bright;
textView.setText("Blue");
break;
case 1:
imageView.setPaintedDrawableFromResource(R.drawable.ic_action_thumbs_up,
android.R.color.holo_green_light);
color = android.R.color.holo_green_light;
textView.setText("Green");
break;
case 2:
imageView.setPaintedDrawableFromResource(R.drawable.ic_action_thumbs_up,
android.R.color.holo_orange_light);
color = android.R.color.holo_orange_light;
textView.setText("Orange");
break;
case 3:
imageView.setPaintedDrawableFromResource(R.drawable.ic_action_thumbs_up,
android.R.color.holo_purple);
default:
color = android.R.color.holo_purple;
textView.setText("Purple");
break;
}
imageView.setPaintedDrawableFromResource(R.drawable.ic_action_thumbs_up, color);
button.setPaintResource(color);
}
});
}
Expand Down
4 changes: 3 additions & 1 deletion demo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
android:textSize="16sp"
android:layout_alignBottom="@+id/imageView"/>

<Button
<in.raveesh.proteus.Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:drawableLeft="@drawable/ic_action_thumbs_up"
app:paint="@android:color/white"
android:text="Change Color"/>
</RelativeLayout>
6 changes: 5 additions & 1 deletion proteus/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 21
compileSdkVersion 23
buildToolsVersion "21.1.2"

defaultConfig {
Expand All @@ -14,4 +14,8 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies{
compile 'com.android.support:support-annotations:23.1.1'
}
65 changes: 65 additions & 0 deletions proteus/src/main/java/in/raveesh/proteus/Button.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package in.raveesh.proteus;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.util.AttributeSet;

/**
* Created by Raveesh on 27/01/16.
*/
public class Button extends android.widget.Button {
private int proteusColorResource = 0;
private Drawable[] originalDrawables;

public Button(Context context) {
super(context);
}

public Button(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.ImageView);
originalDrawables = getCompoundDrawables();
setPaint(a.getColor(R.styleable.ImageView_paint, 0));
a.recycle();
}

public Button(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom){
if (proteusColorResource != 0){
if (left != null){
Rect bounds = left.getBounds();
left = new BitmapDrawable(getResources(), Proteus.getColoredBitmap(left, proteusColorResource));
left.setBounds(bounds);
}
if (top != null){
top = new BitmapDrawable(getResources(), Proteus.getColoredBitmap(top, proteusColorResource));
}
if (right != null){
right = new BitmapDrawable(getResources(), Proteus.getColoredBitmap(right, proteusColorResource));
}
if (bottom != null){
bottom = new BitmapDrawable(getResources(), Proteus.getColoredBitmap(bottom, proteusColorResource));
}
}
super.setCompoundDrawables(left, top, right, bottom);
}

public void setPaint(@ColorInt int paint){
proteusColorResource = paint;
setCompoundDrawables(originalDrawables[0], originalDrawables[1], originalDrawables[2], originalDrawables[3]);
}
public void setPaintResource(@ColorRes int paint){
proteusColorResource = getContext().getResources().getColor(paint);
setCompoundDrawables(originalDrawables[0], originalDrawables[1], originalDrawables[2], originalDrawables[3]);
}
}
22 changes: 18 additions & 4 deletions proteus/src/main/java/in/raveesh/proteus/ImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import android.content.res.TypedArray;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.util.AttributeSet;

/**
* Created by Raveesh on 02/04/15.
*/
public class ImageView extends android.widget.ImageView {
private int proteusColorResource = 0;
@ColorInt private int proteusColorResource = 0;

public ImageView(Context context) {
super(context);
Expand All @@ -22,13 +25,14 @@ public ImageView(Context context, AttributeSet attrs) {

public ImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.ImageView, defStyleAttr, 0);
TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.ImageView);
proteusColorResource = a.getColor(R.styleable.ImageView_paint, 0);
Drawable d = getDrawable();
if (d != null && proteusColorResource != 0) {
setImageDrawable(d);
}
a.recycle();
}

@Override
Expand All @@ -46,8 +50,18 @@ public void setImageDrawable(Drawable drawable) {
* @param drawable Resource id for image
* @param paintResource Resource id for paint
*/
public void setPaintedDrawableFromResource(int drawable, int paintResource) {
public void setPaintedDrawableFromResource(@DrawableRes int drawable, @ColorRes int paintResource) {
proteusColorResource = getContext().getResources().getColor(paintResource);
setImageDrawable(getContext().getResources().getDrawable(drawable));
}

/**
* Use this function to set an painted image using resource identifiers for drawable and paint
* @param drawable Resource id for image
* @param paint argb value for paint
*/
public void setPaintedDrawable(@DrawableRes int drawable, @ColorInt int paint) {
proteusColorResource = paint;
setImageDrawable(getContext().getResources().getDrawable(drawable));
}
}