我想Aspect Fill
我的形象。我的图片大小是1242x168。我想要的是,如果我的屏幕很小,我的图像将被剪切并保持对齐,但似乎它仍然是aspect fit
。
这是我的代码
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int device_width = displaymetrics.widthPixels;
float density = getResources().getDisplayMetrics().density;
float dpWidth = displaymetrics.widthPixels / density;
if(dpWidth >= 768) {
int aspectRatio = (int) (0.135 * device_width);
gifImageView.getLayoutParams().height = aspectRatio;
gifImageView.getLayoutParams().width = device_width;
}
这是我的xml
<pl.droidsonroids.gif.GifImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/displayImage"
android:layout_gravity="left"
android:scaleType="fitStart"
android:background="@color/red"
android:adjustViewBounds="true"
/>
我尝试使用android:scaleType="centerCrop"
,但它在中心裁剪我想要削减右侧,这就是我想要保持对齐的原因。
答案 0 :(得分:0)
尝试根据屏幕宽度缩放图像:
public static float getScreenWidth(Activity activity) {
Display display = activity.getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float pxWidth = outMetrics.widthPixels;
return pxWidth;
}
float screenWidth=getScreenWidth(act)
float newHeight = screenWidth;
if (bitmap.getWidth() != 0 && bitmap.getHeight() != 0) {
newHeight = (screenWidth * bitmap.getHeight()) / bitmap.getWidth();
}
Bitmap scaledBitmap=Bitmap.createScaledBitmap(bitmap, (int) screenWidth, (int) newHeight, true);
android:adjustViewBounds=true.
android:scaleType="centerCrop"
链接:Resizing ImageView to fit to aspect ratio, https://gist.github.com/JakeWharton/2856179
答案 1 :(得分:0)
我试过这个
//Get width of device
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
float density = getResources().getDisplayMetrics().density;
float dpWidth = displaymetrics.widthPixels / density;
if(dpWidth > 768 ) {
int aspectRatio = (int) (0.135 * device_width);
gifImageView.getLayoutParams().height = aspectRatio;
gifImageView.getLayoutParams().width = device_width;
gifImageView.setIsScale(false);
}else{
int aspectRatio = (int) (0.219 * device_width);
gifImageView.getLayoutParams().height = aspectRatio;
gifImageView.getLayoutParams().width = device_width;
gifImageView.setScaleType(ProportionalImageView.ScaleType.MATRIX);
gifImageView.setIsScale(true);
}
创建一个扩展到ImageView
/ GifImageView
public class ProportionalImageView extends GifImageView {
Boolean isScale = false;
public ProportionalImageView(Context context) {
super(context);
if(isScale) {
setup();
}
}
public ProportionalImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
if(isScale) {
setup();
}
}
public ProportionalImageView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
if(isScale) {
setup();
}
}
private void setup() {
setScaleType(ScaleType.MATRIX);
}
@Override
protected boolean setFrame(int l, int t, int r, int b)
{
if(isScale) {
computeMatrix();
}
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);
if(isScale) {
computeMatrix();
}
}
private void computeMatrix() {
Matrix matrix = getImageMatrix();
float scaleFactor, scaleFactorWidth, scaleFactorHeight;
scaleFactorWidth = (float) getWidth() / (float) getDrawable().getIntrinsicWidth();
scaleFactorHeight = (float) getHeight() / (float) getDrawable().getIntrinsicHeight();
if (scaleFactorHeight > scaleFactorWidth) {
scaleFactor = scaleFactorHeight;
} else {
scaleFactor = scaleFactorWidth;
}
matrix.setScale(scaleFactor, scaleFactor, 0, 0);
setImageMatrix(matrix);
}
public void setIsScale(Boolean isScale){
this.isScale = isScale;
}
public Boolean getIsScale() {
return isScale;
}
}