我做的SVG的路径和影像作品。我已加载SVG文件,并得到一个形象,尝试设置在画布上THI图像。但是画布不显示图像。我入住的高度和宽度,此图片/照片的无效支票,不为空,所以我无法理解,之所以画布不显示图像。任何帮助
我的code:
公共类MainActivity延伸活动{ 上下文℃; @覆盖 公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); C = getApplicationContext(); 的setContentView(新GameView(本)); } 公共类GameView扩展视图{ 私人诠释宽度,高度; 私人长期svgId; 画中画; 长的startTime; 浮动比例因子; 公共GameView(上下文的背景下){ 超级(上下文); SVG SVG = SVGParser.getSVGFromResource(getResources(),R.raw.android); 照片= svg.getPicture(); } @覆盖 保护无效onLayout(布尔变化,诠释离开,诠释顶部,右诠释,诠释底部){ //获取可视面积 宽度=右 - 左; 高度=底 - 顶; } @覆盖 公共无效的onDraw(帆布油画){ //画一个白色背景... canvas.drawColor(Color.BLACK); 如果(帆布!= NULL) { Toast.makeText(C,yahooooooooooooooooo+ picture.getHeight(),Toast.LENGTH_LONG).show(); 比例因子= Math.min((浮动)的getHeight()/ picture.getHeight(),(浮动)的getWidth()/ picture.getWidth()); canvas.scale((浮动)的比例因子,(浮点)比例因子); canvas.drawPicture(图片); } } }}
解决方案
这是我用返回一个对象绘制,我将通过我的所有应用使用它的工作原理相当不错的。
最后BitmapDrawable getSVG_Drawable (INT svgResourceID,诠释的宽度,高度INT){ //从资源中的SVG /原始图片。 最终SVG矢量= SVGParser.getSVGFromResource(getResources(),svgResourceID); 最后DisplayMetrics指标=新DisplayMetrics(); 。getWindowManager()getDefaultDisplay()getMetrics(指标)。 //重绘图片到一个新的大小。 最后的BMP位图= Bitmap.createBitmap(宽度,高度,Bitmap.Config.ARGB_8888); 最后帆布CNV =新的Canvas(BMP); cnv.setDensity((int)的(metrics.xdpi)); cnv.drawPicture(vector.getPicture(),新的Rect(0,0,宽度,高度)); 最后BitmapDrawable DRW =新BitmapDrawable(getResources(),BMP); //返回绘制。 返回DRW;}
I am doing a work on SVG Paths and Image. I have loaded SVG file and get an image and try to set thi image on canvas . But canvas is not showing image. I check the height and width and null check of this image/picture and it is not null so i am unable to understand that why canvas is not showing image. any help
My code:
public class MainActivity extends Activity{
Context c;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
c=getApplicationContext();
setContentView(new GameView(this));
}
public class GameView extends View{
private int width, height;
private long svgId;
Picture picture;
long startTime;
float scaleFactor;
public GameView(Context context) {
super(context);
SVG svg = SVGParser.getSVGFromResource(getResources(),R.raw.android);
picture = svg.getPicture();
}
@Override
protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
// get visible area
width = right - left;
height = bottom - top;
}
@Override
public void onDraw(Canvas canvas) {
// paint a white background...
canvas.drawColor(Color.BLACK);
if (canvas!=null)
{
Toast.makeText(c, "yahooooooooooooooooo"+picture.getHeight(), Toast.LENGTH_LONG).show();
scaleFactor=Math.min((float)getHeight()/picture.getHeight(),(float)getWidth()/picture.getWidth());
canvas.scale((float)scaleFactor,(float)scaleFactor);
canvas.drawPicture(picture);
}
}
}
}
解决方案
This is what I use to return a drawable object that I will use through all my app It works quite well.
final BitmapDrawable getSVG_Drawable
(int svgResourceID, int width, int height)
{
// Get a Picture from the SVG in res/raw.
final SVG vector =
SVGParser.getSVGFromResource(getResources(), svgResourceID);
final DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
// Redraw the picture to a new size.
final Bitmap bmp =
Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
final Canvas cnv = new Canvas(bmp);
cnv.setDensity((int) (metrics.xdpi));
cnv.drawPicture(vector.getPicture(), new Rect(0, 0, width, height));
final BitmapDrawable drw = new BitmapDrawable(getResources(), bmp);
// Return the drawable.
return drw;
}