Skip to content

Commit

Permalink
Merge pull request #1 from Expensify/mattc-add-images
Browse files Browse the repository at this point in the history
Adding optional images for left-to-right languages
  • Loading branch information
capezzbr committed Aug 16, 2018
2 parents ca1addf + 1b49e13 commit 71a8ca0
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ public class TagContainerLayout extends ViewGroup {
*/
private List<String> mTags;

/**
* Default image for new tags
*/
private int mDefaultImageDrawableID = -1;

/**
* Can drag TagView(default false)
*/
Expand Down Expand Up @@ -525,7 +530,12 @@ private void onAddTag(String text, int position) {
if (position < 0 || position > mChildViews.size()) {
throw new RuntimeException("Illegal position!");
}
TagView tagView = new TagView(getContext(), text);
TagView tagView;
if (mDefaultImageDrawableID != -1) {
tagView = new TagView(getContext(), text, mDefaultImageDrawableID);
} else {
tagView = new TagView(getContext(), text);
}
initTagView(tagView, position);
mChildViews.add(position, tagView);
if (position < mChildViews.size()) {
Expand Down Expand Up @@ -1000,6 +1010,24 @@ public void setSensitivity(float sensitivity) {
this.mSensitivity = sensitivity;
}

/**
* Get default tag image
*
* @return
*/
public int getDefaultImageDrawableID() {
return mDefaultImageDrawableID;
}

/**
* Set default image for tags.
*
* @param imageID
*/
public void setDefaultImageDrawableID(int imageID) {
this.mDefaultImageDrawableID = imageID;
}

/**
* Set max line count for TagContainerLayout
*
Expand Down
38 changes: 36 additions & 2 deletions androidtagview/src/main/java/co/lujun/androidtagview/TagView.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.os.Build;
import android.support.v4.widget.ViewDragHelper;
Expand Down Expand Up @@ -119,6 +123,8 @@ public class TagView extends View {

private ValueAnimator mRippleValueAnimator;

private Bitmap mBitmapImage;

private boolean mEnableCross;

private float mCrossAreaWidth;
Expand Down Expand Up @@ -149,6 +155,12 @@ public TagView(Context context, String text){
init(context, text);
}

public TagView(Context context, String text, int defaultImageID){
super(context);
init(context, text);
mBitmapImage = BitmapFactory.decodeResource(getResources(), defaultImageID);
}

private void init(Context context, String text){
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mRipplePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Expand Down Expand Up @@ -186,7 +198,7 @@ private void onDealText(){
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = mVerticalPadding * 2 + (int) fontH;
int width = mHorizontalPadding * 2 + (int) fontW + (isEnableCross() ? height : 0);
int width = mHorizontalPadding * 2 + (int) fontW + (isEnableCross() ? height : 0) + (isEnableImage() ? height : 0);
mCrossAreaWidth = Math.min(Math.max(mCrossAreaWidth, height), width);
setMeasuredDimension(width, height);
}
Expand Down Expand Up @@ -233,12 +245,15 @@ protected void onDraw(Canvas canvas) {
}
} else {
canvas.drawText(mAbstractText,
(isEnableCross() ? getWidth() - getHeight() : getWidth()) / 2 - fontW / 2,
(isEnableCross() ? getWidth() - getHeight() : getWidth()) / 2 - fontW / 2 + (isEnableImage() ? getHeight() / 2 : 0),
getHeight() / 2 + fontH / 2 - bdDistance, mPaint);
}

// draw cross
drawCross(canvas);

// draw image
drawImage(canvas);
}

@Override
Expand Down Expand Up @@ -326,6 +341,18 @@ private boolean isClickCrossArea(MotionEvent event){
return event.getX() >= getWidth() - mCrossAreaWidth;
}

private void drawImage(Canvas canvas){
if (isEnableImage()) {
Bitmap scaledImageBitmap = Bitmap.createScaledBitmap(mBitmapImage, Math.round(getHeight() - mBorderWidth), Math.round(getHeight() - mBorderWidth), false);

Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(new BitmapShader(scaledImageBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
RectF rect = new RectF(mBorderWidth, mBorderWidth, getHeight() - mBorderWidth, getHeight() - mBorderWidth);
canvas.drawRoundRect(rect, rect.height()/2, rect.height()/2, paint);
}
}

private void drawCross(Canvas canvas){
if (isEnableCross()){
mCrossAreaPadding = mCrossAreaPadding > getHeight() / 2 ? getHeight() / 2 :
Expand Down Expand Up @@ -456,6 +483,11 @@ public void setIsViewClickable(boolean clickable) {
this.isViewClickable = clickable;
}

public void setImage(Bitmap newImage) {
this.mBitmapImage = newImage;
this.invalidate();
}

public interface OnTagClickListener{
void onTagClick(int position, String text);
void onTagLongClick(int position, String text);
Expand Down Expand Up @@ -491,6 +523,8 @@ public void setBdDistance(float bdDistance) {
this.bdDistance = bdDistance;
}

public boolean isEnableImage() { return mBitmapImage != null && mTextDirection != View.TEXT_DIRECTION_RTL; }

public boolean isEnableCross() {
return mEnableCross;
}
Expand Down
6 changes: 6 additions & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ android {
}
}

repositories {
mavenCentral()
maven { url 'https://maven.google.com' }
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':androidtagview')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:design:25.0.1'
compile 'com.github.bumptech.glide:glide:4.1.1'
}
2 changes: 1 addition & 1 deletion sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
</intent-filter>
</activity>
</application>

<uses-permission android:name="android.permission.INTERNET" />
</manifest>
34 changes: 34 additions & 0 deletions sample/src/main/java/co/lujun/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
Expand All @@ -21,6 +22,11 @@
import co.lujun.androidtagview.TagContainerLayout;
import co.lujun.androidtagview.TagView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.transition.Transition;
import com.bumptech.glide.request.target.SimpleTarget;

public class MainActivity extends AppCompatActivity {

private TagContainerLayout mTagContainerLayout1, mTagContainerLayout2,
Expand Down Expand Up @@ -73,6 +79,8 @@ protected void onCreate(Bundle savedInstanceState) {
mTagContainerLayout4 = (TagContainerLayout) findViewById(R.id.tagcontainerLayout4);
mTagcontainerLayout5 = (TagContainerLayout) findViewById(R.id.tagcontainerLayout5);

mTagContainerLayout1.setDefaultImageDrawableID(R.drawable.yellow_avatar);

// Set custom click listener
mTagContainerLayout1.setOnTagClickListener(new TagView.OnTagClickListener() {
@Override
Expand Down Expand Up @@ -132,6 +140,7 @@ public void onTagCrossClick(int position) {

// After you set your own attributes for TagView, then set tag(s) or add tag(s)
mTagContainerLayout1.setTags(list1);
loadImages(list1);
mTagContainerLayout2.setTags(list2);
mTagContainerLayout3.setTags(list3);
mTagContainerLayout4.setTags(list4);
Expand Down Expand Up @@ -172,6 +181,31 @@ public void onClick(View v) {
// recyclerView.setAdapter(adapter);
}

private void loadImages(List<String> list) {
String[] avatars = new String[]{"https://forums.oneplus.com/data/avatars/m/231/231279.jpg",
"https://d1marr3m5x4iac.cloudfront.net/images/block/movies/17214/17214_aa.jpg",
"https://lh3.googleusercontent.com/-KSI1bJ1aVS4/AAAAAAAAAAI/AAAAAAAAB9c/Vrgt6WyS5OU/il/photo.jpg"};

for (int i=0; i<list.size(); i++) {
final int index = i;
Glide.with(mTagContainerLayout1.getContext())
.asBitmap()
.load(avatars[i % avatars.length])
.apply(new RequestOptions().override(85))
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
mTagContainerLayout1.getTagView(index).setImage(resource);
}
});
try {
Thread.sleep(50);
} catch (InterruptedException e) {
System.err.println(e.getMessage());
}
}
}

public class TagRecyclerViewAdapter
extends RecyclerView.Adapter<TagRecyclerViewAdapter.TagViewHolder> {

Expand Down
Binary file added sample/src/main/res/drawable/yellow_avatar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 71a8ca0

Please sign in to comment.