包含动态按钮的旋转框架布局

2024-02-08

我有一个 Framelayout,它在运行时添加四个图像视图,并且在中心它包含主图像,用户可以使用该主图像执行不同的操作,但我面临旋转布局视图的问题

目前正在触摸旋转按钮我正在这样做

public void setRotateListener() {
    mRotateImage.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            float x = event.getX(0);
            float y = event.getY(0);
            float theta = getTheta(x, y);

            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_POINTER_DOWN:
                theta_old = theta;
                break;
            case MotionEvent.ACTION_MOVE:
                float delta_theta = theta - theta_old;
                theta_old = theta;
                int direction = (delta_theta > 0) ? 1 : -1;
                angle += 3 * direction;

                Log.d("Tag", "rotate angle : " + obj.getHeight());
                obj.setRotation(angle);
                notifyListener(direction);
                break;
            }
            return true;
        }
    });
}

private float getTheta(float x, float y) {
    float sx = x - (obj.getWidth() / 2.0f);
    float sy = y - (obj.getHeight() / 2.0f);

    float length = (float) Math.sqrt(sx * sx + sy * sy);
    float nx = sx / length;
    float ny = sy / length;
    float theta = (float) Math.atan2(ny, nx);

    final float rad2deg = (float) (180.0 / Math.PI);
    float thetaDeg = theta * rad2deg;

    return (thetaDeg < 0) ? thetaDeg + 360.0f : thetaDeg;
}

but i can't get the expected result i already refer this link as well https://github.com/rprouse/XkcdClock https://github.com/rprouse/XkcdClock as well as try to rotate with gesture and animation too but it seems not working as per my move on the screenwhile on touch the rotate button wants to rotate whole view in both direction clock and anticlockwise


我设计了一个可以满足您需要的布局。 下载演示版here https://ravikoradiya.com/android/StickerDemo/StickerDemo.zip

