从assets文件夹中读取SVG获取错误?

时间:2017-02-20 13:17:48

标签: android android-studio svg android-studio-2.2

我正在使用Android studio 2.2并在我的Android项目中使用AndroidSVG SVG渲染库。以下是读取特定SVG的相关代码部分。

// Read an SVG from the assets folder
SVG  svg = SVG.getFromAsset(getContext().getAssets(), filename);

// Create a canvas to draw onto
if (svg.getDocumentWidth() != -1) {
   Bitmap  newBM = Bitmap.createBitmap(Math.ceil(svg.getDocumentWidth()),
                                       Math.ceil(svg.getDocumentHeight()),
                                       Bitmap.Config.ARGB_8888);
   Canvas  bmcanvas = new Canvas(newBM);

   // Clear background to white
   bmcanvas.drawRGB(255, 255, 255);

   // Render our document onto our canvas
   svg.renderToCanvas(bmcanvas);
}

我已正确创建了Assets文件夹但在getAssets()后面的行显示错误无法解析方法。

SVG svg = SVG.getFromAsset(getContext().getAssets(), Freesample);

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

getAssets() is a method on an android.content.Context object。根据您的评论,android.content.Context没有返回其中一个。您需要获得Activity,例如getAssets(),并将其用于import UIKit import PlaygroundSupport var screenSize = CGSize(width: 1920, height: 1080) let containerView = UIView(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height)) containerView.backgroundColor = UIColor.green func chopImageFromRect(image: UIImage, newRect: CGRect) -> UIImage { let img = CIImage(image: image)!.cropping(to: newRect) let image = UIImage(ciImage: img, scale: 1.0, orientation: image.imageOrientation) return image } func combine2LONGImage(imageUn: UIImage, imageDeux: UIImage) -> UIImage { let size = CGSize(width: imageUn.size.width + imageDeux.size.width, height: imageUn.size.height) UIGraphicsBeginImageContext(size) imageUn.draw(in: CGRect(x: 0, y: 0, width: imageUn.size.width, height: imageUn.size.height)) imageDeux.draw(in: CGRect(x: imageUn.size.width, y: 0, width: imageDeux.size.width, height: imageDeux.size.height)) let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() return newImage } func resizeLONGImage(image: UIImage, newSize: CGSize) -> UIImage { UIGraphicsBeginImageContext(newSize) image.draw(in: CGRect(x:0, y:0, width: newSize.width, height: newSize.height)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } var finalImage: UIImage? // original image size is 2048 x 768 let image2S = UIImage(named: "orig2048x768.png") // target image size should be 3840 x 1080 let targetImageSize = CGSize(width: screenSize.width * 2.0, height: screenSize.height) if let sz = image2S?.size { // get the left side - x: 0 y: 0 / 1024 x 768 - of image let leftHalf = chopImageFromRect(image: image2S!, newRect: CGRect(x: 0, y: 0, width: sz.width / 2, height: sz.height)) // get the right side - x: 1024 y: 0 / 1024 x 768 - of image let rightHalf = chopImageFromRect(image: image2S!, newRect: CGRect(x: sz.width / 2, y: 0, width: sz.width / 2, height: sz.height)) // target size for each is Half of the Target Image Size (double the screen width x the screen height) let targetHalfSize = CGSize(width: targetImageSize.width / 2, height: targetImageSize.height) // scale each half to targetHalfSize let newLH = resizeLONGImage(image: leftHalf, newSize: targetHalfSize) let newRH = resizeLONGImage(image: rightHalf, newSize: targetHalfSize) // combine the two newly-scaled halfs finalImage = combine2LONGImage(imageUn: newLH, imageDeux: newRH) // create an UIImageView the same size as the screen let imgView = UIImageView(frame: containerView.bounds) // set contentMode to .left imgView.contentMode = .left // set the image of the image view imgView.image = finalImage // add the image view to the screen containerView.addSubview(imgView) } PlaygroundPage.current.liveView = containerView PlaygroundPage.current.needsIndefiniteExecution = true 来电。