2013年11月20日 星期三

Scale the view contains stroke with anti-aliasing

How to filter anti-aliasing when we scale the view contains stroke?

Situation:
We have a view which contains stroke on portrait mode and the size of the view is 1000*1000.
On landscape mode, the size will change to 500*500.
According to this requirement, we need to scaling this view includes a canvas.
Remember that we don't change the view size on landscape mode, just scale it.
That is , the size of the view is 1000*1000 on portrait and landscape mode.

First, we calculate the scale rate.
Because of the different size on different mode, we can get the rate:
500/1000 = 0.5

rate = 0.5;
setScaleX(rate);
setScaleY(rate);
canvas.scale(rate, rate);

In order to the scaling action, the position of the view and event are incorrect.
Thus, we need to correct them.

Use these two function to modify the position.
setTranslationX(translationX);
setTranslationY(translationY);

Modify the event for down, move and up.
event.getX() / rate
event.getY() / rate

Finally,
invalidate();

Of cause, when we turn to portrait mode and we need to reset all of them.

 if (orientation == Configuration.ORIENTATION_PORTRAIT) {
      rate = (float) 1.0;
      setScaleX(rate);
      setScaleY(rate);
      if (this.getX() != 0) {
          setTranslationX(0);
      }
      if (this.getY() != 0) {
          setTranslationY(0);
      }
      canvas.scale(rate, rate);
      invalidate();
 }


沒有留言:

張貼留言