gpt4 book ai didi

android - Android屏幕上的 float View

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:59:21 27 4
gpt4 key购买 nike

我试图制作一个用户可以在屏幕上拖动的 float View 。

这个想法是启动一个服务,然后在屏幕上膨胀一个 View 。

但是有个问题,它不是取属于自己的事件,而是取所有用户输入的事件。

这是我的代码: list .xml:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.floatandroidpractice"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.floatandroidpractice.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service android:name="com.example.floatandroidpractice.WalkingIconService" />
</application>

</manifest>

可拖动的 float View :

package com.example.floatandroidpractice;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;

public class littleIconView extends View {
private float viewX;
private float viewY;
private Paint mPaint;
private Bitmap androidIcon;

public littleIconView(Context context) {
super(context);
mPaint = new Paint();
mPaint.setColor(Color.BLACK);
androidIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

}

@Override
public void onDraw(Canvas cvs) {
cvs.drawBitmap(androidIcon, viewX - androidIcon.getWidth() / 2, viewY - androidIcon.getHeight()
/ 2, mPaint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
boolean touchedX = Math.abs(viewX - event.getX()) > androidIcon.getWidth();
boolean touchedY = Math.abs(viewY - event.getY()) > androidIcon.getHeight();
boolean isValidTouch = !touchedX && !touchedY;
if (isValidTouch) {
if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE
|| event.getAction() == MotionEvent.ACTION_UP) {
viewX = event.getX();
viewY = event.getY();
}
invalidate();
return true;

}
else
return false;
}
}

和服务:

package com.example.floatandroidpractice;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

public class WalkingIconService extends Service {

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

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
littleIconView a = new littleIconView(getApplicationContext());
LayoutParams mLayoutParams = new WindowManager.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0, PixelFormat.TRANSPARENT);
WindowManager mWindowManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
mLayoutParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE
| LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
LayoutParams.FLAG_LAYOUT_INSET_DECOR | LayoutParams.FLAG_LAYOUT_IN_SCREEN;
mWindowManager.addView(a, mLayoutParams);
return super.onStartCommand(intent, flags, startId);
}

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

}

完整项目:https://github.com/shanwu/shanwu_coding_base/tree/xxx/floatAndroidPractice

最佳答案

我尝试了您在 Canvas 上绘制图像的方法,但没有成功。一个例子,EatHeat's github ,我从哪里学到的。也许这能让你朝着正确的方向前进,也许不会,但它对我有用。

WalkingIconService.java

package ...
//imports

public class WalkingIconService extends Service {
private WindowManager mWindowManager;
private ImageView image;

public void onCreate() {
super.onCreate();
image = new ImageView(this);

image.setImageResource(R.drawable.ic_launcher);

mWindowManager = (WindowManager)getSystemService(WINDOW_SERVICE);

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

paramsF.gravity = Gravity.TOP | Gravity.LEFT;
paramsF.x=0;
paramsF.y=100;
mWindowManager.addView(image, paramsF);

try{

image.setOnTouchListener(new View.OnTouchListener() {
WindowManager.LayoutParams paramsT = paramsF;
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 = paramsF.x;
initialY = paramsF.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
paramsF.x = initialX + (int) (event.getRawX() - initialTouchX);
paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
mWindowManager.updateViewLayout(v, paramsF);
break;
}
return false;
}
});
} catch (Exception e){
e.printStackTrace();
}
}

@Overrride
public void onDestroy(){
super.onDestory();
}

}

主 Activity .java

package ...
//imports

public class MainActivity extends Activity {

@Override
public void onCreate(icicle) {
super.onCreate(icicle);
setcontentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.tv);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//toast
startService(new Intent(MainActivity.this, WalkingIconService.class));
}
});
//stopService (from my original code)
Button stop = (Button)findViewById.(R.id.btnStop);
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stopService(new Intent(MainActivity.this, WalkingIconService.class));
}
});
}
}

关于android - Android屏幕上的 float View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24748442/

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