2017-12-04 4 views
0

男の画像を推測します。 (&の脚を下にしてください):P身長を動的に設定して画像を切り取る

layoutparamを使用して高さを動的に設定しています。

Iインスタンス300のために高さを設定し、

なお、画像の脚に向かって300のユニットにヘッドからの高さを設定します。

私が望むのは、高さをLEGから上位300単位に設定することです。

imageView.setHeight() 
+0

あなたが指定した(X、Y)とビットマップをトリミングする必要があります。 [This](https://stackoverflow.com/questions/3725501/how-to-crop-the-parsed-image-in-android)を参照してください。 – ADM

+0

高さを0(下)〜100(上)に設定できますか? 0高さから100%高さまでの画像表示が好きです。 可能ですか? –

+0

高さは同じです(300または任意のもの)。座標は変化します(X、Y)。 – ADM

答えて

1

使用して、Javaクラス内でこのメソッド:今

private void scaleImage(ImageView view) throws NoSuchElementException { 
    // Get bitmap from the the ImageView. 
    Bitmap bitmap = null; 

    try { 
     Drawable drawing = view.getDrawable(); 
     bitmap = ((BitmapDrawable) drawing).getBitmap(); 
    } catch (NullPointerException e) { 
     throw new NoSuchElementException("No drawable on given view"); 
    } catch (ClassCastException e) { 
     // Check bitmap is Ion drawable 
//   bitmap = Ion.with(view).getBitmap(); 
    } 

    // Get current dimensions AND the desired bounding box 
    int width = 0; 

    try { 
     width = bitmap.getWidth(); 
    } catch (NullPointerException e) { 
     throw new NoSuchElementException("Can't find bitmap on given view/drawable"); 
    } 

    int height = bitmap.getHeight(); 
    int bounding = dpToPx(150);// set height 
    Logger.i("Test", "original width = " + Integer.toString(width)); 
    Logger.i("Test", "original height = " + Integer.toString(height)); 
    Logger.i("Test", "bounding = " + Integer.toString(bounding)); 

    // Determine how much to scale: the dimension requiring less scaling is 
    // closer to the its side. This way the image always stays inside your 
    // bounding box AND either x/y axis touches it. 
    float xScale = ((float) bounding)/width; 
    float yScale = ((float) bounding)/height; 
    float scale = (xScale <= yScale) ? xScale : yScale; 
    Logger.i("Test", "xScale = " + Float.toString(xScale)); 
    Logger.i("Test", "yScale = " + Float.toString(yScale)); 
    Logger.i("Test", "scale = " + Float.toString(scale)); 

    // Create a matrix for the scaling and add the scaling data 
    Matrix matrix = new Matrix(); 
    matrix.postScale(scale, scale); 

    // Create a new bitmap and convert it to a format understood by the ImageView 
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); 
    width = scaledBitmap.getWidth(); // re-use 
    height = scaledBitmap.getHeight(); // re-use 
    BitmapDrawable result = new BitmapDrawable(scaledBitmap); 
    Logger.i("Test", "scaled width = " + Integer.toString(width)); 
    Logger.i("Test", "scaled height = " + Integer.toString(height)); 

    // Apply the scaled bitmap 
    view.setImageDrawable(result); 

    // Now change ImageView's dimensions to match the scaled image 
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams(); 
    params.width = width; 
    params.height = height; 
    view.setLayoutParams(params); 

    Logger.i("Test", "done"); 
    } 

    private int dpToPx(int dp) { 
    float density = getApplicationContext().getResources().getDisplayMetrics().density; 
    return Math.round((float) dp * density); 
    } 

、このmethordを呼び出す方法:

 Bitmap bitmap; 

      try { 
       bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage); 
       imageView.setImageBitmap(bitmap); 
       scaleImage(imageView);// call your imageview 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
関連する問題