为什么不设置android支持v4库的CircleImageView公开?

时间:2015-07-03 09:07:09

标签: android imageview

我已经注意到android.support.v4.widget.CircleImageView很久了。每当我想要使用圆形的ImageView时,CircleImageView就会出现在我的脑海中。我试过多次使用它,但每次都失败了。由于android.support.v4.widget.CircleImageView的访问权限为 默认 ,这意味着只有CircleImageView的同一个包中的类,即android.support.v4.widget,能够访问它。

我现在无法理解轮次ImageView是常用的,为什么不将CircleImageView设置为公开,以便开发人员不必将ImageView覆盖为圆形ImageView?是不是Google Android团队强迫我们重新发明轮子

或者,我不知道这个CircleImageView吗?

任何提示将不胜感激。提前谢谢。

2 个答案:

答案 0 :(得分:8)

我尝试复制并粘贴android.support.v4.widget.CircleImageView的源代码,使其公开,如下所示:

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后面添加阴影,其结果不是RoundCornerImageViewRoundImageView。因此,如果我想要RoundImageView,我必须忘记这个类并通过覆盖ImageView来实现此效果。

最后,有文件评论,指出使用android.support.v4.widget.CircleImageView

  

为解决AnimationListeners问题而创建的私有类   在动画实际完成和支持之前被调用   旧平台上的阴影。

我希望没有人再问这样一个愚蠢的问题,让它在这里结束,^ _ ^,哈哈〜

答案 1 :(得分:3)

根据文档,这个类是用于解决的私有类,我们无法实例化它。可能是我怀疑的那个课程将被删除。

有一些方法可以为视图创建圆形背景。

file: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"
       />