是否可以创建位图,将其添加到画布并使用view.draw绘制它?

时间:2014-03-23 10:30:40

标签: java android

例如......

        Bitmap myPic = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        Bitmap newPic = Bitmap.createScaledBitmap(myPic, 50, 50, true);
        Canvas myCanv = new Canvas(newPic);
        View myView = (View)findViewById(R.id.view1);
        myView.draw(myCanv);

就像上面那样可行吗?

1 个答案:

答案 0 :(得分:1)

你真的不应该这样做。我的建议是这样做

Bitmap myPic = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
myPic = Bitmap.createScaledBitmap(myPic,50,50, true);
BitmapDrawable draw = new BitmapDrawable(this.getResources(), myPic);
View myView = (View) findViewById (R.id.view1);
myView.setBackground(draw);

这样您就可以将View一个drawable作为背景传递。 如果您确实想要设置Bitmap作为来源,那么您应该制作ImageView