在框架布局上绘图 canvas
Drawing canvas on framelayout
我有一个带背景的框架布局 image.I 我正在使用 canvas 在此框架布局上画线。
问题是,在不同的屏幕尺寸上,线条不会出现在同一个地方。
我试过用屏幕密度 dpi 乘以坐标,但还是不行。
我的 canvas 代码片段
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.FILL);
bitmap = ((BitmapDrawable) zoom_lay.getBackground()).getBitmap();
mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
canvas = new Canvas(mutableBitmap);
canvas.drawColor(Color.TRANSPARENT);
zoom_lay.setBackground(new BitmapDrawable(getResources(), mutableBitmap));
final Path pathPolygon_2 = new Path();
pathPolygon_2.reset(); // only needed when reusing this path for a new build
canvas.drawLine(500,500,600,615,paint);
谢谢
首先需要得到Bitmap的大小,然后根据需要计算出直线的终点。比如你要在Bitmap中间画一条线,Y坐标就是这个Bitmap.getHeight() * 0.5f。希望能帮到你。
Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
int height = mutableBitmap.getHeight();
int width = mutableBitmap.getWidth();
float startX = width * 0f;
float startY = height * 0.5f;
float stopX = width * 1f;
float stopY = height * 0.5f;
canvas.drawLine(startX, startY, stopX, stopY, paint);
使用以下参数创建布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/background"
tools:context=".MainActivity">
可以是任何类型,FrameLayout只是一个例子。重要的部分是设置它的宽度和高度以匹配父级。
从那里你可以使用你的代码来获取位图并在上面绘制唯一的东西,假设你想在中心水平绘制线:
canvas.drawLine(0,muteableBitmap.getHeight()/2,muteableBitmap.getWidth(),muteableBitmap.getHeight()/2,paint);
假设你想要一条从中心垂直的线:
canvas.drawLine(muteable.getWidth()/2,0,muteableBitmap.getWidth()/2,muteableBitmap.getHeight,paint);
我手动写了这个,所以它可能由于大写字母等原因无法编译,但它应该可以工作
我有一个带背景的框架布局 image.I 我正在使用 canvas 在此框架布局上画线。 问题是,在不同的屏幕尺寸上,线条不会出现在同一个地方。
我试过用屏幕密度 dpi 乘以坐标,但还是不行。
我的 canvas 代码片段
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.FILL);
bitmap = ((BitmapDrawable) zoom_lay.getBackground()).getBitmap();
mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
canvas = new Canvas(mutableBitmap);
canvas.drawColor(Color.TRANSPARENT);
zoom_lay.setBackground(new BitmapDrawable(getResources(), mutableBitmap));
final Path pathPolygon_2 = new Path();
pathPolygon_2.reset(); // only needed when reusing this path for a new build
canvas.drawLine(500,500,600,615,paint);
谢谢
首先需要得到Bitmap的大小,然后根据需要计算出直线的终点。比如你要在Bitmap中间画一条线,Y坐标就是这个Bitmap.getHeight() * 0.5f。希望能帮到你。
Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
int height = mutableBitmap.getHeight();
int width = mutableBitmap.getWidth();
float startX = width * 0f;
float startY = height * 0.5f;
float stopX = width * 1f;
float stopY = height * 0.5f;
canvas.drawLine(startX, startY, stopX, stopY, paint);
使用以下参数创建布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/background"
tools:context=".MainActivity">
可以是任何类型,FrameLayout只是一个例子。重要的部分是设置它的宽度和高度以匹配父级。 从那里你可以使用你的代码来获取位图并在上面绘制唯一的东西,假设你想在中心水平绘制线:
canvas.drawLine(0,muteableBitmap.getHeight()/2,muteableBitmap.getWidth(),muteableBitmap.getHeight()/2,paint);
假设你想要一条从中心垂直的线:
canvas.drawLine(muteable.getWidth()/2,0,muteableBitmap.getWidth()/2,muteableBitmap.getHeight,paint);
我手动写了这个,所以它可能由于大写字母等原因无法编译,但它应该可以工作