Java android上的图像大小调整不正确

Java android上的图像大小调整不正确,java,android,xml,image,resize,Java,Android,Xml,Image,Resize,我从GitHub得到的一个库有问题。 图书馆通向一个圆形的图像。圆角部分工作得很好,但图像大小调整不如其他部分好。它因图像不同而不同,我希望能够将任何图像的大小调整到android上我的视图大小。例如,如果我用android调用这个:layout_width=100dp,我希望图像的大小能调整那么大 非常感谢您抽出时间 这是图书馆: package com.roundimage.support; import android.annotation.SuppressLint; import an

我从GitHub得到的一个库有问题。 图书馆通向一个圆形的图像。圆角部分工作得很好,但图像大小调整不如其他部分好。它因图像不同而不同,我希望能够将任何图像的大小调整到android上我的视图大小。例如,如果我用android调用这个:layout_width=100dp,我希望图像的大小能调整那么大

非常感谢您抽出时间

这是图书馆:

package com.roundimage.support;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.provider.MediaStore;
import android.util.AttributeSet;
import android.widget.ImageView;

public class CircularImageView extends ImageView {

    private int borderWidth = 3;
    private int viewWidth;
    private int viewHeight;
    private Bitmap image;
    private Paint paint;
    private Paint paintBorder;
    private BitmapShader shader;

    public CircularImageView(Context context) {
        super(context);
        setup();
    }

    public CircularImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setup();
    }

    public CircularImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setup();
    }

    private void setup()
    {
        // init paint
        paint = new Paint();
        paint.setAntiAlias(true);

        paintBorder = new Paint();
        setBorderColor(Color.WHITE);
        paintBorder.setAntiAlias(true);     
    }

    public void setBorderWidth(int borderWidth)
    {
        this.borderWidth = borderWidth;
        this.invalidate();
    }

    public void setBorderColor(int borderColor)
    {       
        if(paintBorder != null)
            paintBorder.setColor(borderColor);

        this.invalidate();
    }

    private void loadBitmap()
    {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) this.getDrawable();

        if(bitmapDrawable != null)
            image = bitmapDrawable.getBitmap();
    }

    @SuppressLint("DrawAllocation")
    @Override
    public void onDraw(Canvas canvas)
    {
        //load the bitmap
        loadBitmap();

        // init shader
        if(image !=null)
        {           
            shader = new BitmapShader(Bitmap.createScaledBitmap(image, canvas.getWidth(), canvas.getHeight(), false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            paint.setShader(shader);
            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, paintBorder);
            canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter, paint);
        }       
    }

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

        viewWidth = width - (borderWidth *2);
        viewHeight = height - (borderWidth*2);

        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;
    }
}
这是我在XML上的称呼:

<com.roundimage.support.CircularImageView
    android:id="@+id/item_pic"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:src="@drawable/example_id" />
如何保持这种状态:

以及图像的真实情况:


对于不同大小的不同图像,对于不同分辨率的不同设备,图像以不同的方式显示。

尝试在xml中使用scaleType来显示

android:ScaleType=fitXY

或以编程方式

imageview.setScaleType ScaleType.FITXY

如果不起作用,请尝试另一个scaletype


编辑:请参阅本文。

好的,您的代码应该可以很好地工作,但它可以将图像放入图像视图,而无需调整大小以适应查看器,因为您得到了这些结果

尝试在布局中添加此选项:

android:scaleType="fitCenter"
阅读本教程中有关此内容的模式:

链接正确指出:

您必须将图像位图调整为所需大小:

` 私有void加载位图 { BitmapDrawable BitmapDrawable=BitmapDrawable this.getDrawable

    if(bitmapDrawable != null) {
        int size = getPixelsFromDp(70);
        Bitmap imageRealSize = bitmapDrawable.getBitmap();
        image = Bitmap.createScaledBitmap(imageRealSize, size, size, true);
    }
}
` 其中“getPixelsFromDp”是一个函数,用于获取添加到xml上的dp(以像素为单位),您可以找到页面负载以获取其实现