Android - 用手指旋转 ImageView 会移动其他视图

发布于 2024-10-06 19:03:30 字数 3602 浏览 2 评论 0原文

我已经学习 Android 大约两周了(as2/3 专家)。

我创建了一个简单的LinearLayout1,其中包含一个ImageView`,其中包含一张黑胶唱片的png(基本上只是一个圆盘)

我已经将这个ImageView设置为旋转当用户在其上拖动手指时,就像刮擦唱片一样。

我的代码似乎有效(不确定这是否是最好的方法,但它有效)。问题是当 ImageView 旋转时,它会推动其他视图。我确定这是因为我的圆形图像实际上是一个带有透明角的正方形。正是这些角推动了其他视图,并导致记录从屏幕左侧推开(如果我将 ImageView 左对齐)。

网上有一些解决这个问题的尝试,但它们似乎都不是我所需要的,而且有些例子包含在非常强大的示例中,我无法辨别在我的初级水平上可以从中得到什么。

如果有人能阐明我能做什么,那么解决这个问题那就太好了。我意识到可能不存在简单的答案,但请记住我是 Java 菜鸟,并尽可能保持简单!非常感谢您的帮助!

另外,如果有人能告诉我为什么我需要从旋转中减去 50 才能使记录移动到用户触摸的确切位置下,那也会很有帮助!请参阅 updateRotation() 方法。

这是我的 xml 标记:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/baseView"
    android:background="#FFFFFFFF"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:id="@+id/turntable"
        android:src="@drawable/turntable"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1"/>

</LinearLayout>

这是我的 java 代码:

package com.codingfiend.android.turntable;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

public class Turntable extends Activity
{
    View baseView;
    ImageView turntable;
    TextView bottomText;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        baseView = (View)findViewById(R.id.baseView);
        turntable = (ImageView)findViewById(R.id.turntable);

        turntable.setOnTouchListener(onTableTouched);
    }

    public android.view.View.OnTouchListener onTableTouched = new android.view.View.OnTouchListener()
    {
        public boolean onTouch(View v, MotionEvent evt)
        {
            double r = Math.atan2(evt.getX() - turntable.getWidth() / 2, turntable.getHeight() / 2 - evt.getY());
            int rotation = (int) Math.toDegrees(r);


            if (evt.getAction() == MotionEvent.ACTION_DOWN)
            {
                //
            }

            if (evt.getAction() == MotionEvent.ACTION_MOVE)
            {
                updateRotation(rotation);
            }

            if (evt.getAction() == MotionEvent.ACTION_UP)
            {
                //
            }

            return true;
        }
    };

    private void updateRotation(double rot)
    {
        float newRot = new Float(rot);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.turntable);

        Matrix matrix = new Matrix();
        matrix.postRotate(newRot - 50);

        Bitmap redrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        turntable.setImageBitmap(redrawnBitmap);
    }
}

编辑

这被证明是一个令人恼火的问题。必须有一种方法可以旋转与屏幕一样大或更大的图像,而不会缩放图像,以便整个图像保留在屏幕上。为什么旋转图像的一部分不能离开屏幕?非常令人沮丧。我尝试将 ImageView 包装到 FrameView 中,这在一定程度上有所帮助。现在我可以添加其他不受影响的视图,但我的光盘仍然不够大,因为它在旋转时不允许通过屏幕边框。我不明白为什么。我还没有检查扩展 ImageView 。任何其他想法仍然将不胜感激!

I've been learning Android for about two weeks now (expert in as2/3).

I have created a simple LinearLayout1, which contains anImageView`, containing a png of a vinyl record (just a circular disc basically)

I have set up this ImageView to be rotated when the user drags a finger on it, like scratching a record.

My code seems to be working (not sure if its the best way but it works). The problem is when the ImageView is rotated it pushes other views around. I have determined this is because my round image is really a square with transparent corners. It is these corners that are pushing other views around, and causing the record to push away from the left side of the screen if I have the ImageView left justified.

There are a few attempts to solve this online, but none of them seem to be quite what I need and some are wrapped in examples so robust I can't discern what to take from it at my beginning level.

If anyone could shed some light on what I can do so solve this that would be great. I realize a simple answer may not exist, but keep in mind I am a Java noob and keep it as simple as you can! Many thanks in advance for any help!

also if anyone can tell me why I need to subtract 50 from the rotation to make the record move under the exact spot the user touches, that would be helpful as well! see this in the method updateRotation().

Here is my xml markup:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/baseView"
    android:background="#FFFFFFFF"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:id="@+id/turntable"
        android:src="@drawable/turntable"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1"/>

</LinearLayout>

and here is my java code:

package com.codingfiend.android.turntable;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

public class Turntable extends Activity
{
    View baseView;
    ImageView turntable;
    TextView bottomText;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        baseView = (View)findViewById(R.id.baseView);
        turntable = (ImageView)findViewById(R.id.turntable);

        turntable.setOnTouchListener(onTableTouched);
    }

    public android.view.View.OnTouchListener onTableTouched = new android.view.View.OnTouchListener()
    {
        public boolean onTouch(View v, MotionEvent evt)
        {
            double r = Math.atan2(evt.getX() - turntable.getWidth() / 2, turntable.getHeight() / 2 - evt.getY());
            int rotation = (int) Math.toDegrees(r);


            if (evt.getAction() == MotionEvent.ACTION_DOWN)
            {
                //
            }

            if (evt.getAction() == MotionEvent.ACTION_MOVE)
            {
                updateRotation(rotation);
            }

            if (evt.getAction() == MotionEvent.ACTION_UP)
            {
                //
            }

            return true;
        }
    };

    private void updateRotation(double rot)
    {
        float newRot = new Float(rot);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.turntable);

        Matrix matrix = new Matrix();
        matrix.postRotate(newRot - 50);

        Bitmap redrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        turntable.setImageBitmap(redrawnBitmap);
    }
}

EDIT

This is turning out to be an irritating problem. There has got to be a way to rotate an image that is as big or bigger than the screen without it getting scaled so the whole image stays on screen. Why can't part of the rotating image go off screen? Very frustrating. I tried wrapping the ImageView into a FrameView, which partially helped. Now I can add other views that won't be affected, but I still can't have my disc big enough because it isn't allowed to pass the screen borders when it rotates. I don't get why. I haven't checked out extending ImageView yet. Any other thoughts would still be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一江春梦 2024-10-13 19:03:30

实际上那是因为您尝试以线性布局旋转视图。而这会导致其专用空间扩大或缩小。

想法 1:尝试使用框架布局,并且您的视图在其中旋转
想法 2:尝试自定义视图子类并在 onDraw 中绘制转盘。在互联网上寻找指南针绘图示例作为开始。

Actually thats because you try to rotate a view in a linear layout. And this will cause its dedicated space to expand or shrink.

Idea 1: try using a frame layout have and your view rotate inside there
Idea 2: try a custom view subclass and draw your turntable in the onDraw. In the internet look for a compass drawing example for start.

庆幸我还是我 2024-10-13 19:03:30

修复您的 imageview

通过imageview.setScaleType(ScaleType.CENTER);

Fix your imageview by

imageview.setScaleType(ScaleType.CENTER);

风月客 2024-10-13 19:03:30

简单明了,只需绘制一个圆形画布并将您的图像添加到画布中即可。画布图像不会有角。另外,使用 FrameLayout 将一张图像放在另一张图像之上,这将解决您的问题

Simple and clear, just draw a circle canvas and add your image to the canvas.The canvas image will not have corners. Also, use FrameLayout to lay one image on top of the other, this will solve your problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文