Reputation: 356
Here is my simple Layout
<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>
I want to apply scale animation on Layout/ImageView (I don't know on which?) to reveal hidden part of Image which are cropped due to scaleType centerCrop.
I have tried simple scale animation but it stretches the image itself.
Upvotes: 0
Views: 272
Reputation: 24740
try this custom Animation:
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);
}
}
starting code:
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);
Upvotes: 0