Android ImageView - 16:9

问题描述:

我需要将Bitmap设置为ImageView以保持16:9的比例。你有什么建议吗?主要的解决方案可能是在自定义ImageView的覆盖方法onMeasure但是如何?Android ImageView - 16:9

+0

[如何将Imageview中的百分比相对布局设置为基于设备屏幕大小的16:9,1:1,3:2,2:1,4:3](https: //*.com/questions/36305054/how-to-set-imageview-in-percent-relative-layout-to-following-ratios-based-on-de) –

public class CustomImageView extends ImageView { 

    // some other necessary things 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     int width = getMeasuredWidth(); 

     //force a 16:9 aspect ratio    
     int height = Math.round(width * .5625f); 
     setMeasuredDimension(width, height); 
    } 
} 

然后在你的XML

<path.to.package.CustomImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/img"/> 

编辑:谷歌已正式弃用%的支持率文库开始API 26,你应该远离它,一旦动地。

集大小为比率 可以视图的大小设定为比,如16:如果视图尺寸中的至少一个被设置为“匹配约束” 9(0dp)。要启用比率,请单击切换长宽比约束(图10中的标注1),然后在显示的输入中输入宽高比。

如果宽度和高度都设置为匹配约束,则可以单击切换长宽比约束以选择基于另一维的比例的维。视图检查器通过用实线连接相应边缘来指示将哪个设置为比率。

例如,如果将两侧设置为“匹配约束”,请单击切换长宽比约束两次以将宽度设置为高度的比率。现在整个大小由图(其可以以任何方式来限定)的高度决定,如图如下:

The view is set to a 16:9 aspect with the width based on a ratio of the height.

的更多信息在这里:https://developer.android.com/training/constraint-layout/index.html#adjust-the-view-size


支持库中有一种叫PercentFrameLayoutPercentRelativeLayout的东西。

<android.support.percent.PercentFrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <ImageView 
    app:layout_widthPercent="100%" 
    app:layout_aspectRatio="178%" 
    android:scaleType="centerCrop" 
    android:src="@drawable/header_background"/> 
    <!-- The rest of your layout --> 
</android.support.percent.PercentRelativeLayout> 

如果你看看上面的布局仔细一看,你会发现有问题的ImageView(需要被固定为16:9)是PercentFrameLayout缠绕并有上设置的两个属性ImageView,你可能没有见过的:

app:layout_widthPercent="100%" 
app:layout_aspectRatio="178%" 

所以(1)您需要定义尺寸(宽度或高度)之一,为我们的问题ImageView领先尺寸。在这种情况下,ImageView将垂直扩展到它的最大宽度(如android:layout_width="match_parent") (2)您需要设置百分比(因此是库的名称)的纵横比,在这种情况下为178%(16/9 = 1.77777777778或更简单地将1.78:1或178%)。

阅读有关百分比支持库here的更多信息。

以上的答案并没有为我工作,我发现了这一点,并努力:

public class AspectRatioImageView extends ImageView { 
    // NOTE: These must be kept in sync with the AspectRatioImageView attributes in attrs.xml. 
    public static final int MEASUREMENT_WIDTH = 0; 
    public static final int MEASUREMENT_HEIGHT = 1; 

    private static final float DEFAULT_ASPECT_RATIO = 1f; 
    private static final boolean DEFAULT_ASPECT_RATIO_ENABLED = false; 
    private static final int DEFAULT_DOMINANT_MEASUREMENT = MEASUREMENT_WIDTH; 

    private float aspectRatio; 
    private boolean aspectRatioEnabled; 
    private int dominantMeasurement; 

    public AspectRatioImageView(Context context) { 
    this(context, null); 
    } 

