Dor*_*ori 84 android imageview
我有一个ImageView
显示png的宽高比比设备更大(垂直方向 - 意味着它更长).我希望在保持宽高比,匹配父级宽度以及将图像视图固定到屏幕顶部的同时显示它.
使用CENTER_CROP
缩放类型时遇到的问题是,它(可理解)使缩放图像居中,而不是将顶边与图像视图的顶边对齐.
问题FIT_START
是图像将适合屏幕高度而不是填充宽度.
我已经通过使用自定义ImageView解决了这个问题,onDraw(Canvas)
并使用画布手动覆盖和处理此问题; 这种方法的问题是1)我担心可能有一个更简单的解决方案,2)super(AttributeSet)
当在堆有3 mb空闲时尝试设置src img为330kb时,我在调用构造函数时遇到VM mem异常(堆大小为6 mb)并且无法解决原因.
任何想法/建议/解决方案都非常欢迎:)
谢谢
ps我认为解决方案可能是使用矩阵比例类型并自己做,但这似乎与我当前的解决方案相同或更多的工作!
Dor*_*ori 83
好的,我有一个有效的解决方案.来自Darko的提示让我再次看了ImageView类(感谢)并使用Matrix应用了转换(正如我最初怀疑但在我第一次尝试时没有成功!).在我的自定义的ImageView类我打电话setScaleType(ScaleType.MATRIX)
后,super()
在构造函数中,并具有以下方法.
@Override
protected boolean setFrame(int l, int t, int r, int b)
{
Matrix matrix = getImageMatrix();
float scaleFactor = getWidth()/(float)getDrawable().getIntrinsicWidth();
matrix.setScale(scaleFactor, scaleFactor, 0, 0);
setImageMatrix(matrix);
return super.setFrame(l, t, r, b);
}
Run Code Online (Sandbox Code Playgroud)
我在setFrame()
方法中放置了int,就像在ImageView中一样,调用configureBounds()
是在这个方法中,这是所有缩放和矩阵内容发生的地方,所以对我来说似乎合乎逻辑(如果你不同意的话)
下面是AOSP的super.setFrame()方法
@Override
protected boolean setFrame(int l, int t, int r, int b) {
boolean changed = super.setFrame(l, t, r, b);
mHaveFrame = true;
configureBounds();
return changed;
}
Run Code Online (Sandbox Code Playgroud)
在这里找到完整的类src
Maj*_*ast 41
这是我在底部居中的代码.顺便说一句.在Dori的代码中有一个小错误:由于在super.frame()
最后调用,该getWidth()
方法可能返回错误的值.如果你想把它放在顶部,只需删除postTranslate行,你就完成了.好的是,使用此代码,您可以将其移动到任何您想要的位置.(右,中心=>没问题;)
public class CenterBottomImageView extends ImageView {
public CenterBottomImageView(Context context) {
super(context);
setup();
}
public CenterBottomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
setup();
}
public CenterBottomImageView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
setup();
}
private void setup() {
setScaleType(ScaleType.MATRIX);
}
@Override
protected boolean setFrame(int frameLeft, int frameTop, int frameRight, int frameBottom) {
if (getDrawable() == null) {
return super.setFrame(frameLeft, frameTop, frameRight, frameBottom);
}
float frameWidth = frameRight - frameLeft;
float frameHeight = frameBottom - frameTop;
float originalImageWidth = (float)getDrawable().getIntrinsicWidth();
float originalImageHeight = (float)getDrawable().getIntrinsicHeight();
float usedScaleFactor = 1;
if((frameWidth > originalImageWidth) || (frameHeight > originalImageHeight)) {
// If frame is bigger than image
// => Crop it, keep aspect ratio and position it at the bottom and center horizontally
float fitHorizontallyScaleFactor = frameWidth/originalImageWidth;
float fitVerticallyScaleFactor = frameHeight/originalImageHeight;
usedScaleFactor = Math.max(fitHorizontallyScaleFactor, fitVerticallyScaleFactor);
}
float newImageWidth = originalImageWidth * usedScaleFactor;
float newImageHeight = originalImageHeight * usedScaleFactor;
Matrix matrix = getImageMatrix();
matrix.setScale(usedScaleFactor, usedScaleFactor, 0, 0); // Replaces the old matrix completly
//comment matrix.postTranslate if you want crop from TOP
matrix.postTranslate((frameWidth - newImageWidth) /2, frameHeight - newImageHeight);
setImageMatrix(matrix);
return super.setFrame(frameLeft, frameTop, frameRight, frameBottom);
}
}
Run Code Online (Sandbox Code Playgroud)
Hen*_*nry 34
您无需编写自定义图像视图即可获得该TOP_CROP
功能.你只需要修改matrix
它ImageView
.
设置scaleType
到matrix
为ImageView
:
<ImageView
android:id="@+id/imageView"
android:contentDescription="Image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/image"
android:scaleType="matrix"/>
Run Code Online (Sandbox Code Playgroud)为以下内容设置自定义矩阵ImageView
:
final ImageView imageView = (ImageView) findViewById(R.id.imageView);
final Matrix matrix = imageView.getImageMatrix();
final float imageWidth = imageView.getDrawable().getIntrinsicWidth();
final int screenWidth = getResources().getDisplayMetrics().widthPixels;
final float scaleRatio = screenWidth / imageWidth;
matrix.postScale(scaleRatio, scaleRatio);
imageView.setImageMatrix(matrix);
Run Code Online (Sandbox Code Playgroud)这样做会为您提供TOP_CROP
功能.
Jac*_*cki 25
此示例适用于在创建对象+某些优化后加载的图像.我在代码中添加了一些注释来解释发生了什么.
记得打电话:
imageView.setScaleType(ImageView.ScaleType.MATRIX);
Run Code Online (Sandbox Code Playgroud)
要么
android:scaleType="matrix"
Run Code Online (Sandbox Code Playgroud)
Java来源:
import com.appunite.imageview.OverlayImageView;
public class TopAlignedImageView extends ImageView {
private Matrix mMatrix;
private boolean mHasFrame;
@SuppressWarnings("UnusedDeclaration")
public TopAlignedImageView(Context context) {
this(context, null, 0);
}
@SuppressWarnings("UnusedDeclaration")
public TopAlignedImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
@SuppressWarnings("UnusedDeclaration")
public TopAlignedImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mHasFrame = false;
mMatrix = new Matrix();
// we have to use own matrix because:
// ImageView.setImageMatrix(Matrix matrix) will not call
// configureBounds(); invalidate(); because we will operate on ImageView object
}
@Override
protected boolean setFrame(int l, int t, int r, int b)
{
boolean changed = super.setFrame(l, t, r, b);
if (changed) {
mHasFrame = true;
// we do not want to call this method if nothing changed
setupScaleMatrix(r-l, b-t);
}
return changed;
}
private void setupScaleMatrix(int width, int height) {
if (!mHasFrame) {
// we have to ensure that we already have frame
// called and have width and height
return;
}
final Drawable drawable = getDrawable();
if (drawable == null) {
// we have to check if drawable is null because
// when not initialized at startup drawable we can
// rise NullPointerException
return;
}
Matrix matrix = mMatrix;
final int intrinsicWidth = drawable.getIntrinsicWidth();
final int intrinsicHeight = drawable.getIntrinsicHeight();
float factorWidth = width/(float) intrinsicWidth;
float factorHeight = height/(float) intrinsicHeight;
float factor = Math.max(factorHeight, factorWidth);
// there magic happen and can be adjusted to current
// needs
matrix.setTranslate(-intrinsicWidth/2.0f, 0);
matrix.postScale(factor, factor, 0, 0);
matrix.postTranslate(width/2.0f, 0);
setImageMatrix(matrix);
}
@Override
public void setImageDrawable(Drawable drawable) {
super.setImageDrawable(drawable);
// We have to recalculate image after chaning image
setupScaleMatrix(getWidth(), getHeight());
}
@Override
public void setImageResource(int resId) {
super.setImageResource(resId);
// We have to recalculate image after chaning image
setupScaleMatrix(getWidth(), getHeight());
}
@Override
public void setImageURI(Uri uri) {
super.setImageURI(uri);
// We have to recalculate image after chaning image
setupScaleMatrix(getWidth(), getHeight());
}
// We do not have to overide setImageBitmap because it calls
// setImageDrawable method
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*att 13
基于Dori,我使用的解决方案是根据图像的宽度或高度缩放图像,以便始终填充周围的容器.这允许使用图像的左上角而不是中心作为原点来缩放图像以填充整个可用空间(CENTER_CROP):
@Override
protected boolean setFrame(int l, int t, int r, int b)
{
Matrix matrix = getImageMatrix();
float scaleFactor, scaleFactorWidth, scaleFactorHeight;
scaleFactorWidth = (float)width/(float)getDrawable().getIntrinsicWidth();
scaleFactorHeight = (float)height/(float)getDrawable().getIntrinsicHeight();
if(scaleFactorHeight > scaleFactorWidth) {
scaleFactor = scaleFactorHeight;
} else {
scaleFactor = scaleFactorWidth;
}
matrix.setScale(scaleFactor, scaleFactor, 0, 0);
setImageMatrix(matrix);
return super.setFrame(l, t, r, b);
}
Run Code Online (Sandbox Code Playgroud)
我希望这会有所帮助 - 在我的项目中就像一种享受.
这些解决方案都不适用于我,因为我想要一个从水平或垂直方向支持任意裁剪的类,我希望它允许我动态地更改裁剪.我还需要毕加索的兼容性,毕加索懒洋洋地设置了图像绘画.
我的实现直接来自AOSP中的ImageView.java.要使用它,请在XML中声明:
<com.yourapp.PercentageCropImageView
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="matrix"/>
Run Code Online (Sandbox Code Playgroud)
从来源来看,如果您想拥有顶级作物,请致电:
imageView.setCropYCenterOffsetPct(0f);
Run Code Online (Sandbox Code Playgroud)
如果您希望有底部作物,请致电:
imageView.setCropYCenterOffsetPct(1.0f);
Run Code Online (Sandbox Code Playgroud)
如果您希望减少1/3,请致电:
imageView.setCropYCenterOffsetPct(0.33f);
Run Code Online (Sandbox Code Playgroud)
此外,如果您选择使用另一种裁剪方法,例如fit_center,您可以这样做,并且不会触发任何自定义逻辑.(其他实现只允许您使用他们的裁剪方法).
最后,我添加了一个方法redraw(),因此如果您选择在代码中动态更改裁剪方法/ scaleType,则可以强制视图重绘.例如:
fullsizeImageView.setScaleType(ScaleType.FIT_CENTER);
fullsizeImageView.redraw();
Run Code Online (Sandbox Code Playgroud)
要返回自定义顶级中心第三季作物,请致电:
fullsizeImageView.setScaleType(ScaleType.MATRIX);
fullsizeImageView.redraw();
Run Code Online (Sandbox Code Playgroud)
这是班级:
/*
* Adapted from ImageView code at:
* http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.4_r1/android/widget/ImageView.java
*/
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class PercentageCropImageView extends ImageView{
private Float mCropYCenterOffsetPct;
private Float mCropXCenterOffsetPct;
public PercentageCropImageView(Context context) {
super(context);
}
public PercentageCropImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PercentageCropImageView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public float getCropYCenterOffsetPct() {
return mCropYCenterOffsetPct;
}
public void setCropYCenterOffsetPct(float cropYCenterOffsetPct) {
if (cropYCenterOffsetPct > 1.0) {
throw new IllegalArgumentException("Value too large: Must be <= 1.0");
}
this.mCropYCenterOffsetPct = cropYCenterOffsetPct;
}
public float getCropXCenterOffsetPct() {
return mCropXCenterOffsetPct;
}
public void setCropXCenterOffsetPct(float cropXCenterOffsetPct) {
if (cropXCenterOffsetPct > 1.0) {
throw new IllegalArgumentException("Value too large: Must be <= 1.0");
}
this.mCropXCenterOffsetPct = cropXCenterOffsetPct;
}
private void myConfigureBounds() {
if (this.getScaleType() == ScaleType.MATRIX) {
/*
* Taken from Android's ImageView.java implementation:
*
* Excerpt from their source:
} else if (ScaleType.CENTER_CROP == mScaleType) {
mDrawMatrix = mMatrix;
float scale;
float dx = 0, dy = 0;
if (dwidth * vheight > vwidth * dheight) {
scale = (float) vheight / (float) dheight;
dx = (vwidth - dwidth * scale) * 0.5f;
} else {
scale = (float) vwidth / (float) dwidth;
dy = (vheight - dheight * scale) * 0.5f;
}
mDrawMatrix.setScale(scale, scale);
mDrawMatrix.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
}
*/
Drawable d = this.getDrawable();
if (d != null) {
int dwidth = d.getIntrinsicWidth();
int dheight = d.getIntrinsicHeight();
Matrix m = new Matrix();
int vwidth = getWidth() - this.getPaddingLeft() - this.getPaddingRight();
int vheight = getHeight() - this.getPaddingTop() - this.getPaddingBottom();
float scale;
float dx = 0, dy = 0;
if (dwidth * vheight > vwidth * dheight) {
float cropXCenterOffsetPct = mCropXCenterOffsetPct != null ?
mCropXCenterOffsetPct.floatValue() : 0.5f;
scale = (float) vheight / (float) dheight;
dx = (vwidth - dwidth * scale) * cropXCenterOffsetPct;
} else {
float cropYCenterOffsetPct = mCropYCenterOffsetPct != null ?
mCropYCenterOffsetPct.floatValue() : 0f;
scale = (float) vwidth / (float) dwidth;
dy = (vheight - dheight * scale) * cropYCenterOffsetPct;
}
m.setScale(scale, scale);
m.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
this.setImageMatrix(m);
}
}
}
// These 3 methods call configureBounds in ImageView.java class, which
// adjusts the matrix in a call to center_crop (android's built-in
// scaling and centering crop method). We also want to trigger
// in the same place, but using our own matrix, which is then set
// directly at line 588 of ImageView.java and then copied over
// as the draw matrix at line 942 of ImageVeiw.java
@Override
protected boolean setFrame(int l, int t, int r, int b) {
boolean changed = super.setFrame(l, t, r, b);
this.myConfigureBounds();
return changed;
}
@Override
public void setImageDrawable(Drawable d) {
super.setImageDrawable(d);
this.myConfigureBounds();
}
@Override
public void setImageResource(int resId) {
super.setImageResource(resId);
this.myConfigureBounds();
}
public void redraw() {
Drawable d = this.getDrawable();
if (d != null) {
// Force toggle to recalculate our bounds
this.setImageDrawable(null);
this.setImageDrawable(d);
}
}
}
Run Code Online (Sandbox Code Playgroud)
也许进入android上的图像视图的源代码,看看它如何绘制中心裁剪等..并可能将一些代码复制到您的方法中.我真的不知道比这更好的解决方案.我有手动调整大小和裁剪位图(搜索位图转换)的经验,这减少了它的实际大小,但它仍然在过程中产生了一些开销.
小智 5
public class ImageViewTopCrop extends ImageView {
public ImageViewTopCrop(Context context) {
super(context);
setScaleType(ScaleType.MATRIX);
}
public ImageViewTopCrop(Context context, AttributeSet attrs) {
super(context, attrs);
setScaleType(ScaleType.MATRIX);
}
public ImageViewTopCrop(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setScaleType(ScaleType.MATRIX);
}
@Override
protected boolean setFrame(int l, int t, int r, int b) {
computMatrix();
return super.setFrame(l, t, r, b);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
computMatrix();
}
private void computMatrix() {
Matrix matrix = getImageMatrix();
float scaleFactor = getWidth() / (float) getDrawable().getIntrinsicWidth();
matrix.setScale(scaleFactor, scaleFactor, 0, 0);
setImageMatrix(matrix);
}
Run Code Online (Sandbox Code Playgroud)
}
归档时间: |
|
查看次数: |
38544 次 |
最近记录: |