java - 图像在 Android 上无法正确调整大小

标签 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

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

最佳答案

好吧,您的代码应该可以正常工作,但它会将图像放入 ImageView 中,而无需调整大小以适合查看器,因为您得到了这些结果。

尝试在布局中添加此内容:

android:scaleType="fitCenter"

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

关于java - 图像在 Android 上无法正确调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22134885/

相关文章:

java - 将 HashMap 值添加到 TreeSet 时出错

android - 我可以覆盖可绘制形状的某些属性吗?

java - 将 GreenDao 与 Retrofit 集成

javascript - 我正在为 adobe muse 制作一个小部件,我需要知道如何将域名放在元属性标记的内容前面

javascript - 来自任何 XML 的特定 Javascript 对象

java - 何时在 java 多线程中使用 volatile 与同步?

java - 简单的 Drawable 装饰器不起作用

java - 使用 Gson Response.body() 进行改造不起作用

android - 一个游戏的多个 apk 方法

javascript - 从 infopath 格式的 XML 中获取节点数据