图像在android上没有正确调整大小

时间:2014-03-02 23:03:47

标签: java android xml image resize

我在使用GitHub上的库时遇到了问题。 图书馆导致圆形图像。圆角部分效果很好,但图像大小调整并不像其他部分那样大。它在图像与图像之间有所不同,我想让这项工作将任何图像的大小调整为我在Android上的View的大小。例如,如果我用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" />

这是如何保持的: enter image description here

图像的确如此: enter image description here

对于不同尺寸的不同图像,对于分辨率不同的不同设备,图像以不同的方式显示..

3 个答案:

答案 0 :(得分:0)

尝试在xml中使用scaleType作为fitXY

android:ScaleType =“fitXY”

或以编程方式

imageview.setScaleType(ScaleType.FITXY)

如果不起作用,请尝试其他比例尺。

编辑:请参阅此帖here

答案 1 :(得分:0)

好吧,你的代码应该运行良好,但它将图像放在图像视图中而不调整大小以适应查看器,因为你有这些结果。

尝试在布局中添加:

android:scaleType="fitCenter"

本教程中的阅读模式:http://www.peachpit.com/articles/article.aspx?p=1846580&seqNum=2

答案 2 :(得分:0)

@youssefhassan链接正确指出:

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

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

    if(bitmapDrawable != null) {
        int size = getPixelsFromDp(70);
        Bitmap imageRealSize = bitmapDrawable.getBitmap();
        image = Bitmap.createScaledBitmap(imageRealSize, size, size, true);
    }
}

` 其中&#39; getPixelsFromDp&#39;是一个函数来获取你在xml上添加的dp(以像素为单位)(你可以找到加载页面来实现它)