くらげになりたい。

くらげのようにふわふわ生きたい日曜プログラマなブログ。趣味の備忘録です。

正方形のImageViewをカスタムViewで(Support Library版)

AndroidでRecyclerViewを使って、Gallery的な画面を作りたいなぁと思ったときの備忘録。
ほぼ、うさがにさんの記事の引用。Support LibraryのAppCompat版。

こんな感じ

public class SquareImageView extends android.support.v7.widget.AppCompatImageView {
    public SquareImageView(Context context) {
        super(context);
    }

    public SquareImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

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

        // 縦横サイズ一緒に
        int width = getMeasuredWidth();
        setMeasuredDimension(width, width);
    }
}

参考にしたサイト様