gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 15:04:52 25 4
gpt4 key购买 nike

我在使用从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com