Mirror (flip) a View / ProgressBar

邮差的信 提交于 2021-02-08 13:16:19

问题


I have a custom built circular progress bar used for a seconds counter on a clock, which I'd like to flip, so that the clock is counting counter-clockwise.

Searching here for a solution, I found this:

Right to Left ProgressBar?

Which obviously works for a horizontal progress bar, but I can't simply rotate it 180 degrees, since that will just move the clock 180 degrees and it will still be ticking clockwise.

Is there any other way to mirror a ProgressBar, or any View?

Edit:

Just found "android:rotationY" and the programmatic equivalent, but this ideally needs to be for 2.2 and above..


回答1:


I was able to flip ProgressBar simply with attribute scaleX in XML:

android:scaleX="-1"

This works for other View as well.




回答2:


Extend ProgressBar in the same way you would for a horizontal progress bar, but in the onDraw method, flip the canvas rather than rotate it:

public class InverseProgressBar extends ProgressBar {

public InverseProgressBar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public InverseProgressBar(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public InverseProgressBar(Context context) {
    super(context);
}

@Override
protected synchronized void onDraw(Canvas canvas) {
    canvas.scale(-1f, 1f, super.getWidth() * 0.5f, super.getHeight() * 0.5f);
    super.onDraw(canvas);
}

}



回答3:


Yes. In newer versions of material design library, the circular progress bar can be mirrored:

  • in the layout with this attribute
    app:indicatorDirectionCircular
    
  • or programmatically with this method
    setIndicatorDirection()
    

Refer here for more info.



来源:https://stackoverflow.com/questions/14022298/mirror-flip-a-view-progressbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!