    public AspectRatioImageView(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AspectRatioImageView); 
    aspectRatio = a.getFloat(R.styleable.AspectRatioImageView_aspectRatio, DEFAULT_ASPECT_RATIO); 
    aspectRatioEnabled = a.getBoolean(R.styleable.AspectRatioImageView_aspectRatioEnabled, 
     DEFAULT_ASPECT_RATIO_ENABLED); 
    dominantMeasurement = a.getInt(R.styleable.AspectRatioImageView_dominantMeasurement, 
     DEFAULT_DOMINANT_MEASUREMENT); 
    a.recycle(); 
    } 

    @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    if (!aspectRatioEnabled) return; 

    int newWidth; 
    int newHeight; 
    switch (dominantMeasurement) { 
     case MEASUREMENT_WIDTH: 
     newWidth = getMeasuredWidth(); 
     newHeight = (int) (newWidth/aspectRatio); 
     break; 

     case MEASUREMENT_HEIGHT: 
     newHeight = getMeasuredHeight(); 
     newWidth = (int) (newHeight * aspectRatio); 
     break; 

     default: 
     throw new IllegalStateException("Unknown measurement with ID " + dominantMeasurement); 
    } 

    setMeasuredDimension(newWidth, newHeight); 
    } 

    /** Get the aspect ratio for this image view. */ 
    public float getAspectRatio() { 
    return aspectRatio; 
    } 

    /** Set the aspect ratio for this image view. This will update the view instantly. */ 
    public void setAspectRatio(float aspectRatio) { 
    this.aspectRatio = aspectRatio; 
    if (aspectRatioEnabled) { 
     requestLayout(); 
    } 
    } 

    /** Get whether or not forcing the aspect ratio is enabled. */ 
    public boolean getAspectRatioEnabled() { 
    return aspectRatioEnabled; 
    } 

    /** set whether or not forcing the aspect ratio is enabled. This will re-layout the view. */ 
    public void setAspectRatioEnabled(boolean aspectRatioEnabled) { 
    this.aspectRatioEnabled = aspectRatioEnabled; 
    requestLayout(); 
    } 

    /** Get the dominant measurement for the aspect ratio. */ 
    public int getDominantMeasurement() { 
    return dominantMeasurement; 
    } 

    /** 
    * Set the dominant measurement for the aspect ratio. 
    * 
    * @see #MEASUREMENT_WIDTH 
    * @see #MEASUREMENT_HEIGHT 
    */ 
    public void setDominantMeasurement(int dominantMeasurement) { 
    if (dominantMeasurement != MEASUREMENT_HEIGHT && dominantMeasurement != MEASUREMENT_WIDTH) { 
     throw new IllegalArgumentException("Invalid measurement type."); 
    } 
    this.dominantMeasurement = dominantMeasurement; 
    requestLayout(); 
    } 
} 

注:一个辛博尔*更改为/(读取源的注释) 这在/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="AspectRatioImageView"> 
     <attr name="aspectRatio" format="float" /> 
     <attr name="aspectRatioEnabled" format="boolean" /> 
     <attr name="dominantMeasurement"> 
      <enum name="width" value="0" /> 
      <enum name="height" value="1" /> 
     </attr> 
    </declare-styleable> 
</resources> 

然后你就可以在你的布局(的aspectRatio =宽/高)用这样的方式10

Source

由于PercentFrameLayoutPercentRelativeLayout是在API层面26.0.0过时了,我建议你考虑使用的ConstraintLayout保持比例16:9的ImageViewConstraintLayout是构建适用于Android平台的响应式UI的强大工具,您可以在这里找到更多详细信息Build a Responsive UI with ConstraintLayout

这里的例子,如何建立你的ImageView到ConstraintLayout保持16:9比例:

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_marginEnd="0dp" 
     android:layout_marginStart="0dp" 
     android:layout_marginTop="0dp" 
     app:srcCompat="@mipmap/ic_launcher" 
     app:layout_constraintDimensionRatio="H,16:9" 
     app:layout_constraintStart_toStartOf="parent" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

</android.support.constraint.ConstraintLayout> 

不要忘了constraint-layout依赖添加到您的模块的build.gradle文件

implementation "com.android.support.constraint:constraint-layout:1.0.2" 

或者不是编辑XML文件,直接在布局编辑器中编辑布局:

Layout Editor