컴퓨터과학/Android
2014. 2. 13. 19:20
간혹 ImageView가 정사각형으로 나왔으면 하는 경우가 있다.
단순히 정사각형이라면 layout_width와 layout_height를 같은 수치로 넣어주면 되지만...
그런 경우 동적으로 사이즈가 늘어나진 않아 불편하다.
아래 사이트에 답이 있다.
http://android-layouts.com/category/tags/square
위 내용을 참고하여 간단히 정리하면 ImageView를 상속받는 클래스를 생성 후 onMeasure() 메서드를 오버라이딩하고 width와 height를 동일하게 맞춰주면 된다. (가로 세로 둘 중에 짧은 길이로 통일)
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//Get canvas width and height int w = MeasureSpec.getSize(widthMeasureSpec); int h = MeasureSpec.getSize(heightMeasureSpec); w = Math.min(w, h); h = w; setMeasuredDimension(w, h); }
이 방법은 ImageView 뿐만 아니라 대부분의 컨트롤에 적용 가능하다.
<com.lovejb.example.Views.SquareImageView android:layout_width="match_parent" android:layout_height="match_parent"
/>
xml layout에서는 위와 같은 식으로 사용하면 된다.
'컴퓨터과학 > Android' 카테고리의 다른 글
[Android] 인터페이스를 이용한 이벤트의 처리 (0) | 2014.11.22 |
---|---|
[Android] nine patch image에서 bad patch란? (0) | 2014.04.24 |
정사각형 ImageView (0) | 2014.02.13 |
[SQLite] ROWID에 관하여 (0) | 2014.02.04 |
사용자 Adapter 사용시 ListView 갱신 (0) | 2013.05.24 |
댓글을 달아 주세요