我正在工作的一个子类的FrameLayout,应该是将所有的孩子旋转90度。我这样做是为了克服Android 2.1及以下版本中仅存在于风景中的摄像机限制,将活动放置在横向,但将我的摄像机覆盖放置在此framelayout覆盖中,以使其看起来像是纵向(
this is how Layar does it)到完成这个,我适应
Jeff Sharkey’s code旋转视图。我的问题是,我可以旋转Framelayout,但我不能调整它以匹配新的维度。所以在我的g1,而不是一个320×480纵向视图在480×320相机视图的风景,我得到一个320×320盒子在中间显示我的肖像视图与侧面切碎。
这里是我的代码到目前为止:
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维度,但还没有能够让它工作。
您可以提供任何帮助,非常感谢。