2012-06-09 2 views
1

드로어 블 setShaderFactory() 메서드를 사용하여 PathShape 사용자 정의 ShapeDrawable을 채우는 데 문제가 있습니다. 나는 사용자 지정 PathShapeRectShape은, 그러나, 당김 만 그라데이션 시작 색상 (빨간색)와 전체 모양을 채 웁니다 변경하면그라디언트로 ShapeDrawable (PathShape 포함)을 채우는 방법

ShapeDrawable shape = new ShapeDrawable(); 
shape.setShape(new RectShape()); 
shape.setShaderFactory(new ShaderFactory() { 
    @Override 
    public Shader resize(int width, int height) { 
     LinearGradient gradient = new LinearGradient (0, 0, 
       width, height, Color.Red, Color.Blue, 
       TileMode.REPEAT); 
     return gradient; 
    } 
}); 

하십시오 RectShape을 그릴 때 다음 코드는 완벽하게 작동합니다. 즉, 사용자 정의 모양이 올바르게 그려 지지만 색상이 완전히 잘못되었습니다. 아무도 전에 이것을보고 문제가 무엇인지 알았습니까?

답변

0

실험을 한 후 그라디언트의 크기는 PathShape의 표준 너비와 표준 높이와 관련이 있어야하며,이 너비와 높이는 ShapeDrawable의 높이와 너비와 관련이 없습니다. 즉, ShapeDrawable의 전체 수명 동안 PathShape에 할당 한 표준 너비/높이를 추적해야합니다.

public static final int STD_WIDTH = 20; 
public static final int STD_HEIGHT = 20; 

PathShape shape = new PathShape(myPath, STD_WIDTH, 
     STD_HEIGHT); 
ShapeDrawable drawable = new ShapeDrawable(); 
drawable.setShape(myPathShape); 
drawable.setShaderFactory(new ShaderFactory() { 
    @Override 
    public Shader resize(int width, int height) { 
     LinearGradient gradient = new LinearGradient (0, 0, 
       STD_WIDTH, STD_HEIGHT, Color.Red, Color.Blue, 
       TileMode.REPEAT); 
     return gradient; 
    } 
}); 
:

다소 우아하지만, 여기에 솔루션입니다

관련 문제