net*_*ein 6 android views android-animation
我有一个项目,我有两个挑战:
第一:
为此,我发现的最佳方法是.layout()在视图上使用方法.
第二:
有没有更好的方法在Android上移动视图?
使用该方法可能有什么缺点.layout()?
public void layout (int l, int t, int r, int b)
Since: API Level 1
Assign a size and position to a view and all of its descendants
Parameters:
l Left position, relative to parent
t Top position, relative to parent
r Right position, relative to parent
b Bottom position, relative to parent
Run Code Online (Sandbox Code Playgroud)
提前致谢.
WindowManager LayoutParams为每个视图维护至少两个类的实例,除了它View本身之外的那个实例.
检查updateViewLayout方法WindowManager,特别是这部分:
view.setLayoutParams(wparams);
synchronized (this) {
int index = findViewLocked(view, true);
ViewRoot root = mRoots[index];
mParams[index] = wparams;
root.setLayoutParams(wparams, false);
}
Run Code Online (Sandbox Code Playgroud)
我相信你可以通过layout直接打电话弄得一团糟.请WindowManager.updateViewLayout改用.它会更慢,但更安全(只是IMO).
UPDATE
[来自:https://stackoverflow.com/a/11188273/327011 ]
WindowManager windowsManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)
WindowManager.LayoutParams windowParams = new WindowManager.LayoutParams();
windowParams.x = <new X coord>;
windowParams.y = <new Y coord>
windowParams.height = myImageView.getHeight();
windowParams.width = myImageView.getWidth();
windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
windowParams.format = PixelFormat.TRANSLUCENT;
windowParams.windowAnimations = 0;
windowManager.updateViewLayout(myImageView, windowParams);
Run Code Online (Sandbox Code Playgroud)
用手指移动视图的最简单方法是使用 View 类中的 setTranslationX 和 setTranslationY。
http://developer.android.com/reference/android/view/View.html#setTranslationX(float )
如果您的目标是 api < 11,您还可以更改 View LayoutParams 上的边距以移动。
我相信在 onLayout 或 onDraw 函数上移动项目更糟糕,因为视图已经具有上面的这些选项,并且随着您添加更多项目和不同视图,手动绘制视图可能会变得复杂。
| 归档时间: |
|
| 查看次数: |
19856 次 |
| 最近记录: |