Programing

안드로이드에서 svg를 사용하는 가장 쉬운 방법은 무엇입니까?

crosscheck 2020. 7. 15. 07:41
반응형

안드로이드에서 svg를 사용하는 가장 쉬운 방법은 무엇입니까?


안드로이드에서 svg를 사용하고 각 해상도마다 다른 해상도를 만들고 파일을 삭제하는 것을 피하기 위해 무수한 라이브러리를 찾았습니다. 앱에 많은 아이콘이나 이미지가 있으면 매우 성가 시게됩니다.

누구나 안드로이드에서 SVG를 사용하기 위해 라이브러리를 사용하는 가장 간단한 프로세스를 단계별로 제공 할 수 있다면 친절 할 수 있습니다. 많은 사람들도 도움이 될 것입니다.

또한 아이콘과 이미지를 생성하기 위해 Android Studio 및 Illustrator를 사용합니다.


먼저 svg간단한 단계를 수행하여 파일 을 가져와야 합니다.

  1. 드로어 블을 마우스 오른쪽 버튼으로 클릭
  2. 새로 클릭
  3. 벡터 자산 선택

컴퓨터에서 이미지를 사용할 수 있으면 로컬 svg파일 을 선택 하십시오. 그런 다음 이미지 경로를 선택하고 이미지 크기를 변경하는 옵션을 원하는 경우 대화 상자의 오른쪽에서도 사용할 수 있습니다. 이런 식으로 svg프로젝트에서 이미지를 가져옵니다.이 이미지를 사용하기 위해 동일한 절차를 따릅니다.

@drawable/yourimagename

업데이트 : 이 오래된 대답을 사용 하지 말고 더 잘 사용하십시오 : https://stackoverflow.com/a/39266840/4031815

몇 시간의 연구 끝에 svg-android 가 사용하기 쉽다는 것을 알았으므로 단계별 지침을 여기에 남겨 둡니다.

  1. https://code.google.com/p/svg-android/downloads/list 에서 lib를 다운로드 하십시오 . 작성 시점의 최신 버전은 다음과 같습니다.svg-android-1.1.jar

  2. 항아리를 libdir에 넣으십시오 .

  3. * .svg 파일을 res/drawabledir에 저장하십시오 (Illustrator에서는 다른 이름으로 저장을 누르고 svg를 선택하는 것만 큼 쉽습니다)

  4. svg 라이브러리를 사용하여 활동에서 다음을 코딩하십시오.

    ImageView imageView = (ImageView) findViewById(R.id.imgView);
    SVG svg = SVGParser.getSVGFromResource(getResources(), R.drawable.example);
    //The following is needed because of image accelaration in some devices such as samsung
    imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    imageView.setImageDrawable(svg.createPictureDrawable());
    



상용구 코드를 다음과 같이 줄일 수 있습니다

다음과 같이 과거 코드를 포함하고 상용구 코드를 줄이기 위해 간단한 클래스를 만들었습니다.

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;

import com.larvalabs.svgandroid.SVG;
import com.larvalabs.svgandroid.SVGParser;

public class SvgImage {

    private static ImageView imageView;
    private Activity activity;
    private SVG svg;
    private int xmlLayoutId;
    private int drawableId;


    public SvgImage(Activity activity, int layoutId, int drawableId) {
        imageView = (ImageView) activity.findViewById(layoutId);
        svg = SVGParser.getSVGFromResource(activity.getResources(), drawableId);
        //Needed because of image accelaration in some devices such as samsung
        imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        imageView.setImageDrawable(svg.createPictureDrawable());
    }
}

이제 활동에서 다음과 같이 호출 할 수 있습니다.

    SvgImage rainSVG = new SvgImage(MainActivity.this, R.id.rainImageView, R.drawable.rain);
    SvgImage thunderSVG = new SvgImage(MainActivity.this, R.id.thunderImageView, R.drawable.thunder);
    SvgImage oceanSVG = new SvgImage(MainActivity.this, R.id.oceanImageView, R.drawable.ocean);
    SvgImage fireSVG = new SvgImage(MainActivity.this, R.id.fireImageView, R.drawable.fire);
    SvgImage windSVG = new SvgImage(MainActivity.this, R.id.windImageView,R.drawable.wind);
    SvgImage universeSVG = new SvgImage(MainActivity.this, R.id.universeImageView,R.drawable.universe);

Android Studio는 1.4 이상 에서 SVG를 지원합니다

다음은 가져 오는 방법에 대한 비디오입니다 .


apk 크기를 증가시키는 라이브러리를 추가하는 대신 http://inloop.github.io/svg2android/를 사용하여 Svg를 드로어 블로 변환하는 것이 좋습니다 . vectorDrawables.useSupportLibrary = truegradle에 추가하십시오 .


Try the SVG2VectorDrawable Plugin. Go to Preferences->Plugins->Browse Plugins and install SVG2VectorDrawable. Great for converting sag files to vector drawable. Once you have installed you will find an icon for this in the toolbar section just to the right of the help (?) icon.


  1. you need to convert SVG to XML to use in android project.

1.1 you can do this with this site: http://inloop.github.io/svg2android/ but it does not support all the features of SVG like some gradients.

1.2 you can convert via android studio but it might use some features that only supports API 24 and higher that cuase crashe your app in older devices.

and add vectorDrawables.useSupportLibrary = true in gradle file and use like this:

<android.support.v7.widget.AppCompatImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:srcCompat="@drawable/ic_item1" />
  1. use this library https://github.com/MegatronKing/SVG-Android that supports these features : https://github.com/MegatronKing/SVG-Android/blob/master/support_doc.md

add this code in application class:

public void onCreate() {
    SVGLoader.load(this)
}

and use the SVG like this :

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_android_red"/>

참고URL : https://stackoverflow.com/questions/30923205/easiest-way-to-use-svg-in-android

반응형