Android - 用手指旋转 ImageView 会移动其他视图 [英] Android - Rotating an ImageView with finger moves other views around

查看:44
本文介绍了Android - 用手指旋转 ImageView 会移动其他视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经学习 Android 大约两周了(as2/3 方面的专家).

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

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

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

我已将此 ImageView 设置为当用户在其上拖动手指时旋转,就像在记录上刮擦一样.

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

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

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.

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

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!

如果有人能告诉我为什么我需要从旋转中减去 50 以使记录在用户触摸的确切位置下移动,那也会很有帮助!在方法 updateRotation() 中看到这一点.

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().

这是我的 xml 标记:

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>

这是我的java代码:

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);
    }
}

编辑

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

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!

推荐答案

其实那是因为你尝试在线性布局中旋转视图.这将导致其专用空间扩大或缩小.

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

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

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.

这篇关于Android - 用手指旋转 ImageView 会移动其他视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