Android自定义View实现QQ消息气泡

 更新时间:2022年08月17日 10:25:12   作者:z真真  
这篇文章主要为大家详细介绍了Android自定义View实现QQ消息气泡,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android自定义View实现QQ消息气泡的具体代码,供大家参考,具体内容如下

效果图:

原理:

控件源码:

public class DragView extends View {

    private int defaultZoomSize = 8;
    //初始化圆的大小
    private int initRadius;
    //圆1的圆心位置
    private PointF center1;
    private PointF center2;

    private PointF point1;
    private PointF point2;
    private PointF point3;
    private PointF point4;

    private int mWidth;
    private int mHeight;

    private float realZoomSize;
    private float currentRadius;
    private float minRadiusScale = 1 / 2f;


    private Paint paint;
    private Path path;
    private Bitmap bitmap;

    @DragStatus
    private int mDragStatus;


    public DragView(Context context) {
        this(context, null);
    }

    public DragView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DragView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(4);
        paint.setAntiAlias(true);

        path = new Path();
        center1 = new PointF();
        center2 = new PointF();
        point1 = new PointF();
        point2 = new PointF();
        point3 = new PointF();
        point4 = new PointF();

        bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon_pot);
        initRadius = Math.min(bitmap.getWidth(), bitmap.getHeight()) / 2;
        Log.e("zhen", "解析bitmap: " + bitmap.getWidth() + " * " + bitmap.getHeight() + " * " + initRadius);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
        center1.set(mWidth / 2, mHeight / 2);
        Log.d("zhen", "圆心位置:x" + center1.x + " y: " + center1.y);
    }

    private boolean isSelected = false;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (Math.sqrt(Math.pow(x - center1.x, 2) + Math.pow(y - center1.y, 2)) < initRadius
                        && mDragStatus == DragStatus.NORMAL) {
                    inAnimation = false;
                    isSelected = true;
                    Log.e("zhen", "选中状态");
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (isSelected) {
//                    Log.d("zhen", "拖动距离: " + dragDistance);
                    if (mDragStatus != DragStatus.DRAG_BACK && mDragStatus != DragStatus.DRAG_TO) {
                        mDragStatus = DragStatus.DRAG_MOVE;
                        center2.set(x, y);
                        float dragDistance = (float) (Math.sqrt(Math.pow(center2.x - center1.x, 2)
                                + Math.pow(center2.y - center1.y, 2)));
                        //多少倍圆的大小
                        realZoomSize = dragDistance / initRadius;
                        invalidate();
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                if (isSelected) {
                    if (realZoomSize <= defaultZoomSize) {
                        //回弹,改变center2.x, center2.y直到等于center1.x, center1.y
                        doAnimation(DragStatus.DRAG_BACK, center2, center1);
                    }
                }
                isSelected = false;
                break;
        }
        return true;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //圆的半径改变
        currentRadius = initRadius * (1 + (minRadiusScale - 1) / defaultZoomSize * realZoomSize);
        if (realZoomSize > defaultZoomSize) {
            //圆缩小为一半,去往目的地,就应该消失了
            doAnimation(DragStatus.DRAG_TO, center1, center2);
        }
        //中间矩形
//        paint.setColor(Color.BLACK);
        float angle = (float) Math.atan((center2.y - center1.y) / (center2.x - center1.x));

        float sinValue;
        float cosValue;
        float controlX;
        float controlY;
        sinValue = (float) Math.abs((currentRadius * Math.sin(angle)));
        cosValue = (float) Math.abs((currentRadius * Math.cos(angle)));
        controlX = (center1.x + center2.x) / 2;
        controlY = (center1.y + center2.y) / 2;
        point1.set(center1.x - sinValue, center1.y - cosValue);
        point2.set(center1.x + sinValue, center1.y + cosValue);
        point3.set(center2.x - sinValue, center2.y - cosValue);
        point4.set(center2.x + sinValue, center2.y + cosValue);

        path.reset();
        switch (mDragStatus) {
            case DragStatus.NORMAL:
                currentRadius = initRadius;
                //原始图片
                canvas.drawBitmap(bitmap, center1.x - initRadius, center1.y - initRadius, paint);
                //起始位置的圆
//                paint.setColor(Color.RED);
//                canvas.drawCircle(center1.x, center1.y, currentRadius, paint);
                break;
            case DragStatus.DRAG_MOVE:
                //拖动过程中
                path.moveTo(point1.x, point1.y);
                path.lineTo(point2.x, point2.y);
                path.quadTo(controlX, controlY, point4.x, point4.y);
                path.lineTo(point3.x, point3.y);
                path.quadTo(controlX, controlY, point1.x, point1.y);
                canvas.drawPath(path, paint);
                //起始位置的圆
                paint.setColor(Color.RED);
                canvas.drawCircle(center1.x, center1.y, currentRadius, paint);
                //结束位置的圆
//                paint.setColor(Color.BLUE);
//                canvas.drawCircle(center2.x, center2.y, currentRadius, paint);
                //原始图片
                canvas.drawBitmap(bitmap, center2.x - initRadius, center2.y - initRadius, paint);
                break;
            case DragStatus.DRAG_BACK:
                //改变center2.x, center2.y直到等于center1.x, center1.y
                path.reset();
                path.moveTo(point1.x, point1.y);
                path.quadTo(center2.x, center2.y, point2.x, point2.y);
                canvas.drawPath(path, paint);
                //起始位置的圆
//                paint.setColor(Color.RED);
//                canvas.drawCircle(center1.x, center1.y, currentRadius, paint);
                //原始图片
                canvas.drawBitmap(bitmap, center1.x - initRadius, center1.y - initRadius, paint);
                break;
            case DragStatus.DRAG_TO:
                //改变center1.x, center1.y,直到等于center2.x, center2.y
                path.reset();
                path.moveTo(point3.x, point3.y);
                path.quadTo(center1.x, center1.y, point4.x, point4.y);
                canvas.drawPath(path, paint);
//                //起始位置的圆
//                paint.setColor(Color.RED);
//                canvas.drawCircle(center1.x, center1.y, currentRadius, paint);
//                //结束位置的圆
//                paint.setColor(Color.BLUE);
//                canvas.drawCircle(center2.x, center2.y, currentRadius, paint);
                //原始图片
                canvas.drawBitmap(bitmap, center2.x - initRadius, center2.y - initRadius, paint);
                break;
        }
//        Log.d("zhen", "dragStatus: " + mDragStatus + " 圆1:" + center1 + " 圆2:" + center2 + " 半径: " + currentRadius);
//        Log.w("zhen", "dragStatus: " + mDragStatus + " point3:" + point3 + " point4" + point4 + " sinValue " + sinValue + " cosValue " + cosValue);
        Log.w("zhen", "dragStatus: " + mDragStatus + " 圆1:" + center1 + " 圆2:" + center2 + " 半径: " + currentRadius);

    }

    int i = 0;
    private boolean inAnimation = false;

    private void doAnimation(int dragStatus, final PointF startPoint, final PointF endPoint) {
        if (inAnimation) return;
        inAnimation = true;
        final int step = 10;
        final float stepx = (endPoint.x - startPoint.x) / step;
        final float stepy = (endPoint.y - startPoint.y) / step;
        i = 1;
        mDragStatus = dragStatus;
        Log.d("zhen", "dragStatus: " + mDragStatus + " startPoint:" + startPoint
                + " endPoint:" + endPoint + " stepx: " + stepy + " stepx: " + stepy);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (i <= step) {
                    startPoint.x += stepx;
                    startPoint.y += stepy;
                    postInvalidate();
                    i++;
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                mDragStatus = DragStatus.NORMAL;
                invalidate();

                Log.e("zhen", "恢复为可拖动状态");
            }
        }).start();
    }


    @IntDef({DragStatus.DRAG_MOVE, DragStatus.DRAG_TO, DragStatus.DRAG_BACK})
    public @interface DragStatus {
        int NORMAL = 0;
        //拖动中
        int DRAG_MOVE = 1;
        //
        int DRAG_TO = 2;
        //回弹
        int DRAG_BACK = 3;
    }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android实现雷达View效果的示例代码

    Android实现雷达View效果的示例代码

    这篇文章主要介绍了Android实现雷达View效果,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • 内存泄漏检测工具LeakCanary源码解析

    内存泄漏检测工具LeakCanary源码解析

    这篇文章主要为大家介绍了内存泄漏检测工具LeakCanary源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • 基于Android RecyclerView实现宫格拖拽效果

    基于Android RecyclerView实现宫格拖拽效果

    在Android发展的进程中,网格布局一直比较有热度,其中一个原因是对用户来说便捷操作,对app厂商而言也会带来很多的曝光量,本篇我们会使用RecyclerView来实现网格拖拽,本篇将结合图片分片案例,实现拖拽效果,需要的朋友可以参考下
    2024-03-03
  • Android开发之背景动画简单实现方法

    Android开发之背景动画简单实现方法

    这篇文章主要介绍了Android开发之背景动画简单实现方法,涉及Android背景动画简单设置与使用技巧,需要的朋友可以参考下
    2017-10-10
  • 实例讲解Android app开发中ListView的基本使用及优化

    实例讲解Android app开发中ListView的基本使用及优化

    这篇文章主要介绍了Android app开发中ListView的基本使用及优化,ListView视图组件是Android中最常用的组件之一需要的朋友可以参考下
    2016-02-02
  • Android应用开发中自定义ViewGroup视图容器的教程

    Android应用开发中自定义ViewGroup视图容器的教程

    这篇文章主要介绍了Android应用开发中自定义ViewGroup视图容器的教程,重点在于View之间的参数传递,文中还讲到了使用ViewDragHelper自定义ViewGroup的方法,需要的朋友可以参考下
    2016-04-04
  • Android编程之重力感应用法分析

    Android编程之重力感应用法分析

    这篇文章主要介绍了Android编程之重力感应用法,结合实例形式较为详细的分析了重力感应的原理、相关概念与实现技巧,需要的朋友可以参考下
    2016-10-10
  • Android中的动态加载机制的学习研究

    Android中的动态加载机制的学习研究

    本篇文章主要介绍了Android中的动态加载机制,对android项目开发有着一定的帮助,有兴趣的同学可以了解一下。
    2016-11-11
  • Android实现系统日历同步日程

    Android实现系统日历同步日程

    这篇文章主要为大家详细介绍了Android实现系统日历同步日程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-04-04
  • 解析Android横竖屏切换的问题

    解析Android横竖屏切换的问题

    本篇文章是对Android中横竖屏切换的问题进行了详细的分析介绍,需要的朋友参考下
    2013-06-06

最新评论