在父視圖調整大小後,我在避免在子視圖放置期間自定義ViewGroup
期間發生不需要的轉換時遇到了一些問題。此問題似乎只在使用WindowManager
而不是setContentView()
時纔會出現。下面是一個簡單的例子:Android View調整大小和WindowManager:在子視圖放置期間發生不必要的轉換
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的大小。
我實現了在CustomViewGroup
的onClick
和onMeasure
方法,使在ACTION_DOWN
事件的情況下CustomViewGroup
規模擴大到整個顯示器。如果發生ACTION_UP
事件,大小將恢復爲默認值。在CustomViewGroup
內FloatingActionButton
的位置始終是
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
有內ViewRootImpl
(線5415)和該方法的執行過程中進行不希望的可見過渡到
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);
的呼叫。如果我從主活動中的xml中充值CustomViewGroup
,則在觸摸事件的情況下不會調用上述方法,並且由於在此情況下過渡不可見,我猜我可以考慮mWindowSession
數據成員的relayout
方法(類型是IWindowSession
)其負責。