Created
November 3, 2018 11:49
-
-
Save prashand/8b4223c7f96c7d5e300655c82f731b9c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<com.acme.anvilapp.widget.PicassoTargetableTextView | |
android:id="@+id/text_view" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</FrameLayout> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.acme.anvilapp.widget; | |
public class PicassoTargetableTextView extends android.support.v7.widget.AppCompatTextView { | |
private Picasso picasso = Picasso.get(); | |
private TheTarget target = new TheTarget(); | |
private enum Position {LEFT, TOP, RIGHT, BOTTOM} | |
private static final int DEFAULT_COMPOUND_DRAWABLE_SIZE = -1; | |
private int compoundDrawableWidth; | |
private int compoundDrawableHeight; | |
public PicassoTargetableTextView(Context context) { | |
this(context, null); | |
} | |
public PicassoTargetableTextView(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public PicassoTargetableTextView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
Picasso.Builder builder = new Picasso.Builder(context); | |
builder.listener((picasso, uri, exception) -> exception.printStackTrace()); | |
init(context, attrs); | |
} | |
private void init(Context context, AttributeSet attrs) { | |
final TypedArray typedArray = | |
context.obtainStyledAttributes(attrs, R.styleable.PicassoTargetableTextView); | |
compoundDrawableWidth = | |
typedArray.getDimensionPixelSize(R.styleable.PicassoTargetableTextView_compoundDrawableWidth, | |
DEFAULT_COMPOUND_DRAWABLE_SIZE); | |
compoundDrawableHeight = | |
typedArray.getDimensionPixelSize(R.styleable.PicassoTargetableTextView_compoundDrawableHeight, | |
DEFAULT_COMPOUND_DRAWABLE_SIZE); | |
typedArray.recycle(); | |
resizeCompoundDrawables(); | |
} | |
private void resizeCompoundDrawables() { | |
Drawable[] drawables = getCompoundDrawables(); | |
if (compoundDrawableWidth > 0 || compoundDrawableHeight > 0) { | |
for (Drawable drawable : drawables) { | |
if (drawable == null) { | |
continue; | |
} | |
Rect realBounds = drawable.getBounds(); | |
float scaleFactor = realBounds.height() / (float) realBounds.width(); | |
float drawableWidth = realBounds.width(); | |
float drawableHeight = realBounds.height(); | |
if (this.compoundDrawableWidth > 0) { | |
if (drawableWidth > this.compoundDrawableWidth) { | |
drawableWidth = this.compoundDrawableWidth; | |
drawableHeight = drawableWidth * scaleFactor; | |
} | |
} | |
if (this.compoundDrawableHeight > 0) { | |
if (drawableHeight > this.compoundDrawableHeight) { | |
drawableHeight = this.compoundDrawableHeight; | |
drawableWidth = drawableHeight / scaleFactor; | |
} | |
} | |
realBounds.right = realBounds.left + Math.round(drawableWidth); | |
realBounds.bottom = realBounds.top + Math.round(drawableHeight); | |
drawable.setBounds(realBounds); | |
} | |
} | |
super.setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]); | |
} | |
public int getCompoundDrawableWidth() { | |
return compoundDrawableWidth; | |
} | |
public void setCompoundDrawableWidth(int compoundDrawableWidth) { | |
this.compoundDrawableWidth = compoundDrawableWidth; | |
} | |
public int getCompoundDrawableHeight() { | |
return compoundDrawableHeight; | |
} | |
public void setCompoundDrawableHeight(int compoundDrawableHeight) { | |
this.compoundDrawableHeight = compoundDrawableHeight; | |
} | |
@Override | |
public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) { | |
super.setCompoundDrawables(left, top, right, bottom); | |
resizeCompoundDrawables(); | |
} | |
@Override | |
public void setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) { | |
super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom); | |
resizeCompoundDrawables(); | |
} | |
@Override | |
public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, | |
Drawable bottom) { | |
super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom); | |
resizeCompoundDrawables(); | |
} | |
@Override public void setCompoundDrawablesRelative(Drawable start, Drawable top, Drawable end, | |
Drawable bottom) { | |
super.setCompoundDrawablesRelative(start, top, end, bottom); | |
resizeCompoundDrawables(); | |
} | |
@Override public void setCompoundDrawablesRelativeWithIntrinsicBounds(int start, int top, int end, | |
int bottom) { | |
super.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom); | |
resizeCompoundDrawables(); | |
} | |
@Override | |
public void setCompoundDrawablesRelativeWithIntrinsicBounds(Drawable start, Drawable top, | |
Drawable end, Drawable bottom) { | |
super.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom); | |
resizeCompoundDrawables(); | |
} | |
public void load(String left, String top, String right, String bottom){ | |
if(left != null){ | |
picasso.load(left).into(target.position(Position.LEFT)); | |
} | |
if(top != null){ | |
picasso.load(top).into(target.position(Position.TOP)); | |
} | |
if(right != null){ | |
picasso.load(right).into(target.position(Position.RIGHT)); | |
} | |
if(bottom != null){ | |
picasso.load(bottom).into(target.position(Position.BOTTOM)); | |
} | |
} | |
private class TheTarget implements Target { | |
private Position position; | |
Target position(Position position){ | |
this.position = position; | |
return this; | |
} | |
@Override | |
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { | |
Drawable d = new BitmapDrawable(getResources(), bitmap); | |
if(position == Position.LEFT) { | |
setCompoundDrawablesWithIntrinsicBounds(d, null, null, null); | |
}else if(position == Position.TOP) { | |
setCompoundDrawablesWithIntrinsicBounds(null, d, null, null); | |
}else if(position == Position.RIGHT) { | |
setCompoundDrawablesWithIntrinsicBounds(null, null, d, null); | |
}else { | |
setCompoundDrawablesWithIntrinsicBounds(null, null, null, d); | |
} | |
setCompoundDrawablePadding(5); | |
} | |
@Override | |
public void onBitmapFailed(Exception e, Drawable errorDrawable) { | |
Log.e("LogTag", e.getMessage()); | |
} | |
@Override | |
public void onPrepareLoad(Drawable placeHolderDrawable) { } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SampleActivity extends AppCompatActivity{ | |
private PicassoTargetableTextView textView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_sample); | |
textView = findViewById(R.id.text_view); | |
setTextAndDrawable(); | |
} | |
void setTextAndDrawable(){ | |
textView.setText("ACME Corporation"); | |
textView.load( | |
"http://acmecorp.com/logo.jpg", //Drawable left | |
null, //Drawable top | |
null, //Drawable right | |
null //Drawable bottom | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
error: cannot find symbol
context.obtainStyledAttributes(attrs, R.styleable.PicassoTargetableTextView);