为什么不设置 android 的 CircleImageView 支持 v4 库 public?
Why don't set CircleImageView of android support v4 library public?
我注意到 android.support.v4.widget.CircleImageView
很久了。每次我想用一个圆形的ImageView
,我的脑海里就会出现CircleImageView
。我曾多次尝试使用它,但每次都失败了。因为android.support.v4.widget.CircleImageView
的访问权限是default也就是说只有类和CircleImageView
同一个包,即 android.support.v4.widget
,能够访问它。
我现在无法理解轮 ImageView
是通用的,为什么不将 CircleImageView
设置为 public 这样开发人员就不必覆盖 ImageView
变成一个圆形的ImageView?不就是GoogleAndroid团队逼我们重新发明轮子吗?
或者,我不是很了解这个CircleImageView
吗?
如有任何提示,我们将不胜感激。提前致谢。
根据文档,此 class 是私有的 class 用于变通,我们无法实例化它。我怀疑 class 可能会更快被删除。
有多种方法可以为视图创建圆形背景。
文件:drawable/contact_badge_round.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<size android:width="32dp" android:height="32dp"/>
<gradient android:centerColor="#123456" <!--Put your custom color for bg -->
android:startColor="#123456"
android:endColor="#123456"
/>
</shape>
然后在您的布局中创建一个 Button 并将背景设置为 contact_badge_round
<ImageView
android:id="@+id/roundContact"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="@drawalbe/your_image"
android:gravity="center"
android:background="@drawable/background_new_entity_symbol"
/>
我试图复制并粘贴 android.support.v4.widget.CircleImageView
的源代码,使其成为 public 就像这样:
package me.danielpan.youtubelike.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.support.v4.view.ViewCompat;
import android.view.animation.Animation;
import android.widget.ImageView;
/**
* Private class created to work around issues with AnimationListeners being
* called before the animation is actually complete and support shadows on older
* platforms.
*
* @hide
*/
public class CircleImageView extends ImageView {
private static final int KEY_SHADOW_COLOR = 0x1E000000;
private static final int FILL_SHADOW_COLOR = 0x3D000000;
// PX
private static final float X_OFFSET = 0f;
private static final float Y_OFFSET = 1.75f;
private static final float SHADOW_RADIUS = 3.5f;
private static final int SHADOW_ELEVATION = 4;
private Animation.AnimationListener mListener;
private int mShadowRadius;
public CircleImageView(Context context, int color, final float radius) {
super(context);
final float density = getContext().getResources().getDisplayMetrics().density;
final int diameter = (int) (radius * density * 2);
final int shadowYOffset = (int) (density * Y_OFFSET);
final int shadowXOffset = (int) (density * X_OFFSET);
mShadowRadius = (int) (density * SHADOW_RADIUS);
ShapeDrawable circle;
if (elevationSupported()) {
circle = new ShapeDrawable(new OvalShape());
ViewCompat.setElevation(this, SHADOW_ELEVATION * density);
} else {
OvalShape oval = new OvalShadow(mShadowRadius, diameter);
circle = new ShapeDrawable(oval);
ViewCompat.setLayerType(this, ViewCompat.LAYER_TYPE_SOFTWARE, circle.getPaint());
circle.getPaint().setShadowLayer(mShadowRadius, shadowXOffset, shadowYOffset,
KEY_SHADOW_COLOR);
final int padding = mShadowRadius;
// set padding so the inner image sits correctly within the shadow.
setPadding(padding, padding, padding, padding);
}
circle.getPaint().setColor(color);
setBackgroundDrawable(circle);
}
private boolean elevationSupported() {
return android.os.Build.VERSION.SDK_INT >= 21;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (!elevationSupported()) {
setMeasuredDimension(getMeasuredWidth() + mShadowRadius*2, getMeasuredHeight()
+ mShadowRadius*2);
}
}
public void setAnimationListener(Animation.AnimationListener listener) {
mListener = listener;
}
@Override
public void onAnimationStart() {
super.onAnimationStart();
if (mListener != null) {
mListener.onAnimationStart(getAnimation());
}
}
@Override
public void onAnimationEnd() {
super.onAnimationEnd();
if (mListener != null) {
mListener.onAnimationEnd(getAnimation());
}
}
/**
* Update the background color of the circle image view.
*
* @param colorRes Id of a color resource.
*/
public void setBackgroundColorRes(int colorRes) {
setBackgroundColor(getContext().getResources().getColor(colorRes));
}
@Override
public void setBackgroundColor(int color) {
if (getBackground() instanceof ShapeDrawable) {
((ShapeDrawable) getBackground()).getPaint().setColor(color);
}
}
private class OvalShadow extends OvalShape {
private RadialGradient mRadialGradient;
private Paint mShadowPaint;
private int mCircleDiameter;
public OvalShadow(int shadowRadius, int circleDiameter) {
super();
mShadowPaint = new Paint();
mShadowRadius = shadowRadius;
mCircleDiameter = circleDiameter;
mRadialGradient = new RadialGradient(mCircleDiameter / 2, mCircleDiameter / 2,
mShadowRadius, new int[] {
FILL_SHADOW_COLOR, Color.TRANSPARENT
}, null, Shader.TileMode.CLAMP);
mShadowPaint.setShader(mRadialGradient);
}
@Override
public void draw(Canvas canvas, Paint paint) {
final int viewWidth = CircleImageView.this.getWidth();
final int viewHeight = CircleImageView.this.getHeight();
canvas.drawCircle(viewWidth / 2, viewHeight / 2, (mCircleDiameter / 2 + mShadowRadius),
mShadowPaint);
canvas.drawCircle(viewWidth / 2, viewHeight / 2, (mCircleDiameter / 2), paint);
}
}
}
看起来不错吧?它没有自定义属性,似乎可以用作普通 ImageView
.
但是如果你尝试过,你会发现抛出了NoSuchMethodException
。此异常意味着必要的构造函数未被覆盖。这样你甚至不能将它实例化为一个普通的 View
.
看了这些源码才知道,CircleImageView
只是在ImageView
后面加了阴影,结果不是RoundCornerImageView
,也不是RoundImageView
。所以如果我想要一个 RoundImageView
,我必须忘记这个 class 并通过覆盖 ImageView
.
来实现这个效果
最后是文件注释,指出了android.support.v4.widget.CircleImageView
的用法:
Private class created to work around issues with AnimationListeners
being called before the animation is actually complete and support
shadows on older platforms.
也希望大家不要再问这种傻问题了,就到这里吧,^_^,哈哈~
我注意到 android.support.v4.widget.CircleImageView
很久了。每次我想用一个圆形的ImageView
,我的脑海里就会出现CircleImageView
。我曾多次尝试使用它,但每次都失败了。因为android.support.v4.widget.CircleImageView
的访问权限是default也就是说只有类和CircleImageView
同一个包,即 android.support.v4.widget
,能够访问它。
我现在无法理解轮 ImageView
是通用的,为什么不将 CircleImageView
设置为 public 这样开发人员就不必覆盖 ImageView
变成一个圆形的ImageView?不就是GoogleAndroid团队逼我们重新发明轮子吗?
或者,我不是很了解这个CircleImageView
吗?
如有任何提示,我们将不胜感激。提前致谢。
根据文档,此 class 是私有的 class 用于变通,我们无法实例化它。我怀疑 class 可能会更快被删除。
有多种方法可以为视图创建圆形背景。
文件:drawable/contact_badge_round.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<size android:width="32dp" android:height="32dp"/>
<gradient android:centerColor="#123456" <!--Put your custom color for bg -->
android:startColor="#123456"
android:endColor="#123456"
/>
</shape>
然后在您的布局中创建一个 Button 并将背景设置为 contact_badge_round
<ImageView
android:id="@+id/roundContact"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="@drawalbe/your_image"
android:gravity="center"
android:background="@drawable/background_new_entity_symbol"
/>
我试图复制并粘贴 android.support.v4.widget.CircleImageView
的源代码,使其成为 public 就像这样:
package me.danielpan.youtubelike.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.support.v4.view.ViewCompat;
import android.view.animation.Animation;
import android.widget.ImageView;
/**
* Private class created to work around issues with AnimationListeners being
* called before the animation is actually complete and support shadows on older
* platforms.
*
* @hide
*/
public class CircleImageView extends ImageView {
private static final int KEY_SHADOW_COLOR = 0x1E000000;
private static final int FILL_SHADOW_COLOR = 0x3D000000;
// PX
private static final float X_OFFSET = 0f;
private static final float Y_OFFSET = 1.75f;
private static final float SHADOW_RADIUS = 3.5f;
private static final int SHADOW_ELEVATION = 4;
private Animation.AnimationListener mListener;
private int mShadowRadius;
public CircleImageView(Context context, int color, final float radius) {
super(context);
final float density = getContext().getResources().getDisplayMetrics().density;
final int diameter = (int) (radius * density * 2);
final int shadowYOffset = (int) (density * Y_OFFSET);
final int shadowXOffset = (int) (density * X_OFFSET);
mShadowRadius = (int) (density * SHADOW_RADIUS);
ShapeDrawable circle;
if (elevationSupported()) {
circle = new ShapeDrawable(new OvalShape());
ViewCompat.setElevation(this, SHADOW_ELEVATION * density);
} else {
OvalShape oval = new OvalShadow(mShadowRadius, diameter);
circle = new ShapeDrawable(oval);
ViewCompat.setLayerType(this, ViewCompat.LAYER_TYPE_SOFTWARE, circle.getPaint());
circle.getPaint().setShadowLayer(mShadowRadius, shadowXOffset, shadowYOffset,
KEY_SHADOW_COLOR);
final int padding = mShadowRadius;
// set padding so the inner image sits correctly within the shadow.
setPadding(padding, padding, padding, padding);
}
circle.getPaint().setColor(color);
setBackgroundDrawable(circle);
}
private boolean elevationSupported() {
return android.os.Build.VERSION.SDK_INT >= 21;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (!elevationSupported()) {
setMeasuredDimension(getMeasuredWidth() + mShadowRadius*2, getMeasuredHeight()
+ mShadowRadius*2);
}
}
public void setAnimationListener(Animation.AnimationListener listener) {
mListener = listener;
}
@Override
public void onAnimationStart() {
super.onAnimationStart();
if (mListener != null) {
mListener.onAnimationStart(getAnimation());
}
}
@Override
public void onAnimationEnd() {
super.onAnimationEnd();
if (mListener != null) {
mListener.onAnimationEnd(getAnimation());
}
}
/**
* Update the background color of the circle image view.
*
* @param colorRes Id of a color resource.
*/
public void setBackgroundColorRes(int colorRes) {
setBackgroundColor(getContext().getResources().getColor(colorRes));
}
@Override
public void setBackgroundColor(int color) {
if (getBackground() instanceof ShapeDrawable) {
((ShapeDrawable) getBackground()).getPaint().setColor(color);
}
}
private class OvalShadow extends OvalShape {
private RadialGradient mRadialGradient;
private Paint mShadowPaint;
private int mCircleDiameter;
public OvalShadow(int shadowRadius, int circleDiameter) {
super();
mShadowPaint = new Paint();
mShadowRadius = shadowRadius;
mCircleDiameter = circleDiameter;
mRadialGradient = new RadialGradient(mCircleDiameter / 2, mCircleDiameter / 2,
mShadowRadius, new int[] {
FILL_SHADOW_COLOR, Color.TRANSPARENT
}, null, Shader.TileMode.CLAMP);
mShadowPaint.setShader(mRadialGradient);
}
@Override
public void draw(Canvas canvas, Paint paint) {
final int viewWidth = CircleImageView.this.getWidth();
final int viewHeight = CircleImageView.this.getHeight();
canvas.drawCircle(viewWidth / 2, viewHeight / 2, (mCircleDiameter / 2 + mShadowRadius),
mShadowPaint);
canvas.drawCircle(viewWidth / 2, viewHeight / 2, (mCircleDiameter / 2), paint);
}
}
}
看起来不错吧?它没有自定义属性,似乎可以用作普通 ImageView
.
但是如果你尝试过,你会发现抛出了NoSuchMethodException
。此异常意味着必要的构造函数未被覆盖。这样你甚至不能将它实例化为一个普通的 View
.
看了这些源码才知道,CircleImageView
只是在ImageView
后面加了阴影,结果不是RoundCornerImageView
,也不是RoundImageView
。所以如果我想要一个 RoundImageView
,我必须忘记这个 class 并通过覆盖 ImageView
.
最后是文件注释,指出了android.support.v4.widget.CircleImageView
的用法:
Private class created to work around issues with AnimationListeners being called before the animation is actually complete and support shadows on older platforms.
也希望大家不要再问这种傻问题了,就到这里吧,^_^,哈哈~