27

ImageView の高さがどれほど小さくても、画像の下部が常にビューの下部に固定されるように ImageView を配置しようとしています。ただし、どのスケール タイプも、私がやろうとしていることに適合していないようです。CenterCrop は近いですが、画像を中央に配置したくありません。CSS が絶対位置を処理する方法に似ています。

その理由は、ImageView の高さをアニメーション化する必要がありますが、画像の上部を「明らかに」しているように見せるためです。画像をトリミングして ImageView の高さをアニメーション化するこの方法を理解するのがこれを行う最も簡単な方法だと思いますが、誰かがより良い方法を知っていれば、正しい方向に向けられるのが大好きです。

どんな助けでも感謝します。

4

6 に答える 6

16

私は@Jpoliachikコードを使用しましたが、うまくいきました。時々戻ってきたので、いくつかの調整を行いgetWidthgetHeight問題0getMeasuredWidth解決getMeasuredHeightしました。

@Override
protected boolean setFrame(int l, int t, int r, int b) {
   if (getDrawable() == null)
       return super.setFrame(l, t, r, b);

   Matrix matrix = getImageMatrix();

   float scale;
   int viewWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
   int viewHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
   int drawableWidth = getDrawable().getIntrinsicWidth();
   int drawableHeight = getDrawable().getIntrinsicHeight();
   //Get the scale
   if (drawableWidth * viewHeight > drawableHeight * viewWidth) {
       scale = (float) viewHeight / (float) drawableHeight;
   } else {
       scale = (float) viewWidth / (float) drawableWidth;
   }

   //Define the rect to take image portion from
   RectF drawableRect = new RectF(0, drawableHeight - (viewHeight / scale), drawableWidth, drawableHeight);
   RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
   matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.FILL);

   setImageMatrix(matrix);

   return super.setFrame(l, t, r, b);
}
于 2014-09-14T18:40:13.013 に答える
-4

Imageview の Scaletype FIT_ENDを試しましたか?イメージの終わりを表示するための最良のオプションです。

于 2013-09-23T05:30:40.217 に答える