나는이 문제를 해결하기 위해 내 머리를 감싸는 데별로 행운이 없다. 그래서 나를 도와 줄 수 있기를 바란다.Canvas를 확장하여 Android에서 SVG 크기 조정
SVG에서 svg-android를 사용하여 드로어 블을 가져오고 있지만 드로어 블이 뷰로 스케일링되지 않습니다. 내가 찾을 수 있었던 모든 것은 캔버스에 직접 그려야하고 캔버스를 다시 조정해야한다고 말하지만, 시도 할 때 경계를 변경하지만 이미지의 크기를 조정하지 않는 것만 같습니다.
이
내가 지금까지 시도한 것입니다 :ImageView testview = (ImageView)findViewById(R.id.testview);
//Get SVG and convert to drawable
SVG vector = SVGParser.getSVGFromResource(getResources(),R.drawable.testvector);
Drawable test = vector.createPictureDrawable();
testview.setBackground(test); //displays fine, but won't scale to the dimensions of
//the View
//function that clips the image but doesn't scale:
Drawable testTwo = new CustomPictureDrawable(vector.getPicture(),
(float)0.5, (float)0.5);
testView.setBackground(testTwo);
class CustomPictureDrawable extends PictureDrawable {
private float scalex, scaley;
public CustomPictureDrawable(Picture picture, float scalex, float scaley) {
super(picture);
this.scalex = scalex;
this.scaley = scaley;
}
@Override
public void draw(Canvas canvas) {
Matrix original = canvas.getMatrix();
canvas.scale(scalex, scaley);
super.draw(canvas);
canvas.setMatrix(original);
}
}
//doesn't display anything
Picture testThree = vector.getPicture();
Bitmap b = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_4444);
Canvas c = new Canvas(b);
c.drawPicture(testThree, new Rect(0,0,10,10));
testview.draw(c);
나는 또한 스케일 비트 맵을 생성하는 함수를 발견했지만, 화질이 현저히 감소되기 때문에 나뿐만 아니라 바로 사용할 수 있습니다 확장 된 PNG.
분명히 나는 뭔가를 놓치고 있으며 경험 부족으로 정말 실망 스럽습니다. 내가 할 수있을 같은 을했던 것과
는 그것의 사진 또는 PictureDrawable를 당겨 전에 SVG-안드로이드 완전히 다시 규모에게 SVG를 가지고,하지만 난 SVGParser를 단계별로하는 방법을 알아낼 수 없습니다 어쨌든 모든 좌표 쌍에서 승수를 사용하면 아마도 수퍼 리소스가 많이 소모됩니다.
[편집] 그림을 크기 조정하고 다시 그려서보기에 할당하여 사용자 지정보기를 만들고 OnDraw를 재정의하는 유일한 방법은 무엇입니까?
즉
Picture testThree = vector.getPicture();
Bitmap b = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_4444);
Canvas c = new Canvas(b);
c.drawPicture(testThree, new Rect(0,0,10,10));
//CustomView extends ImageView or Button or whatever with OnDraw overridden and no
//other changes
CustomView testview = (CustomView)findViewById(R.id.testview);
testview.OnDraw(c);
오전 나는 곧 정상 궤도에? 캔버스 C는 기본 캔버스를 덮어 씁니다 (내가 원하는대로). 그렇지 않습니까?
솔루션이 사용자 정의보기의 onDraw 메서드에 있다고 가정하고 해당 메서드를 직접 호출하지 않는다고 판단한 경우 ... ;-) 새 Canvas를 만드는 데 아무 것도 할 필요가 없습니다. 또는 심지어 비트 맵. 그렇게하면 svg를 사용할 때 효율성과 관련된 이점 (메모리 관련)을 잃게됩니다. 대신 onDraw 메서드에 전달 된 캔버스를 사용하여 canvas.drawPicture (vector.getPicture(), 새 Rect (0,0, width, height))를 호출 할 수 있습니다. 뷰 클래스를 확장 가능하게하려면 원시 svg의 이름을 나타내는 스타일 가능한 속성을 추가 한 다음 ... – wkhatch
(2)을 입력 한 다음 String resourceName을 사용하여 원시 이름에 해당하는 리소스 ID에 액세스합니다 = a.getString (R.styleable.YouViewClassName_yourViewsCustomAttributeName); svgResourceId = getResources(). getIdentifier (resourceName, "raw", getContext(). getPackageName()); – wkhatch
아, 그리고 변수는 TypedArray 객체입니다. 뷰에 지정한 스타일 속성을 모두 액세스 할 수 있으며 일반적으로 생성자 중 하나에서 설정하는 것이거나 더 나은 방법으로 생성자가 호출하는 간단한 방법입니다. 둘 중 하나에서 다음과 같은 작업을 수행 할 수 있습니다. final TypedArray a = getContext(). obtainStyledAttributes (attrs, R.styleable.YourCustomViewClassName, defStyle, 0); – wkhatch