1

ユーザーがギャラリーから画像をインポートしてから、画面の幅や高さに合わせて拡大または縮小できる描画アプリを設計しています。ユーザーが以下のコードを使用して、インポートされた画像に描画できるようにします。

私はView、という名前のを拡張していDrawViewます。これDrawViewはscreenwidthと同じですが、その上にいくつかのボタンがあり、機能しているボタンの下の画面の下部にDrawViewを配置しているため、その高さはscreenheightよりも低くなっていますDrawViewHeight

変数のディメンションと結果の例については、以下を参照してください。

質問:

ビットマップは、に合うように適切にロードおよびスケーリングできますDrawView

ただし、の上部にありDrawViewます。画面の中央に配置したいので、次のコードを追加しましたが、失敗します。

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

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

4 に答える 4

3
    // 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);

これを使用して、別のビットマップの中央にビットマップを描画できます。

于 2015-11-25T10:52:11.147 に答える
1

あなたはあなたの左上隅にそれを描く(x、y)としてonDraw()指定するので、一度呼び出されるとあなたの効果は失われます。(0,0)bitmapDrawViewx_adjustment, y_adjustmentonDraw

あなたは内部ではなく内部であなたのbitmap位置調整コードを行うべきですonDrawonClick

@Override
protected void onDraw(Canvas canvas)  // called each time this View is drawn 
{
  canvas.drawBitmap(bitmap, x_adjustment, y_adjustment, null);       
} 
于 2013-02-16T19:12:51.957 に答える
0

LayoutParametersimageviewまたはimageviewの親ビューのは、画面内の中央揃えを指定する必要があります

また、XMLレイアウトがある場合はそれを投稿し、コード内でイメージビューのビットマップ部分を作成する場所も投稿します。

于 2013-02-03T17:17:10.043 に答える
0

iTechが指摘しているように、ビットマップをDrawViewの中央に描画するには、onDrawメソッド内の正しい位置に描画する必要があります。

2つの追加フィールドを作成します。

int x_adjust, y_adjust;

onClick(ビットマップをロードするとき)で、正しい中心を計算し、x_adjustとy_adjustを関連する値に設定します。次に、これらの値を(0,0ではなく)onDrawメソッドで使用していることを確認してください。

@Override
protected void onDraw(Canvas canvas)
{
    canvas.drawBitmap(bitmap, x_adjust, y_adjust, null);       
}

注:onSizeChangedメソッドでもx_adjustとy_adjustを再計算してください。そうしないと、デバイスを回転させた後、ビットマップが中央に配置されなくなります。

于 2013-02-16T20:32:21.560 に答える