没有其他库的圆形图像

时间:2017-06-10 17:12:09

标签: android

我需要从外部存储器获取图像并将图像放入圆形 并将该图像保存为sheredprefernce

我尝试过,但我无法做到

我需要它在没有任何外部库的情况下完成

1 个答案:

答案 0 :(得分:0)

对于没有任何库的圆形图像视图,您可以使用以下类。

package com.sample;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * Custom Circular Image view.
 */
public class RoundImageview extends ImageView {

    private int borderWidth = 0;
    private int viewWidth;
    private int viewHeight;
    private Bitmap image;
    private Paint paint;
    private Paint paintBorder;
    private Paint shaderPaint;
    private BitmapShader shader;
    private static int mBorderColor = android.R.color.white;
    private Context context;

    private boolean needToDrawOverlay = true;
    private AttributeSet attrs;

    public RoundImageview(final Context context) {
        super(context);
        this.context = context;
    }

    public RoundImageview(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        this.attrs = attrs;
        init();
    }

    public RoundImageview(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        this.attrs = attrs;
        init();
    }

    /**
     * To initialize the paint components.
     */
    private void init() {
        Resources res = context.getResources();
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView, 0, 0);
        int backgroundColor = a.getColor(R.styleable.RoundImageView_bg_color,
                res.getColor(android.R.color.darker_gray));
        a.recycle();

        paint = new Paint();
        paint.setAntiAlias(true);

        shaderPaint = new Paint();
        shaderPaint.setAntiAlias(true);
        shaderPaint.setColor(backgroundColor);
        shaderPaint.setStyle(Paint.Style.FILL);

        paintBorder = new Paint();
        paintBorder.setColor(mBorderColor);
        borderWidth = 2;
        paintBorder.setAntiAlias(true);

    }

    @Override
    public void onDraw(Canvas canvas) {
        // load the bitmap
        BitmapDrawable bitmapDrawable = (BitmapDrawable) this.getDrawable();
        if (bitmapDrawable != null) {
            image = bitmapDrawable.getBitmap();
        }

        if (needToDrawOverlay) {
            int circleCenter = viewWidth / 2;

            canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter
                    + borderWidth - 4.0f, shaderPaint);
        }

        // init shader
        if (image != null) {
            shader = createBitmapShader(image, canvas);

            paint.setShader(shader);

            // calculate the center point.
            int circleCenter = viewWidth / 2;

            // circleCenter is the x or y of the view's center
            // radius is the radius in pixels of the cirle to be drawn
            // paint contains the shader that will texture the shape
            canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter
                    + borderWidth - 4.0f, paintBorder);
            canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth,
                    circleCenter - 4.0f, paint);
        }

    }

    /**
     * To create bitmap shader.
     * 
     * @param image
     * @param canvas
     * @return BitmapShader object
     */
    private BitmapShader createBitmapShader(Bitmap image, Canvas canvas) {
        return new BitmapShader(Bitmap.createScaledBitmap(image, this.getWidth(), this.getHeight(),
                false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = measureWidth(widthMeasureSpec);
        int height = measureHeight(heightMeasureSpec, widthMeasureSpec);

        viewWidth = width;
        viewHeight = height;

        setMeasuredDimension(width, height);
    }

    private int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text
            result = viewWidth;
        }

        return result;
    }

    private int measureHeight(int measureSpecHeight, int measureSpecWidth) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpecHeight);
        int specSize = MeasureSpec.getSize(measureSpecHeight);

        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text (beware: ascent is a negative number)
            result = viewHeight;
        }

        return (result);
    }

}

在res模块的values模块中创建一个attrs.xml

<!-- Round Image view -->
<declare-styleable name="RoundImageView">
    <attr name="bg_color" format="color" />
    <attr name="border_color" format="color" />
</declare-styleable>

以这种方式在布局中使用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:roundImageview="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.sample.RoundImageview
        android:id="@+id/imageButton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        android:src="@drawable/image"
        roundImageview:bg_color="#000000" />

</RelativeLayout>