【问题标题】:How can i use RoundedAvatarDrawable for creating round image?如何使用 RoundedAvatarDrawable 创建圆形图像?
【发布时间】:2014-12-25 04:10:21
【问题描述】:

我正在使用类 RoundedAvatarDrawable.java 从示例位图中绘制圆形图像。 但对我来说它不起作用,或者我没有以正确的方式使用它。

https://github.com/chrisbanes/philm/blob/master/app/src/main/java/app/philm/in/drawable/RoundedAvatarDrawable.java

这是我的活动布局:

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/profil" />

            <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/profil2" />

    </LinearLayout>

这是我的活动代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // TODO Auto-generated method stub
    setContentView(R.layout.profil_activity_layout);
    getActionBar().setTitle("ROOT ACTIVITY");

    //Resources res = getResources();
    //Drawable drawable = res.getDrawable(R.drawable.profil);

    Bitmap bImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.profil);
    RoundedAvatarDrawable RondedAvatarImg = new RoundedAvatarDrawable(bImage);

    Bitmap bImageRwonded = RondedAvatarImg.getBitmap();
    ImageView mImg = (ImageView) findViewById(R.id.imageView2);
    mImg.setImageBitmap(bImageRwonded);

}

活动的输出是

我希望在第二个 ImageView 中获得一张圆形照片。 那么有人可以向我解释一下我错过了什么。

