简体   繁体   English

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

[英]Android - Rotating an ImageView with finger moves other views around

I've been learning Android for about two weeks now (expert in as2/3). 我已经学习Android大约两周了(as2 / 3专家)。

I have created a simple LinearLayout1, which contains an ImageView`, containing a png of a vinyl record (just a circular disc basically) 我创建了一个简单的LinearLayout1, which contains an ImageView`,包含一个黑胶唱片的png(基本上只是一个圆盘)

I have set up this ImageView to be rotated when the user drags a finger on it, like scratching a record. 我已将此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. 问题是当ImageView旋转时它会推动其他视图。 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. 正是这些角落正在推动其他视图,并且如果我将ImageView左对齐,则会使记录从屏幕的左侧推开。

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! 我意识到一个简单的答案可能不存在,但请记住我是一个Java菜鸟并尽可能保持简单! 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! 如果有人能告诉我为什么我需要从旋转中减去50以使记录在用户接触的确切位置下移动,那也将是有帮助的! see this in the method updateRotation(). 在updateRotation()方法中看到这个。

Here is my xml markup: 这是我的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>

and here is my java code: 这是我的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);
    }
}

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. 我尝试将ImageView包装到FrameView中 ,这部分有帮助。 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. 我还没有检查过扩展ImageView 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. 这将导致其专用空间扩大或缩小。

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. 想法1:尝试使用框架布局并且您的视图在那里旋转想法2:尝试自定义视图子类并在onDraw中绘制您的转盘。 In the internet look for a compass drawing example for start. 在互联网上寻找一个指南针绘图示例开始。

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 此外,使用FrameLayout将一个图像放在另一个上面,这将解决您的问题

修复你的imageview

imageview.setScaleType(ScaleType.CENTER);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM