-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathCircularContactView.java
More file actions
157 lines (142 loc) · 5 KB
/
CircularContactView.java
File metadata and controls
157 lines (142 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package lb.listviewvariants.utils;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.media.ThumbnailUtils;
import android.os.Build;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import lb.listviewvariants.R;
public class CircularContactView extends ViewSwitcher
{
// private static final int DEFAULT_CONTENT_SIZE_IN_DP=20;
private ImageView mImageView;
private TextView mTextView;
private Bitmap mBitmap;
private CharSequence mText;
private int mBackgroundColor=0, mImageResId=0;
private int mContentSize;
public CircularContactView(final Context context)
{
this(context,null);
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public CircularContactView(final Context context,final AttributeSet attrs)
{
super(context,attrs);
addView(mImageView=new ImageView(context),new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,Gravity.CENTER));
addView(mTextView=new TextView(context),new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,Gravity.CENTER));
mTextView.setGravity(Gravity.CENTER);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH)
mTextView.setAllCaps(true);
mContentSize=getResources().getDimensionPixelSize(R.dimen.list_item__contact_imageview_size);
if(isInEditMode())
setTextAndBackgroundColor("",0xFFff0000);
}
public void setContentSize(final int contentSize)
{
this.mContentSize=contentSize;
}
@SuppressWarnings("deprecation")
private void drawContent(final int viewWidth,final int viewHeight)
{
ShapeDrawable roundedBackgroundDrawable=null;
if(mBackgroundColor!=0)
{
roundedBackgroundDrawable=new ShapeDrawable(new OvalShape());
roundedBackgroundDrawable.getPaint().setColor(mBackgroundColor);
roundedBackgroundDrawable.setIntrinsicHeight(viewHeight);
roundedBackgroundDrawable.setIntrinsicWidth(viewWidth);
roundedBackgroundDrawable.setBounds(new Rect(0,0,viewWidth,viewHeight));
}
if(mImageResId!=0)
{
mImageView.setBackgroundDrawable(roundedBackgroundDrawable);
mImageView.setImageResource(mImageResId);
mImageView.setScaleType(ScaleType.CENTER_INSIDE);
}
else if(mText!=null)
{
mTextView.setText(mText);
mTextView.setBackgroundDrawable(roundedBackgroundDrawable);
mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, viewHeight / 2);
}
else if(mBitmap!=null)
{
mImageView.setScaleType(ScaleType.FIT_CENTER);
mImageView.setBackgroundDrawable(roundedBackgroundDrawable);
if(mBitmap.getWidth()!=mBitmap.getHeight())
mBitmap=ThumbnailUtils.extractThumbnail(mBitmap,viewWidth,viewHeight);
final RoundedBitmapDrawable roundedBitmapDrawable=RoundedBitmapDrawableFactory.create(getResources(),
mBitmap);
roundedBitmapDrawable.setCornerRadius((mBitmap.getWidth()+mBitmap.getHeight())/4);
mImageView.setImageDrawable(roundedBitmapDrawable);
}
resetValuesState(false);
}
public void setTextAndBackgroundColor(final CharSequence text,final int backgroundColor)
{
resetValuesState(true);
while(getCurrentView()!=mTextView)
showNext();
this.mBackgroundColor=backgroundColor;
mText=text;
drawContent(mContentSize,mContentSize);
}
public void setImageResource(final int imageResId,final int backgroundColor)
{
resetValuesState(true);
while(getCurrentView()!=mImageView)
showNext();
mImageResId=imageResId;
this.mBackgroundColor=backgroundColor;
drawContent(mContentSize,mContentSize);
}
public void setImageBitmap(final Bitmap bitmap)
{
setImageBitmapAndBackgroundColor(bitmap,0);
}
public void setImageBitmapAndBackgroundColor(final Bitmap bitmap,final int backgroundColor)
{
resetValuesState(true);
while(getCurrentView()!=mImageView)
showNext();
this.mBackgroundColor=backgroundColor;
mBitmap=bitmap;
drawContent(mContentSize,mContentSize);
}
private void resetValuesState(final boolean alsoResetViews)
{
mBackgroundColor=mImageResId=0;
mBitmap=null;
mText=null;
if(alsoResetViews)
{
mTextView.setText(null);
mTextView.setBackgroundDrawable(null);
mImageView.setImageBitmap(null);
mImageView.setBackgroundDrawable(null);
}
}
public ImageView getImageView()
{
return mImageView;
}
public TextView getTextView()
{
return mTextView;
}
}