【问题讨论】:

    标签: android layout imageview drawable


    【解决方案1】:

    您好,根据您的需要使用以下代码。我们为您的 CirculerImageView 编写了课程。

    <com.widgets.CircularImageView
                android:id="@+id/profile_image"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:background="@drawable/shape_image"
                android:contentDescription="@string/text_app_name"
                android:scaleType="fitXY"
                android:src="@drawable/profile_img" />
    
    public class CircularImageView extends ImageView {
    
        /** The border width. */
        private int borderWidth;
    
        /** The canvas size. */
        private int canvasSize;
    
        /** The image. */
        private Bitmap image;
    
        /** The paint. */
        private Paint paint;
    
        /** The paint border. */
        private Paint paintBorder;
    
        /**
         * Instantiates a new circular image view.
         * 
         * @param context
         *            the context
         */
        public CircularImageView(final Context context) {
            this(context, null);
        }
    
        /**
         * Instantiates a new circular image view.
         * 
         * @param context
         *            the context
         * @param attrs
         *            the attrs
         */
        public CircularImageView(Context context, AttributeSet attrs) {
            this(context, attrs, R.attr.circularImageViewStyle);
        }
    
        /**
         * Instantiates a new circular image view.
         * 
         * @param context
         *            the context
         * @param attrs
         *            the attrs
         * @param defStyle
         *            the def style
         */
        public CircularImageView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
    
            paint = new Paint();
            paint.setAntiAlias(true);
            paintBorder = new Paint();
            paintBorder.setAntiAlias(true);
    
            TypedArray attributes = context.obtainStyledAttributes(attrs,
                    R.styleable.CircularImageView, defStyle, 0);
            if (attributes.getBoolean(R.styleable.CircularImageView_border, true)) {
                setBorderWidth(attributes.getDimensionPixelOffset(
                        R.styleable.CircularImageView_border_width, 0));
                setBorderColor(attributes.getColor(
                        R.styleable.CircularImageView_border_color, Color.WHITE));
            }
    
        }
    
        /**
         * Sets the border width.
         * 
         * @param borderWidth
         *            the new border width
         */
        public void setBorderWidth(int borderWidth) {
            this.borderWidth = borderWidth;
            this.requestLayout();
            this.invalidate();
        }
    
        /**
         * Sets the border color.
         * 
         * @param borderColor
         *            the new border color
         */
        public void setBorderColor(int borderColor) {
            if (paintBorder != null)
                paintBorder.setColor(borderColor);
            this.invalidate();
        }
    
        /**
         * Adds the shadow.
         */
        public void addShadow() {
            setLayerType(LAYER_TYPE_SOFTWARE, paintBorder);
            paintBorder.setShadowLayer(4.0f, 0.0f, 2.0f, Color.BLACK);
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see android.widget.ImageView#onDraw(android.graphics.Canvas)
         */
        @SuppressLint("DrawAllocation")
        @Override
        public void onDraw(Canvas canvas) {
            // load the bitmap
            image = drawableToBitmap(getDrawable());
            // init shader
            if (image != null) {
                canvasSize = canvas.getWidth();
                if (canvas.getHeight() < canvasSize)
                    canvasSize = canvas.getHeight();
                BitmapShader shader = new BitmapShader(Bitmap.createScaledBitmap(
                        image, canvasSize, canvasSize, false),
                        Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
                paint.setShader(shader);
    
                int circleCenter = (canvasSize - (borderWidth * 2)) / 2;
                canvas.drawCircle(circleCenter + borderWidth, circleCenter
                        + borderWidth, ((canvasSize - (borderWidth * 2)) / 2)
                        + borderWidth - 4.0f, paintBorder);
                canvas.drawCircle(circleCenter + borderWidth, circleCenter
                        + borderWidth,
                        ((canvasSize - (borderWidth * 2)) / 2) - 4.0f, paint);
            }
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see android.widget.ImageView#onMeasure(int, int)
         */
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int width = measureWidth(widthMeasureSpec);
            int height = measureHeight(heightMeasureSpec);
            setMeasuredDimension(width, height);
        }
    
        /**
         * Measure width.
         * 
         * @param measureSpec
         *            the measure spec
         * @return the int
         */
        private int measureWidth(int measureSpec) {
            int result = 0;
            int specMode = MeasureSpec.getMode(measureSpec);
            int specSize = MeasureSpec.getSize(measureSpec);
            if (specMode == MeasureSpec.EXACTLY) {
                result = specSize;
            } else if (specMode == MeasureSpec.AT_MOST) {
                result = specSize;
            } else {
                result = canvasSize;
            }
            return result;
        }
    
        /**
         * Measure height.
         * 
         * @param measureSpecHeight
         *            the measure spec height
         * @return the int
         */
        private int measureHeight(int measureSpecHeight) {
            int result = 0;
            int specMode = MeasureSpec.getMode(measureSpecHeight);
            int specSize = MeasureSpec.getSize(measureSpecHeight);
            if (specMode == MeasureSpec.EXACTLY) {
                result = specSize;
            } else if (specMode == MeasureSpec.AT_MOST) {
                result = specSize;
            } else {
                result = canvasSize;
            }
            return (result + 2);
        }
    
        /**
         * Drawable to bitmap.
         * 
         * @param drawable
         *            the drawable
         * @return the bitmap
         */
        public Bitmap drawableToBitmap(Drawable drawable) {
            if (drawable == null) {
                return null;
            } else if (drawable instanceof BitmapDrawable) {
                return ((BitmapDrawable) drawable).getBitmap();
            }
            Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
            return bitmap;
        }
    }
    

    attrs.xml(将其添加到 values 文件夹中)

    <resources>
    
        <declare-styleable name="CircularImageView">
            <attr name="border" format="boolean"></attr>
            <attr name="border_width" format="dimension"></attr>
            <attr name="border_color" format="color"></attr>
            <attr name="shadow" format="boolean"></attr>
        </declare-styleable>
        <declare-styleable name="Theme">
            <attr name="circularImageViewStyle" format="reference"></attr>
        </declare-styleable>
    
    </resources>
    

    【讨论】:

    • 我正在尝试使用您的课程来导入所有这些库:import android.content.Context;导入 android.content.res.TypedArray;导入android.graphics.Bitmap;导入android.graphics.BitmapShader;导入android.graphics.Canvas;导入android.graphics.Paint;导入 android.graphics.drawable.BitmapDrawable;导入 android.graphics.drawable.Drawable;导入android.util.AttributeSet;导入android.view.View.MeasureSpec;但是 var 缺少 R.styleable.CircularImageView R.styleable.CircularImageView_borde CircularImageView_border_width CircularImageView_border_color
    • 我们需要在 values 文件夹中添加 attrs.xml。我会更新上面的代码。检查一下
    • 对我来说很好。谢谢,非常优雅的解决方案。
    【解决方案2】:

    我设法找到了解决方案:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // TODO Auto-generated method stub
        setContentView(R.layout.profil_activity_layout);
        getActionBar().setTitle("ROOT ACTIVITY");
    
        //Resources res = getResources();
        //Drawable drawable = res.getDrawable(R.drawable.profil);
    
        BitmapDrawable bImage = (BitmapDrawable) getResources().getDrawable(R.drawable.profil);
        RoundedAvatarDrawable RondedAvatarImg = new RoundedAvatarDrawable(bImage.getBitmap());
    
        Bitmap bImageRwonded = RondedAvatarImg.getBitmap();
        ImageView mImg = (ImageView) findViewById(R.id.imageView2);
        mImg.setImageDrawable(new RoundedAvatarDrawable(bImage.getBitmap()));
    
    
    }
    

    这是输出:

    http://i.stack.imgur.com/fiqZf.png

    【讨论】:

      【解决方案3】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多