首先画一个空心的矩形框,代码如下:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView image = (ImageView) findViewById(R.id.image);
Bitmap bitmap = Bitmap.createBitmap(800, 480, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.BLUE);
Rect rect = new Rect(0, 0, 800, 480);
Paint paint= new Paint();
paint.setStyle(Style.STROKE);
paint.setColor(Color.RED);
paint.setStrokeWidth(2);//这个地方改线宽,单位是像素
canvas.drawRect(rect, paint);
image.setImageBitmap(bitmap);
}
}
这时遇到的问题是画矩形框的线宽的单位是什么?
答案是像素。答案来源一方面是自己试,另一方面是来自以下链接:
http://stackoverflow.com/questions/4789806/what-unit-of-measure-does-paint-setstrokewidth-use