我有一个拥有自己背景的ImageView。我试图在一个点画圆,但圆的位置不对。例如,我在0,0画圆,但它不在背景的左上角。我的代码有什么问题?
此外我需要位图大小与imageview相同但是当我在应用程序中实现imageView.getWidth或.getHeight来创建位图时,应用程序将崩溃!如何设置位图的大小与Imageview的大小相同?! 感谢
private void createBitMap() {
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitMap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); //creates bmp
bitMap = bitMap.copy(bitMap.getConfig(), true); //lets bmp to be mutable
Canvas canvas = new Canvas(bitMap); //draw a canvas in defined bmp
Paint paint = new Paint(); //define paint and paint color
paint.setColor(Color.RED);
paint.setStyle(Style.FILL_AND_STROKE);
//paint.setStrokeWidth(0.5f);
paint.setAntiAlias(true); //smooth edges
imageView.setImageBitmap(bitMap);
//changed set image resource to set image background resource
imageView.setBackgroundResource(R.drawable.map);
canvas.drawCircle(0, 0, 3, paint);
//invalidate to update bitmap in imageview
imageView.invalidate();
}
答案 0 :(得分:0)
希望您没有在xml布局中向imageView添加任何填充。 {你的代码看起来不错}
要拉伸图像以匹配您的imageView,请将此代码添加到布局文件的ImageView中:
android:scaleType="fitXY"
答案 1 :(得分:0)
试试这个......
private void createBitMap() {
ImageView imageView = (ImageView) findViewById(R.id.imageView);
// this gives a mutable bitmap. no need to mutate it again...
Bitmap bitMap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitMap); // draw a canvas in defined bmp
Paint paint = new Paint(); // define paint and paint color
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
// paint.setStrokeWidth(0.5f);
paint.setAntiAlias(true); // smooth edges
canvas.drawCircle(0, 0, 3, paint);
imageView.setImageBitmap(bitMap);
// changed set image resource to set image background resource
imageView.setBackgroundResource(R.drawable.map);
}