Java文件

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class ClipArt extends RelativeLayout {
    int baseh;
    int basew;
    int basex;
    int basey;
    ImageButton btndel;
    ImageButton btnrot;
    ImageButton btnscl;
    RelativeLayout clip;
    Context cntx;
    boolean freeze = false;
    int h;
    int i;
    ImageView image;
    String imageUri;
    boolean isShadow;
    int iv;
    RelativeLayout layBg;
    RelativeLayout layGroup;
    RelativeLayout.LayoutParams layoutParams;
    public LayoutInflater mInflater;
    int margl;
    int margt;
    float opacity = 1.0F;
    Bitmap originalBitmap;
    int pivx;
    int pivy;
    int pos;
    Bitmap shadowBitmap;
    float startDegree;
    String[] v;

    public ClipArt(Context paramContext) {
        super(paramContext);
        cntx = paramContext;
        layGroup = this;

        basex = 0;
        basey = 0;
        pivx = 0;
        pivy = 0;

        mInflater = ((LayoutInflater) paramContext.getSystemService("layout_inflater"));
        mInflater.inflate(R.layout.clipart, this, true);
        btndel = ((ImageButton) findViewById(R.id.del));
        btnrot = ((ImageButton) findViewById(R.id.rotate));
        btnscl = ((ImageButton) findViewById(R.id.sacle));
        
        layoutParams = new RelativeLayout.LayoutParams(250, 250);
        layGroup.setLayoutParams(layoutParams);
        image = ((ImageView) findViewById(R.id.clipart));
        image.setImageResource(R.drawable.ic_launcher);
    
        setOnTouchListener(new View.OnTouchListener() {
            final GestureDetector gestureDetector = new GestureDetector(ClipArt.this.cntx,
                    new GestureDetector.SimpleOnGestureListener() {
                public boolean onDoubleTap(MotionEvent paramAnonymous2MotionEvent) {
                    return false;
                }
            });

            public boolean onTouch(View paramAnonymousView, MotionEvent event) {
                if (!ClipArt.this.freeze) {
                    switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        layGroup.invalidate();
                        gestureDetector.onTouchEvent(event);

                        layGroup.performClick();
                        basex = ((int) (event.getRawX() - layoutParams.leftMargin));
                        basey = ((int) (event.getRawY() - layoutParams.topMargin));
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int i = (int) event.getRawX();
                        int j = (int) event.getRawY();
                        layBg = ((RelativeLayout) getParent());
                        if ((i - basex > -(layGroup.getWidth() * 2 / 3))
                                && (i - basex < layBg.getWidth() - layGroup.getWidth() / 3)) {
                            layoutParams.leftMargin = (i - basex);
                        }
                        if ((j - basey > -(layGroup.getHeight() * 2 / 3))
                                && (j - basey < layBg.getHeight() - layGroup.getHeight() / 3)) {
                            layoutParams.topMargin = (j - basey);
                        }
                        layoutParams.rightMargin = -1000;
                        layoutParams.bottomMargin = -1000;
                        layGroup.setLayoutParams(layoutParams);
                        break;

                    }

                    return true;
                }
                return true;
            }
        });
        this.btnscl.setOnTouchListener(new View.OnTouchListener() {
            @SuppressLint({ "NewApi" })
            public boolean onTouch(View paramAnonymousView, MotionEvent event) {
                if (!ClipArt.this.freeze) {
                    int j = (int) event.getRawX();
                    int i = (int) event.getRawY();
                    layoutParams = (RelativeLayout.LayoutParams) layGroup.getLayoutParams();
                    switch (event.getAction()) {

                    case MotionEvent.ACTION_DOWN:
                        ClipArt.this.layGroup.invalidate();
                        ClipArt.this.basex = j;
                        ClipArt.this.basey = i;
                        ClipArt.this.basew = ClipArt.this.layGroup.getWidth();
                        ClipArt.this.baseh = ClipArt.this.layGroup.getHeight();
                        int[] loaction = new int[2];
                        layGroup.getLocationOnScreen(loaction);
                        margl = layoutParams.leftMargin;
                        margt = layoutParams.topMargin;
                        break;
                    case MotionEvent.ACTION_MOVE:

                        float f2 = (float) Math.toDegrees(Math.atan2(i - ClipArt.this.basey, j - ClipArt.this.basex));
                        float f1 = f2;
                        if (f2 < 0.0F) {
                            f1 = f2 + 360.0F;
                        }
                        j -= ClipArt.this.basex;
                        int k = i - ClipArt.this.basey;
                        i = (int) (Math.sqrt(j * j + k * k)
                                * Math.cos(Math.toRadians(f1 - ClipArt.this.layGroup.getRotation())));
                        j = (int) (Math.sqrt(i * i + k * k)
                                * Math.sin(Math.toRadians(f1 - ClipArt.this.layGroup.getRotation())));
                        k = i * 2 + ClipArt.this.basew;
                        int m = j * 2 + ClipArt.this.baseh;
                        if (k > 150) {
                            layoutParams.width = k;
                            layoutParams.leftMargin = (ClipArt.this.margl - i);
                        }
                        if (m > 150) {
                            layoutParams.height = m;
                            layoutParams.topMargin = (ClipArt.this.margt - j);
                        }
                        ClipArt.this.layGroup.setLayoutParams(layoutParams);
                        ClipArt.this.layGroup.performLongClick();
                        break;
                    }
                    return true;

                }
                return ClipArt.this.freeze;
            }
        });
        this.btnrot.setOnTouchListener(new View.OnTouchListener() {
            @SuppressLint({ "NewApi" })
            public boolean onTouch(View paramAnonymousView, MotionEvent event) {
                if (!ClipArt.this.freeze) {
                    layoutParams = (RelativeLayout.LayoutParams) ClipArt.this.layGroup.getLayoutParams();
                    ClipArt.this.layBg = ((RelativeLayout) ClipArt.this.getParent());
                    int[] arrayOfInt = new int[2];
                    layBg.getLocationOnScreen(arrayOfInt);
                    int i = (int) event.getRawX() - arrayOfInt[0];
                    int j = (int) event.getRawY() - arrayOfInt[1];
                    switch (event.getAction()) {

                    case MotionEvent.ACTION_DOWN:
                        ClipArt.this.layGroup.invalidate();
                        ClipArt.this.startDegree = layGroup.getRotation();
                        ClipArt.this.pivx = (layoutParams.leftMargin + ClipArt.this.getWidth() / 2);
                        ClipArt.this.pivy = (layoutParams.topMargin + ClipArt.this.getHeight() / 2);
                        ClipArt.this.basex = (i - ClipArt.this.pivx);
                        ClipArt.this.basey = (ClipArt.this.pivy - j);
                        break;

                    case MotionEvent.ACTION_MOVE:
                        int k = ClipArt.this.pivx;
                        int m = ClipArt.this.pivy;
                        j = (int) (Math.toDegrees(Math.atan2(ClipArt.this.basey, ClipArt.this.basex))
                                - Math.toDegrees(Math.atan2(m - j, i - k)));
                        i = j;
                        if (j < 0) {
                            i = j + 360;
                        }
                        ClipArt.this.layGroup.setRotation((ClipArt.this.startDegree + i) % 360.0F);
                        break;
                    }

                    return true;
                }
                return ClipArt.this.freeze;
            }
        });
        this.btndel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View paramAnonymousView) {
                if (!ClipArt.this.freeze) {
                    layBg = ((RelativeLayout) ClipArt.this.getParent());
                    layBg.performClick();
                    layBg.removeView(ClipArt.this.layGroup);
                }
            }
        });
    }


    public void disableAll() {
        this.btndel.setVisibility(4);
        this.btnrot.setVisibility(4);
        this.btnscl.setVisibility(4);
    }

    public ImageView getImageView() {
        return this.image;
    }

    public void setFreeze(boolean paramBoolean) {
        this.freeze = paramBoolean;
    }
}

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
    <ImageButton android:id="@+id/rotate" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:adjustViewBounds="true" android:background="@android:color/transparent" android:scaleType="fitCenter" android:src="@drawable/rotation"/>
    <ImageButton android:id="@+id/sacle" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:adjustViewBounds="true" android:background="@android:color/transparent" android:scaleType="fitCenter" android:src="@drawable/pointer"/>
    <ImageButton android:id="@+id/del" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:adjustViewBounds="true" android:background="@android:color/transparent" android:scaleType="fitCenter" android:src="@drawable/close"/>
    <ImageView android:id="@+id/clipart" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"/>
    </RelativeLayout>

