Skip to content

Commit

Permalink
Clean up pivot math for scaling.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Jun 25, 2012
1 parent 04629aa commit da9dd5c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,9 @@ private void transformMatrix(Matrix m, View view) {
final float sY = mScaleY;
if ((sX != 1.0f) || (sY != 1.0f)) {
m.postScale(sX, sY);
m.postTranslate(-pX, -pY);
final float sPX = -(pX / w) * ((sX * w) - w);
final float sPY = -(pY / h) * ((sY * h) - h);
m.postTranslate(sPX, sPY);
}

m.postTranslate(mTranslationX, mTranslationY);
Expand Down
10 changes: 8 additions & 2 deletions sample/res/layout/toggles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,23 @@
android:layout_width="fill_parent"
android:orientation="horizontal">
<Button
android:id="@+id/pivot_corner"
android:id="@+id/pivot_zero_zero"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Pivot Corner"/>
android:text="Pivot (0,0)"/>
<Button
android:id="@+id/pivot_center"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Pivot Center"/>
<Button
android:id="@+id/pivot_width_height"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Pivot (w,h)"/>
</LinearLayout>
<LinearLayout
android:layout_height="0dp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void onClick(View v) {
ObjectAnimator.ofFloat(target, "rotation", 0, 180, 0).setDuration(duration).start();
}
});
findViewById(R.id.pivot_corner).setOnClickListener(new OnClickListener() {
findViewById(R.id.pivot_zero_zero).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ViewHelper.setPivotX(target, 0);
Expand All @@ -78,5 +78,12 @@ public void onClick(View v) {
ViewHelper.setPivotY(target, target.getHeight() / 2f);
}
});
findViewById(R.id.pivot_width_height).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ViewHelper.setPivotX(target, target.getWidth());
ViewHelper.setPivotY(target, target.getHeight());
}
});
}
}

0 comments on commit da9dd5c

Please sign in to comment.