我正在设计一个绘图应用,用户可以从图库中导入图像,然后向上或向下缩放以适应屏幕宽度或高度。这样用户就可以使用下面的代码绘制导入的图片。
我正在扩展View
,名为DrawView
。 DrawView
与screenwidth相同,但其高度小于screenheight,因为它上面有一些按钮,将DrawView放在功能按钮下的Screen底部,所以我声明了DrawViewHeight
。
请参阅下文,了解变量的维度和结果的示例。
可以正确加载和缩放位图以适合DrawView
。
但是,它位于DrawView
的顶部。我想将它放在屏幕中间,所以我添加了以下代码,但仍然是FAILS。
bitmapCanvas.drawBitmap(bitmap, x_adjustment, y_adjustment, null);
如何进一步修改,以便导入的图像(导入时解码并复制为位图)放置在DrawView
的中心,并带有空格(例如,白色)加载的缩放位图图像的上方和下方以及左侧和右侧?
注意:图像周围的空间不会被用户吸引。
声明:
private Bitmap bitmap; // drawing area for display or saving
private Canvas bitmapCanvas; // used to draw on bitmap
OnSizeChanged:
public void onSizeChanged(int w, int h, int oldW, int oldH)
{
super.onSizeChanged(w, h, oldW, oldH);
DrawViewWidth = w;
DrawViewHeight = h;
bitmap = Bitmap.createBitmap(getWidth(), DrawViewHeight, Bitmap.Config.ARGB_8888);
bitmapCanvas = new Canvas(bitmap);
bitmap.eraseColor(Color.WHITE); // erase the BitMap with white
}
的OnDraw:
@Override
protected void onDraw(Canvas canvas) // called each time this View is drawn
{
canvas.drawBitmap(bitmap, 0, 0, paintScreen);
}
加载图片:
public void load_pic(final String picturePath)
{
// get screen dimension first
WindowManager wm = (WindowManager) context_new.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
final int screenWidth = display.getWidth();
final int screenHeight = display.getHeight();
//get importing bitmap dimension
Options op = new Options();
op.inJustDecodeBounds = true;
Bitmap pic_to_be_imported = BitmapFactory.decodeFile(picturePath, op);
final int x_pic = op.outWidth;
final int y_pic = op.outHeight;
// scaling
final int IMAGE_MAX_SIZE= (int) Math.max(DrawViewWidth, DrawViewHeight);
int scale = 1;
if (op.outHeight > IMAGE_MAX_SIZE || op.outWidth > IMAGE_MAX_SIZE) {
scale = (int)Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(op.outHeight, op.outWidth)) / Math.log(0.5))); }
final BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
// Start loading image to the DrawView
if ((x_pic > DrawViewWidth) || (y_pic > DrawViewHeight))
{
AlertDialog.Builder onBackBuilder = new AlertDialog.Builder(context_new);
onBackBuilder.setPositiveButton(R.string.buttontext_create_load_pic_stretch, new DialogInterface.OnClickListener()
{
//skipped
}
onBackBuilder.setNegativeButton(R.string.buttontext_create_load_pic_keep_scale, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
float xratio = (float) x_pic / (float) screenWidth;
float yratio = (float) y_pic / (float) DrawViewHeight;
int adjusted_x = 0;
int adjusted_y = 0;
if (xratio >= yratio) {adjusted_x = screenWidth; adjusted_y = (int) (y_pic / xratio);}
if (xratio < yratio) {adjusted_y = DrawViewHeight; adjusted_x = (int) (x_pic / yratio);}
bitmap = (BitmapFactory.decodeFile(picturePath, o2));
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
bitmap = Bitmap.createScaledBitmap(bitmap, adjusted_x, adjusted_y, true);
int x_adjustment = (screenWidth - adjusted_x) /2;
int y_adjustment = (DrawViewHeight -adjusted_y) /2;
bitmapCanvas.drawBitmap(bitmap, x_adjustment, y_adjustment, null);
// How to modify to put to center of DrawView????
invalidate();
}
});
AlertDialog alert = onBackBuilder.create();
alert.show();
}
Screen : 480W * 800H
DrawView : 480W * 590H
Original image : 3264W * 2448H
Scaled image : adjusted_x=480 (meet screenwidth), adjusted_y=360
x_adjustment : 0
y-adjustment : 115
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.pearmak.drawing.DrawView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center_vertical|center_horizontal|center"
android:background="@drawable/drawview_border" />
</RelativeLayout>
答案 0 :(得分:2)
// You can try this :
int width = containerBitmap.getWidth();
int height = containerBitmap.getHeight();
float centerX = (width - centeredBitmap.getWidth()) * 0.5f;
float centerY = (height- centeredBitmap.getHeight()) * 0.5f;
mCanvas.drawBitmap(centeredBitmap, centerX, centerY, paint);
您可以使用它在另一个位图的中心绘制位图。
答案 1 :(得分:1)
在onDraw()
中,您指定(0,0)
作为bitmap
(x,y),它会在DrawView
的左上角绘制x_adjustment, y_adjustment
所以onDraw
调用bitmap
后效果会丢失。
您应该在onDraw
内的onClick
位置调整代码而不是@Override
protected void onDraw(Canvas canvas) // called each time this View is drawn
{
canvas.drawBitmap(bitmap, x_adjustment, y_adjustment, null);
}
{{1}}
答案 2 :(得分:0)
您的imageview的LayoutParameters
或imageview的父视图需要在屏幕中指定居中
另外,发布您的XML布局(如果有),以及您在代码中将位图作为imageview的一部分
答案 3 :(得分:0)
正如iTech所指出的那样,为了将您的Bitmap绘制在DrawView的中心,您需要在onDraw方法中的正确位置绘制它。
创建另外两个字段:
int x_adjust, y_adjust;
在onClick中(加载位图时),计算正确的中心,并将x_adjust和y_adjust设置为相关值。然后确保在onDraw方法中使用这些值(而不是0,0)。
@Override
protected void onDraw(Canvas canvas)
{
canvas.drawBitmap(bitmap, x_adjust, y_adjust, null);
}
注意:确保也在onSizeChanged方法中重新计算x_adjust和y_adjust。否则,在旋转设备后,位图将不再居中。