私はこの
//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
まで伸びcuztomizedクラスを作成をやってみましたが/ 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;
}
}
画像をコンテナにフィットさせるのはまだ「アスペクトフィッティング」です(画像をカットしない)。 – natsumiyu
ですが、 'centerCrop'を使って中央に切り抜きます。それを左揃えする方法はありますか? – natsumiyu