【问题标题】:Displaying SVG file on android real device在android真机上显示SVG文件
【发布时间】:2014-08-22 08:00:19
【问题描述】:

我想在 android 中显示 svg 文件。我使用 Library svg-android 。但是它不起作用。我下载了示例项目。它在设备上给出“app1 已停止”错误。另外,我尝试了硬件部分。我使用了这个代码部分。

super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    SVG svg = SVGParser.getSVGFromResource(getResources(),R.raw.listitemsvg);
 //   Picture picture = svg.getPicture();
    Drawable drawable = svg.createPictureDrawable();
   // Button button1 =   (Button) findViewById( R.id.button1 ) ;
    ImageView image = (ImageView) findViewById(R.id.imageView1 );
    image.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
     image.setImageDrawable(drawable);
    // button1.setBackground(drawable);

如您所见,首先我在按钮视图上进行了尝试。之后,我在imageview上尝试了它。两者都不起作用。我想知道为什么它在安卓设备上不起作用?

Logcat:

08-21 23:10:31.800: I/dalvikvm(4660): 找不到方法 com.larvalabs.svgandroid.SVGParser.getSVGFromResource,引用自方法 com.example.budogui.MainActivity.onCreate 08-21 23:10:31.800: W/dalvikvm(4660): VFY: 无法解析静态方法 8440: Lcom/larvalabs/svgandroid/SVGParser;.getSVGFromResource (Landroid/content/res/Resources;I)Lcom/larvalabs/ SVG安卓/SVG; 08-21 23:10:31.800: D/dalvikvm(4660): VFY: 在 0x000f 处替换操作码 0x71 08-21 23:10:31.880: D/AndroidRuntime(4660): 关闭虚拟机 08-21 23:10:31.880: W/dalvikvm(4660): threadid=1: 线程以未捕获的异常退出 (group=0x40ec3930) 08-21 23:10:31.880:E/AndroidRuntime(4660):致命异常:主要 08-21 23:10:31.880: E/AndroidRuntime(4660): java.lang.NoClassDefFoundError: com.larvalabs.svgandroid.SVGParser

文件: http://imgh.us/listitemsvg.svg

【问题讨论】:

  • "它在设备上给出了 'app1 is stopped` 错误" -- stackoverflow.com/questions/23353173/…
  • 这对我们来说是非常有用的信息。谢谢。我还是有问题。由于声誉较低,我无法分享照片
  • 我不确定您指的是哪张照片。如果您无法解释 Java 堆栈跟踪,请将堆栈跟踪本身作为文本粘贴到您的问题中。除此之外,如果您希望在问题中包含一些图片,请将图片上传到某处并从您的问题中链接到它们。
  • 你是对的。我编辑了
  • 听起来您可能没有将库正确添加到项目中。如果您使用的是 Eclipse,您应该已经将 JAR 文件复制到您的 libs/ 目录中,并且什么都不做。

标签: android button svg imageview


【解决方案1】:

如果将所有图像都转换为 Base64 会更容易,例如最近制作位图。

public Bitmap getBitmap(String image) {
    byte[] image_bytes = Base64.decode(image.getBytes(), Base64.DEFAULT);
    Bitmap bitmap = BitmapFactory.decodeByteArray(image_bytes, 0, image_bytes.length);
    return bitmap;
}

public Bitmap resizeBitmap(Bitmap original_image, float width, float height){
    float originalWidth = original_image.getWidth(), originalHeight = original_image.getHeight();
    float scale = width/originalWidth;
    float xTranslation = 0.0f, yTranslation = (height - originalHeight * scale)/2.0f;

    Bitmap background = Bitmap.createBitmap((int)width, (int)height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(background);
    Matrix transformation = new Matrix();
    Paint paint = new Paint();

    transformation.postTranslate(xTranslation, yTranslation);
    transformation.preScale(scale, scale);
    paint.setFilterBitmap(true);

    canvas.drawBitmap(original_image, transformation, paint);
    return background;
}

【讨论】:

  • 这个工作像 SVG 文件吗?我有解决问题,所以我想使用 svg。如果这可行,我可能更喜欢这个
  • 确实,这种形式最适合支持多种分辨率的图片,我一直都这样
猜你喜欢
  • 2019-02-18
  • 2010-10-31
  • 2021-01-14
  • 2015-12-15
  • 2016-12-14
  • 2019-01-06
  • 2021-07-30
  • 1970-01-01
  • 2020-11-18
相关资源
最近更新 更多