抱歉我的英文不好。
我尝试使用drawText(),drawRect()和drawBitmap()在屏幕上绘制一些东西。 为什么坐标必须是“浮动”? 它们的类型应为“int”?
答案 0 :(得分:0)
以下是代码:
package com.prove.provaproject_29_g;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.content.Context;
import android.graphics.*;
public class MainActivity extends ActionBarActivity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
class MyView extends View
{
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int x = getWidth();
int y = getHeight();
int radius;
radius = 100;
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
// Use Color.parseColor to define HTML colors
paint.setColor(Color.parseColor("#CD5C5C"));
canvas.drawCircle(x / 2, y / 2, radius, paint); // <--- coordinates of type "float"
paint.setColor(Color.BLACK);
paint.setTextSize(40);
canvas.drawText("Hello", x/2, y/2, paint); // <--- coordinates of type "float"
}
}
}