Example #1
0
  private void reloadSvg(final boolean changeColor) {
    mSvg.setOnElementListener(
        new OnSvgElementListener() {

          @Override
          public void onSvgStart(@NonNull Canvas canvas, @Nullable RectF bounds) {}

          @Override
          public void onSvgEnd(@NonNull Canvas canvas, @Nullable RectF bounds) {}

          @Override
          public <T> T onSvgElement(
              @Nullable String id,
              @NonNull T element,
              @Nullable RectF elementBounds,
              @NonNull Canvas canvas,
              @Nullable RectF canvasBounds,
              @Nullable Paint paint) {
            if (changeColor
                && paint != null
                && paint.getStyle() == Paint.Style.FILL
                && ("shirt".equals(id) || "hat".equals(id) || "pants".equals(id))) {
              Random random = new Random();
              paint.setColor(
                  Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256)));
            }
            return element;
          }

          @Override
          public <T> void onSvgElementDrawn(
              @Nullable String id,
              @NonNull T element,
              @NonNull Canvas canvas,
              @Nullable Paint paint) {}
        });
    mSvg.getSharpPicture(
        new Sharp.PictureCallback() {
          @Override
          public void onPictureReady(SharpPicture picture) {
            {
              Drawable drawable = picture.getDrawable(mImageView);
              mImageView.setImageDrawable(drawable);
            }

            {
              // We don't want to use the same drawable, as we're specifying a custom size;
              // therefore
              // we call createDrawable() instead of getDrawable()
              int iconSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
              Drawable drawable = picture.createDrawable(mButton, iconSize);
              mButton.setCompoundDrawables(drawable, null, null, null);
            }

            mAttacher.update();
          }
        });
  }
Example #2
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_svg_demo);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mImageView = (ImageView) findViewById(R.id.iv_image);
    mButton = (Button) findViewById(R.id.bt_button);

    mSvg = Sharp.loadResource(getResources(), R.raw.cartman);
    // If you want to load typefaces from assets:
    //          .withAssets(getAssets());

    // If you want to load an SVG from assets:
    // mSvg = Sharp.loadAsset(getAssets(), "cartman.svg");

    mButton.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            reloadSvg(true);
          }
        });

    mAttacher = new PhotoViewAttacher(mImageView);
    mAttacher.setMaximumScale(10f);

    reloadSvg(false);
  }