Android的 - 旋转手指的ImageView的移动各地的其他意见 [英] Android - Rotating an ImageView with finger moves other views around

查看:139
本文介绍了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.

我的code似乎是工作(不知道它的最好的方式,但它的工作原理)。问题是,当的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 code:

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 呢。任何其他的想法仍然是AP preciated!

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:尝试使用框架布局已经和您的视图旋转里面有 思路二:尝试自定义视图子类,并绘制转盘中的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天全站免登陆