블로그 이미지
흰색앵초

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Notice

2014. 10. 5. 16:50 프로그래밍/Android

ImageView(이미지뷰)의 테두리에 라운드를 주고 싶을땐 아래와 같이 처리하면됩니다.

먼저 ImageView를 상속받아 아래와 같이 재정의 하는 Java 클래스를 만들어줍니다.

※ RadiusImageView.java 


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RadiusImageView extends ImageView {

// 라운드처리 강도 값을 크게하면 라운드 범위가 커짐
    public static float radius = 18.0f;  

    public RadiusImageView(Context context) {
        super(context);
    }

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

    public RadiusImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Path clipPath = new Path();
        RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
        clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
        canvas.clipPath(clipPath);
        super.onDraw(canvas);
    }
}


※ 레이아웃 xml 파일 (ex:activity_main.xml)

    <com.example.image_crop2.RadiusImageView

        android:id="@+id/iv1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="48dp"

        android:src="@drawable/img5" />


위의 xml값을 보시면 패키지명.RadiusImageView로 선언해서 ImageView와 같은 방식으로 쓰시면됩니다.


※ 프로그래밍에서 참조해서 컨트롤을 하시려면...


public class MainActivity extends Activity {

	RadiusImageView iv1;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        iv1 = (RadiusImageView) findViewById(R.id.iv1);
        
    }
}

위와 같이 RadiusImageView로 참조해서 사용하시면 됩니다.

출처 : http://stackoverflow.com/questions/18229358/bitmap-in-imageview-with-rounded-corners

비슷한 포스팅 : Bitmap 테두리 라운드처리하기

posted by 흰색앵초