- 一、自定义悬浮移动View效果图
- 二、使用步骤
- 1.自定义SuspendRedBagView
- 2.使用SuspendRedBagView
代码如下(示例):
package com.walking.go2.widget; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Rect; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class SuspendRedBagView extends View { //父view的坐标位置 private int leftX; private int rightX; private int topY; private int bottomY; //移动的坐标位置 private int removeLeftX; private int removeRightX; private int removeTopY; private int removeBottomY; //true向右移动,false向左移动 private boolean horizontal = true; //true向下移动,false向上移动 private boolean vertical = true; //自定义矩形 private Rect rect; //移动速度 private int moveSpeed = 3; //矩形宽度 private int mWide = 140; //矩形高度 private int mHeight = 160; //矩形背景图片Bitmap private Bitmap bitmap; //矩形背景图片id private int resourceId; //是否悬浮移动 private boolean isSuspend = false; public void setMoveSpeed(int moveSpeed) { this.moveSpeed = moveSpeed; } public void setmWide(int mWide) { this.mWide = mWide; } public void setmHeight(int mHeight) { this.mHeight = mHeight; } public void setResourceId(int resourceId) { this.resourceId = resourceId; } public void setSuspend(boolean suspend) { isSuspend = suspend; } public SuspendRedBagView(Context context) { super(context); } public SuspendRedBagView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); leftX = left; rightX = right; topY = top; bottomY = bottom; removeLeftX = (right / 2) - (mWide / 2); removeRightX = (right / 2) + (mWide / 2); removeTopY = 0; removeBottomY = mHeight; bitmap = BitmapFactory.decodeResource(getResources(), resourceId); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (isSuspend) { if (rect == null) { rect = new Rect(); } rect.set(removeLeftX, removeTopY, removeRightX, removeBottomY); canvas.drawBitmap(bitmap, null, rect, null); if (removeLeftX <= leftX) { horizontal = true; } else if (removeRightX >= rightX) { horizontal = false; } changeX(horizontal); if (removeTopY <= topY) { vertical = true; } else if (removeBottomY >= bottomY) { vertical = false; } changeY(vertical); invalidate(); } } private void changeX(boolean horizontal) { if (horizontal) { removeLeftX = removeLeftX + moveSpeed; removeRightX = removeRightX + moveSpeed; } else { removeLeftX = removeLeftX - moveSpeed; removeRightX = removeRightX - moveSpeed; } } private void changeY(boolean vertical) { if (vertical) { removeTopY = removeTopY + moveSpeed; removeBottomY = removeBottomY + moveSpeed; } else { removeTopY = removeTopY - moveSpeed; removeBottomY = removeBottomY - moveSpeed; } } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { if (rect != null) { if (rect.contains((int) event.getX(), (int) event.getY())) { if (onRedBagClickListener != null) { onRedBagClickListener.onRedBagClick(); return true; } } } } return false; } private onRedBagClickListener onRedBagClickListener; public void setOnRedBagClickListener(SuspendRedBagView.onRedBagClickListener onRedBagClickListener) { this.onRedBagClickListener = onRedBagClickListener; } public interface onRedBagClickListener { void onRedBagClick(); } }2.使用SuspendRedBagView
代码如下(示例):
//设置宽度 suspendRedBagView.setmWide(140); //设置高度 suspendRedBagView.setmHeight(160); //设置显示悬浮移动 suspendRedBagView.setSuspend(true); //设置移动速度 suspendRedBagView.setMoveSpeed(3); //设置背景图片 suspendRedBagView.setResourceId(R.mipmap.bubble_red_packet); //点击事件 suspendRedBagView.setOnRedBagClickListener(new SuspendRedBagView.onRedBagClickListener() { @Override public void onRedBagClick() { Toast.makeText(MainActivity.this, "点击红包", Toast.LENGTH_SHORT).show(); } });
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)