gpt4 book ai didi

android - 聊天头点击事件

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:39 25 4
gpt4 key购买 nike

我使用在线教程创建了一个将 ImageButton 添加到窗口的简单应用程序。我所说的窗口是指 ImageButton 显示在其余 Activity 的上方。它总是可见的。

我不知道如何处理聊天头的点击事件。我设置了一个 onClickListener,但它没有做任何事情。

这是我的 ChatHeadService.java

package com.example.test;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;

public class ChatHeadService extends Service {

private WindowManager windowManager;
private ImageView chatHead;

@Override
public IBinder onBind(Intent intent) {
// Not used
return null;
}

@Override
public void onCreate() {
super.onCreate();

windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

chatHead = new ImageButton(this);
chatHead.setImageResource(R.drawable.ic_launcher);

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);

params.gravity = Gravity.CENTER;
params.x = 0;
params.y = 100;

windowManager.addView(chatHead, params);
chatHead.setClickable(true);
chatHead.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {


}
});

chatHead.setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;

@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_UP:
return true;
case MotionEvent.ACTION_MOVE:
params.x = initialX
+ (int) (event.getRawX() - initialTouchX);
params.y = initialY
+ (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(chatHead, params);
return true;
}
return false;
}
});
}

@Override
public void onDestroy() {
super.onDestroy();
if (chatHead != null)
windowManager.removeView(chatHead);
}
}

最佳答案

试试这个:

                   case MotionEvent.ACTION_UP:
if( (Math.abs(initialTouchX - event.getRawX())<5) && (Math.abs(initialTouchY - event.getRawY())<5) )
{
Log.e(TAG,"It's a click ! ");
}
else Log.e(TAG,"you moved the head");
return true;

关于android - 聊天头点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24063495/

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