动画ImageView以重新隐藏由于centerCrop引起的隐藏部分

时间:2014-05-04 19:55:30

标签: android android-imageview android-animation

这是我的简单布局

<RelativeLayout
        android:id="@+id/layout"
        android:layout_width="100dp"
        android:layout_height="100dp"
       <ImageView
            android:id="@+id/Image"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"                            
            android:scaleType="centerCrop"                
            />
 </RelativeLayout>

我想在Layout / ImageView上应用缩放动画(我不知道在哪个?)来显示由于scaleType centerCrop而裁剪的Image的隐藏部分。

我尝试过简单的缩放动画,但它会拉伸图像本身。

1 个答案:

答案 0 :(得分:0)

试试这个自定义动画:

class MatrixAnimation extends Animation {
    private float[][] v = new float[3][9];
    private ImageView target;

    public MatrixAnimation(ImageView target, Matrix m0, Matrix m1) {
        this.target = target;
        m0.getValues(v[0]);
        m1.getValues(v[1]);
        setDuration(2000);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        Matrix m = target.getImageMatrix();
        for (int i = 0; i < 9; i++) {
            v[2][i] = v[0][i] + (v[1][i] - v[0][i]) * interpolatedTime;
        }
        m.setValues(v[2]);
        target.setImageMatrix(m);
    }
}

开始代码:

    final ImageView iv = new ImageView(this);
    iv.setImageResource(R.drawable.layer0);
    iv.setScaleType(ScaleType.CENTER_CROP);
    LayoutParams p = new LayoutParams(LayoutParams.MATCH_PARENT, 200);
    setContentView(iv, p);

    OnClickListener l = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Matrix m0 = iv.getImageMatrix();
            Matrix m1 = new Matrix();
            Drawable d = iv.getDrawable();
            RectF src = new RectF(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
            RectF dst = new RectF(0, 0, iv.getWidth(), iv.getHeight());
            m1.setRectToRect(src, dst, ScaleToFit.CENTER);

            iv.setScaleType(ScaleType.MATRIX);

            MatrixAnimation anim = new MatrixAnimation(iv, m0, m1);
            iv.startAnimation(anim);
        }
    };
    iv.setOnClickListener(l);