并将图像放入可绘制对象中

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

包含动态按钮的旋转框架布局 的相关文章

随机推荐

  • 使用 LLVM C API 生成对内部函数的调用

    我正在编写一些使用 LLVM C API 的代码 如何使用内在函数 例如llvm cos f64 or llvm sadd with overflow i32 每当我尝试通过生成一个全局来做到这一点LLVMAddGlobal 具有正确的类型
  • 如何使用ExternalTask​​Sensor触发Airflow DAG独立运行

    我构建了两个 DAG dag a dag b 并在 dag b 中创建了一个刺探 dag a 的ExternalTask Sensor 这些 DAG 有两个用例 同时调度dag a和dag b 并使用依赖关系先处理dag a 然后处理dag
  • SwiftUI 2 弹出到根视图,没有场景委托

    我想在用户注销时刷新根视图 但我找不到如何在新的无场景 SwiftUi 2 下执行此操作 Next is 瑞士马克 https stackoverflow com questions 63461933 swiftui how to chan
  • 将纬度和经度转换为十进制值

    我的 GPS 信息以以下形式呈现 北纬 36 57 9 西经 110 4 21 我可以使用Chris Veness 的 javascript 函数 http www movable type co uk scripts latlong ht
  • Golang,goroutines:恐慌:运行时错误:无效的内存地址

    我对 golang 相当陌生 并试图理解主要原理并使用通道编写基于 gouroutines 的代码 在我使用的其他语言中没有这样的工具 我想知道是否会出现诸如恐慌之类的错误 我的代码 package main import fmt impo
  • jQuery getJSON 不适用于已删除的抽搐频道

    我正在尝试制作一个抽搐频道列表 显示谁在线 离线 并显示该人已删除该帐户的消息 问题是 如果人员帐户不再存在 即使请求返回带有几个属性的 JSON 文件 getJSON 方法内部也不会起作用 代码笔 http codepen io Zach
  • 使用 C# 测量 TCP 上的数据传输速率

    我想测量当前的下载速度 我正在通过 TCP 发送巨大的文件 我怎样才能捕获每秒的传输速率 如果我使用 IPv4InterfaceStatistics 或类似方法 我将捕获设备传输速率 而不是捕获文件传输速率 捕获设备传输速率的问题在于 它捕
  • 自连接查询

    考虑下表 mysql gt select from phone numbers number type person id 17182225465 home 1 19172225465 cell 1 12129876543 home 2 1
  • android中ListView无法点击?

    我使用联系人同步制作了一个应用程序 我列出了以下联系信息 包括照片 姓名和电话号码 我成功地在自定义中列出了所有这些内容ListView 但我无法点击ListView 看起来像是被锁定了 无法点击 但我对另一项活动做了同样的程序 使用自定义
  • 在 LINQ 中对集合的集合进行交集

    我有一个要相交的列表列表 List
  • Hibernate JPA ManyToOne 复合键

    我正在尝试设置我的实体以允许 pks 我的数据库由两个字段组成 Dealer detail id pk user detail id pk 两者都根据相应表中的 id 进行连接 到目前为止我已经尝试过但没有成功 Embeddable pub
  • Git Pull 请求没有更改,但 git diff 显示更改

    我的分支机构有问题 介绍 我的项目有 3 个分支 Dev Main 和 Staging On Dev分支我们添加新功能 Main我们修复错误的分支Staging我们部署用于测试 当 Bug PullRequest 完成时Main分支 我们发
  • 使用 VSTS 和 Azure 时正确管理应用程序设置

    如果使用 VSTS 和 Azure 有两种不同的方法来管理应用程序设置 请记住 这只是用于持续部署的工具的示例 您可以覆盖 VSTS 中 CD 管道中的 JSON 文件 旧系统中的 Web 配置 也可以覆盖 Azure 应用程序设置 部分中
  • ElasticSearch——根据字段值提高相关性

    需要在 ElasticSearch 中找到一种方法来根据字段的特定值提高文档的相关性 具体来说 我的所有文档中有一个特殊字段 该字段值越高 包含该字段的文档就越相关 无论搜索如何 考虑以下文档结构 all enabled true prop
  • 如何在 React-Native (JavaScript) 中显示文本更多/更少

    我正在开发反应本机应用程序 在其中 我们展示了一些描述Text 可能是行数 因此 如果数据超过 3 行 如果扩展 我必须显示更多和更少
  • 如何将 (mxn) 维的 Numpy 数组展开为单个向量

    我只想知道是否有将 numpy 数组展开为单个向量的捷径 例如 将以下 Matlab 代码转换为 python Matlab方式 A 零 10 10 A unroll A 预先感谢 这是你的想法吗 编辑 正如 Patrick 指出的 将 A
  • 有没有办法显示密码执行计划?

    我看到过一个话题 了解 Neo4j Cypher Profile 关键字和执行计划 https stackoverflow com questions 17760627 understanding neo4j cypher profile
  • 是否可以在 p:columns 标签内使用 p:cellEditor?

    我有一个 primefaces 数据表 可以动态获取其列 我希望它的单元格可以使用 primefaces cellEditor 进行编辑 如图所示here http www primefaces org showcase ui datata
  • Android - 全局变量?

    我需要在我的应用程序中存储一些数据 我知道我可以这样做 class public class MyApplication extends Application private String someVariable public Stri
  • 包含动态按钮的旋转框架布局

    我有一个 Framelayout 它在运行时添加四个图像视图 并且在中心它包含主图像 用户可以使用该主图像执行不同的操作 但我面临旋转布局视图的问题 目前正在触摸旋转按钮我正在这样做 public void setRotateListene