gpt4 book ai didi

java - imageView 未正确适应屏幕

转载 作者:行者123 更新时间:2023-12-01 18:41:49 24 4
gpt4 key购买 nike

我是刚编写 Android 应用程序的新手,我的 ImageView 面临着显示完整图片的问题,即使屏幕上还有空间。看起来我的 View 有一些左右填充事件,如果情况并非如此。

行为:

这是我的原图: enter image description here

在这里您可以找到实际显示在我的屏幕上的图片: enter image description here

如您所见,我的图片没有完全显示。

代码

这是我的 ImageView 代码:

class DrawableImageView extends ImageView implements FlutterView.OnTouchListener
{
float downx = 0;
float downy = 0;
float upx = 0;
float upy = 0;
boolean draw = false;

Canvas canvas;
Paint paint;
Matrix matrix;

public DrawableImageView(Context context)
{
super(context);
setOnTouchListener(this);
}

public DrawableImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
setOnTouchListener(this);
}

public DrawableImageView(Context context, AttributeSet attrs,
int defStyleAttr)
{
super(context, attrs, defStyleAttr);
setOnTouchListener(this);
}



public void setNewImage(Bitmap alteredBitmap, Bitmap bmp)
{
canvas = new Canvas(alteredBitmap );
paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(5);
matrix = new Matrix();
canvas.drawBitmap(bmp, matrix, paint);

setImageBitmap(alteredBitmap);
}

public void setDraw(boolean newvalue) {
draw = newvalue;
this.postInvalidate();
}

@Override
public boolean onTouch(View v, MotionEvent event)
{
int action = event.getAction();

if(draw) {
switch (action)
{
case MotionEvent.ACTION_DOWN:
downx = getPointerCoords(event)[0];//event.getX();
downy = getPointerCoords(event)[1];//event.getY();
break;
case MotionEvent.ACTION_MOVE:
upx = getPointerCoords(event)[0];//event.getX();
upy = getPointerCoords(event)[1];//event.getY();
canvas.drawLine(downx, downy, upx, upy, paint);
invalidate();
downx = upx;
downy = upy;
break;
case MotionEvent.ACTION_UP:
upx = getPointerCoords(event)[0];//event.getX();
upy = getPointerCoords(event)[1];//event.getY();
canvas.drawLine(downx, downy, upx, upy, paint);
invalidate();
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
}
return true;
}

final float[] getPointerCoords(MotionEvent e)
{
final int index = e.getActionIndex();
final float[] coords = new float[] { e.getX(index), e.getY(index) };
Matrix matrix = new Matrix();
getImageMatrix().invert(matrix);
matrix.postTranslate(getScrollX(), getScrollY());
matrix.mapPoints(coords);
return coords;
}
}

这是我实际 View 的代码:

public class Markup implements PlatformView, MethodChannel.MethodCallHandler {
private MethodChannel methodChannel;
DrawableImageView choosenImageView;
Bitmap alteredBitmap;

private Bitmap bitmap;


Markup(final Context context, int id, Object args, BinaryMessenger messenger, final PluginRegistry.Registrar registrar) {
methodChannel = new MethodChannel(messenger, "plugins.smartapps.flutter_markup/markup_" + id);
methodChannel.setMethodCallHandler(this);

DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;

bitmap = BitmapFactory.decodeFile((String) args);
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
choosenImageView = new DrawableImageView(context);
choosenImageView.setMaxWidth(width);
choosenImageView.setMaxHeight(height);
choosenImageView.setAdjustViewBounds(true);
choosenImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);


//scaleBitmap(bitmap);
alteredBitmap = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getWidth(), bitmap.getConfig());
choosenImageView.setNewImage(alteredBitmap, bitmap);
}


@Override
public View getView() {
return choosenImageView;
}


@Override
public void dispose() {
}

}



我知道我的帖子很长,但有人知道为什么会发生这种情况吗?

谢谢!

最佳答案

试试这个:

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY" or android:scaleType="centerCrop" //add this property
/>

关于java - imageView 未正确适应屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59917954/

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