[英]rotate imageview around the it's center
i want to create an analog clock and i have a problem. 我想创建一个模拟时钟,但我遇到了问题。 i use 3 imageview for sec, min and hour hands.
我使用3 imageview的秒,分钟和时针。 and now i want to each of them rotate around center but i can't.
现在我想让它们每个绕中心旋转,但我不能。 how can i rotate an imageview by giving the angle around the center?
如何通过围绕中心指定角度来旋转imageview?
Assuming you're targeting API 11, the easiest way is: 假设您要定位API 11,最简单的方法是:
view.setPivotX(view.getWidth() / 2);
view.setPivotY(view.getHeight() / 2);
float rotation = //some value between 0f and 360f
view.setRotation(rotation);
The View class has a .setRoatation()
method that would work perfectly. View类具有一个
.setRoatation()
方法,该方法可以完美运行。 Just pass it a number of degrees to rotate and it will, by default, rotate around the center. 只需将其旋转若干度,默认情况下它将绕中心旋转。
EDIT: 编辑:
For rotating images in API levels below 11, see this post. 要旋转API级别低于11的图像,请参阅此帖子。
Below is the code for rotating textview. 下面是旋转textview的代码。 it works API 8 as well.
它也可以使用API 8。 It is just customized text view you need to create the object of it.
它只是您需要创建其对象的自定义文本视图。 And need to set rotation angel (also translation too if needed.)
并且需要设置旋转角度(如果需要,也可以翻译)。
public class VerticalTextView extends ImageView { final boolean topDown=true; 公共类VerticalTextView扩展了ImageView {final boolean topDown = true;
float iRotateAngel, iSetX, iSetY;
int iIndex;
Context context;
public int getiIndex()
{
return iIndex;
}
public void setiIndex(int iIndex)
{
this.iIndex = iIndex;
}
public float getiRotateAngel()
{
return iRotateAngel;
}
public void setiRotateAngel(float iRotateAngel)
{
this.iRotateAngel = iRotateAngel;
}
public float getiSetX()
{
return iSetX;
}
public void setiSetX(float iSetX)
{
this.iSetX = iSetX;
}
public float getiSetY()
{
return iSetY;
}
public void setiSetY(float iSetY)
{
this.iSetY = iSetY;
}
public boolean isTopDown()
{
return topDown;
}
public VerticalTextView(Context context)
{
super(context);
this.context = context;
}
public VerticalTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
@Override
protected boolean setFrame(int l, int t, int r, int b)
{
return super.setFrame(l+32, t+12, l + (b - t)+32, t + (r - l)+12);
}
@Override
public void draw(Canvas canvas)
{
if (topDown)
{
canvas.translate(this.iSetX, this.iSetY);
canvas.rotate(this.iRotateAngel);
}
else
{
canvas.translate(this.iSetX, this.iSetY);
canvas.rotate(this.iRotateAngel);
}
canvas.clipRect(0,0,getWidth(), getHeight(), android.graphics.Region.Op.REPLACE);
super.draw(canvas);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.