一、效果
實(shí)現(xiàn)原理比較簡(jiǎn)單,本章就不做原理講解。一切只因?yàn)轫?xiàng)目中經(jīng)常遇到設(shè)置圖片比例,于是便想到把功能封裝一下方便以后使用。
先看效果:圖1是xml代碼,圖2是效果

AutoImageView-code.png

AutoImageView-result.png
二、代碼
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.util.Log;
import com.fun.ex.app.R;
/**
* 作者: Created by AdminFun
* 郵箱: 614484070@qq.com
* 創(chuàng)建: 2019/1/24
* 修改: 2019/1/24
* 版本: v1.0.0
* 描述: 自動(dòng)計(jì)算并重置寬高的圖片控件,如果有需求可以繼承自其他功能圖片控件
*/
public class AutoSizeImageView extends AppCompatImageView {
private final String TAG = "common";
public AutoSizeImageView(Context context) {
super(context);
}
public AutoSizeImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AutoSizeImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.AutoSizeImageView, defStyleAttr, 0);
mPercentWidth = attributes.getInt(R.styleable.AutoSizeImageView_widget_width_percent, 1);
mPercentHeight = attributes.getInt(R.styleable.AutoSizeImageView_widget_height_percent, 1);
attributes.recycle();
}
private int mPercentWidth = 0, mPercentHeight = 0; // 想要設(shè)置的寬高比例
private int mCurrentWidth = 0, mCurrentHeight = 0; // 當(dāng)前控件在視圖中未處理的寬高
private int mCurrentMode = 4; // 當(dāng)前控件的寬高類型,詳細(xì)類型見(jiàn) getType 注釋
/**
* 設(shè)置控件寬高比:這里只設(shè)置比例即可
* 例如:想設(shè)置寬高比為4:3,那么即可設(shè)置 width = 40,高=30?;蛘遷idth=4,高=3
*/
public void setPercent(int width, int height) {
this.mPercentWidth = width < 0 ? 0 : width;
this.mPercentHeight = height <= 0 ? 1 : height;
this.reMeaureView(mCurrentMode, mCurrentWidth, mCurrentHeight);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// final int minimumWidth = getSuggestedMinimumWidth();
// final int minimumHeight = getSuggestedMinimumHeight();
final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
this.mCurrentWidth = MeasureSpec.getSize(widthMeasureSpec);
this.mCurrentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.mCurrentMode = getType(widthSpecMode, heightSpecMode);
Log.d(TAG, String.format("AutoSizeImageView.onMeasure:width = %s,height = %s,mode = %s",
String.valueOf(mCurrentWidth), String.valueOf(mCurrentHeight), String.valueOf(mCurrentMode)));
this.reMeaureView(mCurrentMode, mCurrentWidth, mCurrentHeight);
}
private void reMeaureView(int CurrentMode, int MeasureWidth, int MeasureHeight) {
switch (CurrentMode) {
case 1:// 已知寬度和比例,不理會(huì)高度。
setMeasuredDimension(MeasureWidth, getHeightByWidth(MeasureWidth, MeasureHeight));
break;
case 3:// 已知高度和比例,不理會(huì)寬度
setMeasuredDimension(getWidthByHeight(MeasureWidth, MeasureHeight), MeasureHeight);
break;
case 2 | 4:// 寬度和高度都已知或都未知,完全不用理會(huì)
// TODO 都已知的情況下是不需要處理的,暫時(shí)還未想到都未知的場(chǎng)景。
break;
}
}
private int getHeightByWidth(int width, int height) {
return (mPercentWidth <= 0 || mPercentHeight <= 0) ?
height : width * mPercentHeight / mPercentWidth;
}
private int getWidthByHeight(int width, int height) {
return (mPercentWidth <= 0 || mPercentHeight <= 0) ?
height : height * mPercentWidth / mPercentHeight;
}
/**
* 四種類型
* 1、width = match, height = warp,已知寬度和寬高比,計(jì)算高度size
* 2、width = match, height = match,寬和高都已知,寬高比也有,這種情況下:不予理睬
* 3、width = warp, height = match,已知高度和寬高比,計(jì)算寬度,重置size
* 4、width = warp, height = warp,寬和高都未知,且知道比例,這種情況下:不予理睬
* 注意:這里不考慮寬高都未設(shè)置的情況,默認(rèn)第4種情況
*/
private int getType(int widthMode, int heightMode) {
switch (widthMode) {
case MeasureSpec.EXACTLY: // 已知寬度
if (heightMode == MeasureSpec.AT_MOST) {
return 1;
} else if (heightMode == MeasureSpec.EXACTLY) {
return 2;
} else {
return 1;
}
case MeasureSpec.AT_MOST | MeasureSpec.UNSPECIFIED: // 寬度未知
if (heightMode == MeasureSpec.AT_MOST) {
return 4;
} else if (heightMode == MeasureSpec.EXACTLY) {
return 3;
} else {
return 4;
}
}
return 4;
}
}
<declare-styleable name="AutoSizeImageView">
<attr name="widget_width_percent" format="integer" />
<attr name="widget_height_percent" format="integer" />
</declare-styleable>
三、使用
方法一(設(shè)置比例并非是設(shè)置寬高)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.fun.ex.app.custom_view.AutoSizeImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerCrop"
android:src="@drawable/fang_"
app:widget_height_percent="1"
app:widget_width_percent="1" />
<android.support.v7.widget.AppCompatImageView
android:id="@+id/image1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#56ffff"
android:scaleType="centerCrop"
android:src="@drawable/fang_" />
</LinearLayout>
方法二
autoSizeImageView.setPercent(1, 1);