0

我现在正在制作一个绘图应用程序,用户可以在其中从图库中导入图片并进一步绘图。图片可以加载成功,东西可以绘制成功,但是我想进一步修改,因为当导入的文件尺寸大于屏幕尺寸时,它只会显示部分图像。

因此,我加载屏幕尺寸和导入文件尺寸,如果某些条件,它将弹出警报对话框,以便用户可以选择拉伸或通过填充最大宽度或高度来导入该图片,保持宽度:高度比例.

对于加载图片,它是在ActivityA调用一个方法ActivityB (name as DrawView).

问题:

alertdialog 强调The constructor AlertDialog.Builder(DrawView) is undefined了短语中的错误

new AlertDialog.Builder(this);  

我尝试将以上内容修改为

new AlertDialog.Builder(DrawView.this);  

但仍然失败。这怎么可能被修改?非常感谢!!!

编码:

   public void load_pic(String picturePath) // load a picture from gallery
   {   
       // get screen dimension first
       WindowManager wm = (WindowManager) context_new.getSystemService(Context.WINDOW_SERVICE);
       Display display = wm.getDefaultDisplay();
       int screenWidth = display.getWidth();
       int screenHeight = display.getHeight(); 

       Options op = new Options();
       op.inJustDecodeBounds = true;
       Bitmap pic_to_be_imported = BitmapFactory.decodeFile(picturePath, op);
       int x_pic_to_be_imported = op.outWidth;
       int y_pic_to_be_imported = op.outHeight;

       if ((x_pic_to_be_imported > screenWidth) || (y_pic_to_be_imported > screenHeight))
       {
              TextView myView2 = new TextView(context_new.getApplicationContext()); 
              myView2.setText(R.string.message_load_pic);
              myView2.setTextSize(15); 

              AlertDialog.Builder onBackBuilder = new AlertDialog.Builder(DoodleView.this);         
              onBackBuilder.setTitle(R.string.menuitem_on_load_pic);
              onBackBuilder.setView(myView2); 

              onBackBuilder.setCancelable(true);                   

              onBackBuilder.setPositiveButton(R.string.buttontext_create_load_pic_extend, new DialogInterface.OnClickListener() 
              {
                 public void onClick(DialogInterface dialog, int id) 
                    {   
                       //code
                    }            
              });

              onBackBuilder.setNegativeButton(R.string.buttontext_create_load_pic_keep_scale, new DialogInterface.OnClickListener() 
              {
                  public void onClick(DialogInterface dialog, int id) 
                    {                       
              bitmap = (BitmapFactory.decodeFile(picturePath)).copy(Bitmap.Config.ARGB_8888, true)
                          .createScaledBitmap(bitmap, screenWidth, screenHeight, true);
              bitmapCanvas = new Canvas(bitmap);
                    }             
              });

              AlertDialog alert = onBackBuilder.create();
              alert.show();            
       }

          // further codes
   }
4

1 回答 1

2

传递用于创建 AlertDialog 而不是非 Activity 类的 Activity 上下文:

AlertDialog.Builder onBackBuilder = new AlertDialog.Builder(context_new);

在 Activity 中创建对象时context_new传递给的 Activity 上下文在哪里DoodleView

于 2013-02-03T11:29:32.320 回答