在中间的Andr​​oid配售位图 [英] android placing bitmap in middle

查看:131
本文介绍了在中间的Andr​​oid配售位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计一个绘图应用,其中用户可以从库中导入图像,然后放大或缩小以适应屏幕宽度或高度。使用户可以画上导入的图片后,使用下面的code。

我延长查看,名为 DrawView 。在 DrawView 是相同屏幕宽度,但其高度小于screenheight因为有一些按钮它上面,放置DrawView至屏幕的底部下的运作的按钮,并所以我宣布 DrawViewHeight

请参阅以下的变量尺寸实例和结果。

问:

位图可以正确地加载和缩放到适合的 DrawView

然而,它位于 DrawView 的顶部。我想将其放置在屏幕的中间,所以我增加了以下code,但仍然失败。

  bitmapCanvas.drawBitmap(位图,x_adjustment,y_adjustment,NULL);

怎么可能被进一步修改,以便在导入图片(德codeD和复制为位图导入时)为放在中心 DrawView ,用空格以上(如白色)及以下和左,加载的缩放位图图像?

注意:在图像周围那些周围的空间都不会被用户所绘制到

codeS:

声明:

 私人位图位图; //绘图区域显示或保存
私人帆布bitmapCanvas; //用于绘制位图

OnSizeChanged:

 公共无效onSizeChanged(INT W,INT小时,INT oldW,诠释oldH)
   {
      super.onSizeChanged(W,H,oldW,oldH);
      DrawViewWidth = W;
      DrawViewHeight = H;      位图= Bitmap.createBitmap(的getWidth(),DrawViewHeight,Bitmap.Config.ARGB_8888);
      bitmapCanvas =新的Canvas(位图);
      bitmap.eraseColor(Color.WHITE); //删除白色位图
   }

的OnDraw:

  @覆盖
   保护无效的onDraw(帆布油画)//每次调用该视图绘制
   {
      canvas.drawBitmap(位图,0,0,paintScreen);
   }

加载图片:

 公共无效load_pic(最后弦乐picturePath)
   {//获取屏幕尺寸第一
窗口管理WM =(窗口管理器)context_new.getSystemService(Context.WINDOW_SERVICE);
显示显示= wm.getDefaultDisplay();
最终诠释屏幕宽度= display.getWidth();
最终诠释screenHeight = display.getHeight();//获取导入位图尺寸
选项​​OP =新的选项();
op.inJustDe codeBounds = TRUE;
位图pic_to_be_imported = BitmapFactory.de codeFILE(picturePath,OP);
最终诠释x_pic = op.outWidth;
最终诠释y_pic = op.outHeight;//缩放
最终诠释IMAGE_MAX_SIZE =(int)的Math.max(DrawViewWidth,DrawViewHeight);int标= 1;
如果(op.outHeight> IMAGE_MAX_SIZE || op.outWidth> IMAGE_MAX_SIZE){
比例=(int)的Math.pow(2,(int)的Math.round(将Math.log(IMAGE_MAX_SIZE /(双)Math.max(op.outHeight,op.outWidth))/将Math.log(0.5))); }最后BitmapFactory.Options O2 =新BitmapFactory.Options();
o2.inSampleSize =规模;//开始加载图片到DrawView如果((x_pic> DrawViewWidth)||(y_pic> DrawViewHeight))
{AlertDialog.Builder onBackBuilder =新AlertDialog.Builder(context_new);onBackBuilder.setPositiveButton(R.string.buttontext_create_load_pic_stretch,新DialogInterface.OnClickListener()
    {
        //跳过
    }
onBackBuilder.setNegativeButton(R.string.buttontext_create_load_pic_keep_scale,新DialogInterface.OnClickListener()
{
    公共无效的onClick(DialogInterface对话框,INT ID)
    {    浮动xratio =(浮点)x_pic /(浮点)屏幕宽度;
    浮动yratio =(浮点)y_​​pic /(浮点)DrawViewHeight;
    INT adjusted_x = 0;
    INT adjusted_y = 0;    如果(xratio> = yratio){adjusted_x =屏幕宽度; adjusted_y =(int)的(y_pic / xratio);}
    如果(xratio< yratio){adjusted_y = DrawViewHeight; adjusted_x =(int)的(x_pic / yratio);}    位图=(BitmapFactory.de codeFILE(picturePath,O2));
    位= bitmap.copy(Bitmap.Config.ARGB_8888,真);
    位= Bitmap.createScaledBitmap(位图,adjusted_x,adjusted_y,真正的);    INT x_adjustment =(屏幕宽度 - adjusted_x)/ 2;
    INT y_adjustment =(DrawViewHeight -adjusted_y)/ 2;    bitmapCanvas.drawBitmap(位图,x_adjustment,y_adjustment,NULL);    //如何修改派上DrawView中心????    无效();
    }
});AlertDialog警报= onBackBuilder.create();
alert.show();
}

变量

和尺寸的实施例的结果:

 屏幕:480W * 800H
DrawView:480W * 590H
原图:326​​4W * 2448H
缩放后的图像:adjusted_x = 480(相遇屏幕宽度),adjusted_y = 360
x_adjustment:0
Y型调整:115

Layout.xml

 <?XML版本=1.0编码=UTF-8&GT?;< RelativeLayout的
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>< com.pearmak.drawing.DrawView
            的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:ID =@ + ID / drawView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_centerInParent =真
            机器人:layout_gravity =center_vertical | CENTER_HORIZONTAL |中心
            机器人:背景=@绘制/ drawview_border/>< / RelativeLayout的>


解决方案

在你的的onDraw()指定(0,0)位图(X,Y),这将在左上角画它你的 DrawView 所以你的 x_adjustment,y_adjustment 效果迷失了的onDraw 被调用。

您应该做你的位图位置调整code里面的的onDraw 未在的onClick

  @覆盖
保护无效的onDraw(帆布油画)//每次调用该视图绘制
{
  canvas.drawBitmap(位图,x_adjustment,y_adjustment,NULL);
}

I am designing a drawing app where user can import image from gallery and then scale up or down to fit the screen width or height. so that user can draw onto the imported picture, using the below code.

I am extending View, named DrawView. The DrawView is same as screenwidth, but its height is less than screenheight because there are some buttons above it, placing the DrawView to the bottom of the Screen under the functioning buttons, and so I declared DrawViewHeight.

See below for examples for dimension and results of variables.

Question:

The bitmap can be properly loaded and scaled to fit to the DrawView.

However, it is located at the top of the DrawView. I would like to place it in the middle of the screen, so i added the following code but still FAILS.

bitmapCanvas.drawBitmap(bitmap, x_adjustment, y_adjustment, null);

How could it be further modified such that the imported image (decoded and copied as bitmap while importing) is placed center of DrawView, with blank space (eg. white) above and below and left and right of the loaded scaled bitmap image?

Note: Those surrounding space around the image are not to be drawn onto by user.

Codes:

Declarations:

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);       
   } 

Load Pictures:

   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();          
}

Examples of dimension and results of variables:

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

Layout.xml

<?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> 

解决方案

In your onDraw() you specify (0,0) as the bitmap (x,y) which will draw it at the top left corner of your DrawView so your x_adjustment, y_adjustment effect is lost once onDraw is called.

You should do your bitmap location adjustment code inside onDraw not inside onClick

@Override
protected void onDraw(Canvas canvas)  // called each time this View is drawn 
{
  canvas.drawBitmap(bitmap, x_adjustment, y_adjustment, null);       
} 

这篇关于在中间的Andr​​oid配售位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