gpt4 book ai didi

Android WindowManager TYPE_SYSTEM_ALERT 触摸布局

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:54:33 25 4
gpt4 key购买 nike

我的应用程序在特定时间向用户显示一个按钮作为 TYPE_SYSTEM_ALERT,我试图让用户 move 它。触摸和 move Action 确实使用以下代码注册,但按钮不会 move 。我错过了什么?

    //create button
mbtRec = new Button(ACR.getContext());
mbtRec.setText(R.string.start_recording);
mbtRec.setTypeface(null, Typeface.BOLD);
mbtRec.setBackgroundColor(Color.RED); //mbtRec.setBackgroundResource(R.drawable.ic_launcher);
mbtRec.setOnClickListener(recButtonOnClickListener);
mbtRec.setOnTouchListener(recButtonOnTouchListener);
//create layout params
recButtonLayoutParams = new WindowManager.LayoutParams();
recButtonLayoutParams.flags = WindowManager.LayoutParams.FORMAT_CHANGED; //8
recButtonLayoutParams.format = PixelFormat.RGBA_8888; //PixelFormat.TRANSLUCENT
recButtonLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
recButtonLayoutParams.gravity = Gravity.TOP | Gravity.CENTER; //51
recButtonLayoutParams.width = WindowManager.LayoutParams.FILL_PARENT;
recButtonLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
paramWindowManager.addView(this.mbtRec, recButtonLayoutParams);


OnTouchListener recButtonOnTouchListener = new View.OnTouchListener() {
@TargetApi(Build.VERSION_CODES.FROYO)
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
Tools.Log("Drag", "Started Dragging");
break;
case MotionEvent.ACTION_UP:
Tools.Log("Drag", "Stopped Dragging");
break;
case MotionEvent.ACTION_MOVE:
Tools.Log("Drag", "Dragging");


recButtonLayoutParams.x =(int) event.getRawX();
recButtonLayoutParams.y= (int) event.getRawY();


mbtRec.setLayoutParams(recButtonLayoutParams);
mbtRec.invalidate();

break;
default:
break;
}
return false;
}
};

最佳答案

通过@yoah 的提示和 10 天的更多研究,我成功地得到了我想要的东西。我必须添加@pingpongboss ( https://github.com/pingpongboss/StandOut ) 对我的电子邮件的回复帮助很大。

这是我想出并在我的通话录音应用程序中实现的内容 ACR

private WindowManager recButtonWindowManager;
private Button recButton = null;
WindowManager.LayoutParams prms;
boolean touchconsumedbyMove = false;
int recButtonLastX;
int recButtonLastY;
int recButtonFirstX;
int recButtonFirstY;

OnTouchListener recButtonOnTouchListener = new View.OnTouchListener() {
@TargetApi(Build.VERSION_CODES.FROYO)
@Override
public boolean onTouch(View v, MotionEvent event) {
WindowManager.LayoutParams prm = getRecbuttonLayout();
int totalDeltaX = recButtonLastX - recButtonFirstX;
int totalDeltaY = recButtonLastY - recButtonFirstY;

switch(event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
recButtonLastX = (int) event.getRawX();
recButtonLastY = (int) event.getRawY();
recButtonFirstX = recButtonLastX;
recButtonFirstY = recButtonLastY;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
int deltaX = (int) event.getRawX() - recButtonLastX;
int deltaY = (int) event.getRawY() - recButtonLastY;
recButtonLastX = (int) event.getRawX();
recButtonLastY = (int) event.getRawY();
if (Math.abs(totalDeltaX) >= 5 || Math.abs(totalDeltaY) >= 5) {
if (event.getPointerCount() == 1) {
prm.x += deltaX;
prm.y += deltaY;
touchconsumedbyMove = true;
recButtonWindowManager.updateViewLayout(getManualRecButton(), prm);
}
else{
touchconsumedbyMove = false;
}
}else{
touchconsumedbyMove = false;
}
break;
default:
break;
}
return touchconsumedbyMove;
}
};
private WindowManager.LayoutParams getRecbuttonLayout() {
if (prms != null) {
return prms;
}
prms = new WindowManager.LayoutParams();
prms.format = PixelFormat.TRANSLUCENT;
prms.flags = LayoutParams.FORMAT_CHANGED; // 8
prms.type = LayoutParams.TYPE_SYSTEM_ALERT;
prms.gravity = Gravity.TOP | Gravity.CENTER;
prms.width = LayoutParams.WRAP_CONTENT;
prms.height = LayoutParams.WRAP_CONTENT;
// Tools.Log("getRecbuttonLayout", "return getRecbuttonLayout()");
return prms;
}

private Button getManualRecButton() {
if (recButton != null) {
return recButton;
}
recButton = new Button(ACR.getContext());
recButton.setText(R.string.start_recording);
recButton.setTypeface(null, Typeface.BOLD);
recButton.setGravity(Gravity.CENTER_VERTICAL);
recButton.setBackgroundColor(Color.parseColor("#65FF0000"));
recButton.setPadding(100, 30, 100, 30);
recButton.setOnClickListener(recButtonOnClickListener);
recButton.setOnTouchListener(recButtonOnTouchListener);
return recButton;
}

关于Android WindowManager TYPE_SYSTEM_ALERT 触摸布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16985371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com