animation view click

本文详细介绍了如何在Android应用中通过点位检测与坐标转换实现复杂UI元素的精确定位与交互,包括获取视图的碰撞矩形、应用动画效果后的坐标变换以及逆向坐标转换以实现精准的用户输入处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 private boolean containPoint(View view, float x, float y) {
        Rect rect = new Rect();
        view.getHitRect(rect);
        
        Transformation trans = new Transformation();
        Animation anim = view.getAnimation();
        anim.getTransformation(view.getDrawingTime(), trans);
        
        float dx = view.getLeft();
        float dy = view.getTop();
        Matrix matTranslate = new Matrix();
        matTranslate.postTranslate(dx, dy);
        
        Matrix matrix = trans.getMatrix();
        matrix.postConcat(matTranslate);
        
        // 求逆矩阵
        Matrix mat = new Matrix();
        if (matrix.invert(mat)) {
            float[] pointsSrc = new float[] { x, y };
            float[] pointsEnd = new float[] { 0, 0 };
            
            // Get the point in inverted matrix.
            mat.mapPoints(pointsEnd, pointsSrc);
            // Offset the point because we translate matrix which dx and dy before.
            x = pointsEnd[0] + dx;
            y = pointsEnd[1] + dy;
            x = (int)pointsEnd[0] + (int)dx;
            y = (int)pointsEnd[1] + (int)dy;
        }
        
        return rect.contains((int)x, (int)y);
    }
    
    protected Rect mapRect(Matrix matrix, Rect srcRect) {
        float[] values = new float[9];
        matrix.getValues(values);
        float scaleX = values[0];
        float scaleY = values[4];
        float left = srcRect.left + srcRect.width() * (1 - scaleX) / 2;
        float top = srcRect.top + srcRect.height() * (1 - scaleY) / 2;
        float right = srcRect.top - srcRect.width() * (1 - scaleX) / 2;
        float bottom = srcRect.bottom - srcRect.height() * (1 - scaleY) / 2;
        
        Rect rect = new Rect((int)left, (int)top, (int)right, (int)bottom);
        return rect;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值