下面我将为您详细讲解“Android仿360悬浮小球自定义view实现示例”的完整攻略,过程中将包含两个示例说明。
一、前置知识
在开始实现自定义view之前,需要掌握以下内容:
-
Android基础知识,如Activity、Fragment、View等的生命周期和使用方法。
-
自定义View的基本知识,如View的绘制、事件处理、属性设置等。
-
Android的动画知识,如属性动画、补间动画等。
-
熟练掌握Java编程语言。
二、实现自定义悬浮球
以下将介绍两种实现自定义悬浮球的方式。
- 实现方式一
第一步:创建一个自定义的View类,并重写onDraw方法进行悬浮球的绘制。
public class FloatBallView extends View {
private Paint mPaint;
private float mBallRadius = 30;
public FloatBallView(Context context) {
super(context);
init();
}
public FloatBallView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(mBallRadius, mBallRadius, mBallRadius, mPaint);
}
}
第二步:在Activity或Fragment中引入自定义View。
public class MainActivity extends AppCompatActivity {
private FloatBallView mFloatBallView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFloatBallView = findViewById(R.id.float_ball_view);
}
}
第三步:实现悬浮球拖拽功能
public class MainActivity extends AppCompatActivity {
private FloatBallView mFloatBallView;
private int lastX;
private int lastY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFloatBallView = findViewById(R.id.float_ball_view);
mFloatBallView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getRawX();
int y = (int) event.getRawY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = x;
lastY = y;
break;
case MotionEvent.ACTION_MOVE:
int offsetX = x - lastX;
int offsetY = y - lastY;
floatBallMove(offsetX, offsetY);
lastX = x;
lastY = y;
break;
}
return false;
}
});
}
private void floatBallMove(int offsetX, int offsetY) {
int left = mFloatBallView.getLeft() + offsetX;
int top = mFloatBallView.getTop() + offsetY;
int right = mFloatBallView.getRight() + offsetX;
int bottom = mFloatBallView.getBottom() + offsetY;
mFloatBallView.layout(left, top, right, bottom);
}
}
- 实现方式二
使用WindowManager实现悬浮球
public class FloatWindowManager {
private static final String TAG = "FloatWindowManager";
private static volatile FloatWindowManager sInstance;
private WindowManager mWindowManager;
private WindowManager.LayoutParams mLayoutParams;
private FloatBallView mFloatBallView;
private int mWidth;
private int mHeight;
private FloatWindowManager(Context context) {
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
mLayoutParams = new WindowManager.LayoutParams();
mLayoutParams.format = PixelFormat.TRANSPARENT;
mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
mLayoutParams.gravity = Gravity.START | Gravity.TOP;
mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
mFloatBallView = new FloatBallView(context);
mFloatBallView.setOnTouchListener(new View.OnTouchListener() {
private int lastX;
private int lastY;
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getRawX();
int y = (int) event.getRawY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = x;
lastY = y;
break;
case MotionEvent.ACTION_MOVE:
int offsetX = x - lastX;
int offsetY = y - lastY;
windowMove(offsetX, offsetY);
lastX = x;
lastY = y;
break;
}
return false;
}
});
mFloatBallView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "点击了悬浮球", Toast.LENGTH_SHORT).show();
}
});
}
public static FloatWindowManager getInstance(Context context) {
if (sInstance == null) {
synchronized (FloatWindowManager.class) {
if (sInstance == null) {
sInstance = new FloatWindowManager(context);
}
}
}
return sInstance;
}
public void show() {
mWindowManager.addView(mFloatBallView, mLayoutParams);
}
public void hide() {
mWindowManager.removeView(mFloatBallView);
}
public void windowMove(int offsetX, int offsetY) {
mLayoutParams.x += offsetX;
mLayoutParams.y += offsetY;
if (Math.abs(mWidth - mLayoutParams.x) < mFloatBallView.getWidth() * 2
|| Math.abs(mLayoutParams.x) < mFloatBallView.getWidth() * 2) {
hide();
}
if (mLayoutParams.y < 0) {
mLayoutParams.y = 0;
}
if (mLayoutParams.y + mFloatBallView.getHeight() > getScreenHeight()) {
mLayoutParams.y = getScreenHeight() - mFloatBallView.getHeight();
}
mWindowManager.updateViewLayout(mFloatBallView, mLayoutParams);
}
private int getScreenWidth() {
return mWindowManager.getDefaultDisplay().getWidth();
}
private int getScreenHeight() {
return mWindowManager.getDefaultDisplay().getHeight();
}
}
以上就是关于“Android仿360悬浮小球自定义view实现示例”的完整攻略,希望能对您有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android仿360悬浮小球自定义view实现示例 - Python技术站