Sam*_*Sam 7 android paint surfaceview android-canvas
通过以下代码,我有一些问题.
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( new View(this) {
Paint mPaint = new Paint();
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
int width = this.getWidth();
int height = this.getHeight();
int radius = width > height ? height/2 : width/2;
int center_x = width/2;
int center_y = height/2;
// prepare a paint
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(5);
mPaint.setAntiAlias(true);
// draw a rectangle
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Paint.Style.FILL); //fill the background with blue color
canvas.drawRect(center_x - radius, center_y - radius, center_x + radius, center_y + radius, mPaint);
// draw some text and rotation
mPaint.setTextSize(50);
mPaint.setTextAlign(Paint.Align.CENTER);
mPaint.setColor(Color.BLACK);
canvas.drawText( "Hello World" , center_x , center_y, mPaint);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)

Q1:如何在框架中填充蓝色?(单词仍然出现)
Q2:这个应用程序中有多少视图和表面?我如何在应用程序中计算这些?
问题3:这款应用中有多少个窗口?
Q4:在代码中,我没有看到任何位图对象.但是,我认为位图是我可以在其上绘制内容的对象.我的理解不正确吗?一种可能性是Canvas构造函数在新的时候初始化位图.
问题5:我知道这些图形化的东西最终会浮出水面,然后传递给surfaceflinger进行最终合成.它在我的代码中的位置?
谢谢你的回复.
五个问题.让我们看看我可以提供哪些帮助.
Q1:告诉Paint填充矩形:paint.setStyle(Paint.Style.FILL);
Q2:我只看到您以编程方式创建的一个视图.你为什么要数这些观点?
Q3:再次:一个
问题4:你用一个包装它们来绘制一个可变位图Canvas.实际绘制的方法是其中的一部分Canvas
问题5:您显示的代码是活动的一部分.该活动由Android调用.这是您进入App的入口点.
谢谢你的回答。我完成了为标记答案制作代码的工作,并且它有效。
Bitmap bg = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bg);
// paint background with the trick
Paint rect_paint = new Paint();
rect_paint.setStyle(Paint.Style.FILL);
rect_paint.setColor(Color.rgb(0, 0, 0));
rect_paint.setAlpha(0x80); // optional
canvas.drawRect(0, 0, width, height, rect_paint); // that's painting the whole canvas in the chosen color.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28826 次 |
| 最近记录: |