2013-10-23 2 views
7

최근 ImageView에서 색상 테두리가있는 이미지를 순환시키는 CircularImageView 클래스를 만들기 위해 확장했습니다. 이렇게 전달 된 캔버스에 그려서 된 onDraw (캔버스) 방법을 통해 수행된다 : 그릴 수 또는 비트 맵 이미지로 설정시Picasso 이미지 다운로드 라이브러리와 호환되지 않는 사용자 정의 ImageView 클래스

//load the bitmap 
    loadBitmap(); 

    // init shader 
    if(image !=null) 
    { 
     shader = new BitmapShader(Bitmap.createScaledBitmap(image, viewWidth + (borderWidth * 2), viewHeight + (borderWidth * 2), true), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 
     paint.setShader(shader); 

     int circleCenter = viewWidth/2; 

     // circleCenter is the x or y of the view's center 
     // radius is the radius in pixels of the cirle to be drawn 
     // paint contains the shader that will texture the shape 
     canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter + borderWidth, paintBorder); 
     canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter, paintBackground); 
     canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter, paint); 
    } 

그래서이 비트는 작동한다. 또한 Google의 Volley NetworkImageView에서도 사용할 수 있도록 확장했습니다.

Volley의 대안으로 피카소 이미지 다운로드 라이브러리와 함께 CircularImageView 클래스를 사용해 보았을 때 내 문제가 발생했습니다. BitmapDrawable을 가져올 때 첫 행의 loadBitmap() 함수에서 ClassCastException이 발생합니다.

private void loadBitmap() 
{ 
    BitmapDrawable bitmapDrawable = (BitmapDrawable) this.getDrawable(); 

    if(bitmapDrawable != null) 
     image = bitmapDrawable.getBitmap(); 
} 

처음에는 Picasso가 그림을 다운로드하기 전에 자리 표시 자 이미지를 반올림했습니다. 그러나 Picasso에서 이미지를 다운로드하자 마자 ClassCastException이 발생하고 BitmapDrawable이 아닌 getDrawable()이 반환되고 PicassoDrawable이 반환됩니다.

나는 CircularImageView 클래스의 onDraw (canvas) 메소드에서 이미지를 반올림하기를 계속하고 싶다. 피카소로 ImageView를 설정할 때마다 프로세스를 수행하는 것과는 정반대로 자동적으로 포함된다. . 이것이 가능한가?

미리 감사드립니다. 피카소와 함께이 일을 할 때

답변

14

피카소를 사용하는 원형 이미지의 경우 변환을 구현하는 this 클래스를 사용하십시오.

Picasso.with(context).load(url).transform(new RoundedTransformation(radius, margin)).into(imageview); 
+0

이렇게하면 매번 새로운 비트 맵이 만들어집니다. – secureboot

+0

DesertIvy, droidx 및 Jake Wharton에게 도움을 주셔서 감사합니다. 이 프로젝트는 보류 상태 였고 이제는 문제가 다시 돌아 왔습니다. 나는 방금 완벽한 작품으로 위를 시도했다. –

5

당신이해야 하나 :

  1. 은 둥근 비트 맵을 메모리에 캐시되도록 Transformation, 또는
  2. 클램프 사용자 정의 ImageView 서브 클래스의 쉐이더와 캔버스로 반올림 적용 . 이 기술에 대한 상세 정보가 ImageView에서 기본 Bitmap을 끌어 시도 ragin 'cagin'에 의해 로맹 가이 on his blog

을 요약 한 것은 반 패턴이다. Bitmap에 액세스해야하는 경우 (위의 방법 중 하나를 사용해야 함) Target을 구현하여 onBitmapSuccess 콜백이 제공하는보기에 구현하십시오.

관련 문제