我正在研究FrameLayout的子类,它应该将其所有子级旋转90度。我这样做是为了克服android 2.1及其以下版本中的风景相机限制,通过让活动处于横向模式,但将相机覆盖层放入此framelayout覆盖层,使其看起来好像是纵向(this is how Layar does it)。完成这个,我正在修改Jeff Sharkey's code来旋转视图。我的问题是我可以旋转Framelayout,但我无法调整它的大小来匹配新的尺寸。所以在我的g1上,取代了横向480x320相机视图上的320x480纵向视图,在中间显示了一个320x320的盒子,显示了我的肖像视图,并且两边被切掉了。旋转视图层次结构90度
这是到目前为止我的代码:
public class RotateLayout extends FrameLayout {
private Matrix mForward = new Matrix();
private Matrix mReverse = new Matrix();
private float[] mTemp = new float[2];
public RotateLayout(Context context) {
super(context);
}
public RotateLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
/* (non-Javadoc)
* @see android.widget.FrameLayout#onMeasure(int, int)
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//This didn't work:
//super.onMeasure(heightMeasureSpec, widthMeasureSpec);
}
/* (non-Javadoc)
* @see android.widget.FrameLayout#onSizeChanged(int, int, int, int)
*/
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void dispatchDraw(Canvas canvas) {
canvas.rotate(270, getWidth()/2, getHeight()/2);
//This code will stretch the canvas to accommodate the new screen size. This is not what I want.
//float scaleX=(float)getHeight()/getWidth();
//float scaleY=(float)getWidth()/getHeight();
//canvas.scale(scaleX, scaleY, getWidth()/2, getHeight()/2);
mForward = canvas.getMatrix();
mForward.invert(mReverse);
canvas.save();
canvas.setMatrix(mForward); //This is the matrix we need to use for proper positioning of touch events
super.dispatchDraw(canvas);
canvas.restore();
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
final float[] temp = mTemp;
temp[0] = event.getX();
temp[1] = event.getY();
mReverse.mapPoints(temp);
event.setLocation(temp[0], temp[1]);
return super.dispatchTouchEvent(event);
}
}
我试图重写OnMeasure
切换X和查看的Y尺寸,但一直没能得到那个工作。 任何帮助,您可以提供非常感谢。
嗨,你有没有解决这个问题? – fhucho 2010-09-05 13:41:55
不 - 最终的解决方案是按原样使用上述代码,并通过将各个按钮包装在其中之一中来单独旋转按钮,文本浏览等。这不是很优雅,但它的作品。 – QRohlf 2010-09-06 22:50:51
我相信'dispatchTouchEvent()'里面应该使用getRawX()和getRawY()来代替getX()和getY()。无论如何,这对我来说效果更好。我的观点是方形的,fww。 – 2011-04-27 23:21:40