ViewGroup
在父视图调整大小后,我在避免在自定义中放置子视图期间出现不必要的转换时遇到了一些问题。这个问题似乎只在使用WindowManager
而不是调用时出现setContentView()
。这是一个简短的例子:
MainActivity
:
public class MainActivity extends AppCompatActivity {
private static final int MY_PERMISSIONS_DRAW_OVER_OTHER_APPS = 1;
CustomViewGroup mCustomViewGroup;
WindowManager mWindowManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, MY_PERMISSIONS_DRAW_OVER_OTHER_APPS);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
if (ContextCompat.checkSelfPermission(this,
android.Manifest.permission.SYSTEM_ALERT_WINDOW)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
android.Manifest.permission.SYSTEM_ALERT_WINDOW)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.SYSTEM_ALERT_WINDOW},
MY_PERMISSIONS_DRAW_OVER_OTHER_APPS);
}
}
}
mCustomViewGroup =
(CustomViewGroup) LayoutInflater.from(this).inflate(R.layout.activity_main,null);
final WindowManager.LayoutParams floatingViewLayoutParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
);
floatingViewLayoutParams.gravity = Gravity.BOTTOM | Gravity.LEFT;
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mCustomViewGroup,floatingViewLayoutParams);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_PERMISSIONS_DRAW_OVER_OTHER_APPS) {
//Check if the permission is granted or not.
if (resultCode == RESULT_OK) {
} else {
Toast.makeText(this,
"Draw over other app permission not available. Closing the application",
Toast.LENGTH_SHORT).show();
finish();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
CustomViewGroup
:
public class CustomViewGroup extends ViewGroup {
private boolean mIsTouchEventActive;
public CustomViewGroup(Context context) {
this(context,null,0);
}
public CustomViewGroup(Context context, AttributeSet attrs) {
this(context,attrs,0);
}
public CustomViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
mIsTouchEventActive = false;
setWillNotDraw(false);
// Add the fab view
LayoutInflater layoutInflater = LayoutInflater.from(context);
View fabView = layoutInflater.inflate(R.layout.fab_view,null,false);
this.addView(fabView);
}
@Override
public boolean shouldDelayChildPressedState() {
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mIsTouchEventActive = true;
requestLayout();
return true;
case MotionEvent.ACTION_UP:
mIsTouchEventActive = false;
requestLayout();
return true;
}
return false;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int maxHeight = 0;
int maxWidth = 0;
int childState = 0;
final View child = getChildAt(0);
if (child.getVisibility() != GONE) {
measureChild(child, widthMeasureSpec, heightMeasureSpec);
maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
childState = combineMeasuredStates(childState, child.getMeasuredState());
}
maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
if (mIsTouchEventActive == true) {
final Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point deviceDisplay = new Point();
display.getSize(deviceDisplay);
maxHeight = deviceDisplay.x;
maxWidth = deviceDisplay.y;
}
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
resolveSizeAndState(maxHeight, heightMeasureSpec, childState << MEASURED_HEIGHT_STATE_SHIFT));
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final Rect fabChildRect = new Rect();
final View child = getChildAt(0);
if (child.getVisibility() != GONE) {
final int width = child.getMeasuredWidth();
final int height = child.getMeasuredHeight();
fabChildRect.left = 0;
fabChildRect.right = width;
fabChildRect.top = 0;
fabChildRect.bottom = height;
child.layout(fabChildRect.left, fabChildRect.top,
fabChildRect.right, fabChildRect.bottom);
}
}
}
activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<packagename.CustomViewGroup
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_view_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"/>
fab_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.design.widget.FloatingActionButton
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:srcCompat="@android:drawable/ic_dialog_email"/>
</LinearLayout>
清单文件应该包含android.permission.SYSTEM_ALERT_WINDOW
权限,我的依赖列表build.gradle
如下:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:24.2.1'
testCompile 'junit:junit:4.12'
}
这是我正在做的事情:
我有一个自定义视图CustomViewGroup
继承自ViewGroup
,在我的简短示例中包含一个FloatingActionButton
, 放置在父视图的左下角。的默认大小是CustomViewGroup
包含的那些FloatingActionButton
:264x264。
我已经实现了onClick
和onMeasure
方法,CustomViewGroup
以便在发生ACTION_DOWN
事件时将大小CustomViewGroup
扩展到整个显示。如果发生ACTION_UP
事件,大小将恢复为默认值。FloatingActionButton
中的位置CustomViewGroup
总是
fabChildRect.left = 0;
fabChildRect.right = width;
fabChildRect.top = 0;
fabChildRect.bottom = height;
其中width
和height
都等于 264,因此当用户触摸后父视图大小发生变化时,按钮本身被放置在不同的位置。
我的问题是:在用户触摸事件的情况下,如何在操作按钮放置期间删除动画?我的意思是按钮视图的垂直运动。我已经尝试过disableTransitionType
使用所有转换类型的方法,但没有任何变化。如果我尝试禁用 xml 文件中的转换,则相同。如果CustomViewGroup
视图通过从 xml 膨胀而不是WindowManager
使用activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="packagename.MainActivity">
<packagename.CustomViewGroup
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_view_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"/>
</RelativeLayout>
你对如何解决我的问题有什么建议,也许还有我所观察到的解释?正如您可能从我的代码中猜到的那样,我是 Android 编程的初学者,因此任何与该线程主题不严格相关的一般建议都非常受欢迎!
谢谢你。
更新:我发现使用WindowManager
有一个调用
int relayoutResult = mWindowSession.relayout(
mWindow, mSeq, params,
(int) (mView.getMeasuredWidth() * appScale + 0.5f),
(int) (mView.getMeasuredHeight() * appScale + 0.5f),
viewVisibility,
insetsPending ? WindowManagerGlobal.RELAYOUT_INSETS_PENDING : 0,mWinFrame,
mPendingOverscanInsets, mPendingContentInsets, mPendingVisibleInsets,
mPendingStableInsets, mPendingOutsets, mPendingConfiguration, mSurface);
在ViewRootImpl
(第 5415 行)内,并且在执行此方法期间执行不需要的可见转换。如果我在主活动中从 xml 膨胀,CustomViewGroup
则在触摸事件的情况下不会调用上述方法,并且由于在这种情况下转换也不可见,我想我可以考虑数据成员的relayout
方法mWindowSession
(类型为IWindowSession
)它负责。