Android 如何创建一个旋转轮控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10222730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:55:24  来源:igfitidea点击:

How To Create a Rotating Wheel Control?

androidanimationrotationandroid-animationimage-rotation

提问by AkashG

I am trying to implement the Rotatory wheelin android, just like the image displayed below.I came across the tutorial from this link. But i want to implement just as shown in the below image.The wheel consists of individual images.Does anybody have any idea regarding this implementation?? Any help would be appreciated.

我正在尝试在 android 中实现旋转轮,就像下面显示的图像一样。我从这个链接中看到了教程。但我想实现如下图所示。轮子由单个图像组成。有人对这个实现有任何想法吗?任何帮助,将不胜感激。

enter image description here

在此处输入图片说明

Thanks in advance.

提前致谢。

Akash

阿卡什

采纳答案by AkashG

Applied on OnTouchListener on the imageView through which i got three events namely:

应用在 imageView 上的 OnTouchListener 上,我通过它获得了三个事件:

  1. MotionEvent.ACTION_DOWN,
  2. MotionEvent.ACTION_MOVE&
  3. MotionEvent.ACTION_UP.
  1. MotionEvent.ACTION_DOWN,
  2. MotionEvent.ACTION_MOVE&
  3. MotionEvent.ACTION_UP

On MotionEvent.ACTION_DOWN got the angle where the users touches and on MotionEvent.ACTION_UP got the angle where user releases.

在 MotionEvent.ACTION_DOWN 上获得用户触摸的角度,在 MotionEvent.ACTION_UP 上获得用户释放的角度。

After getting difference of the two angles,rotated the image of that angle.

得到两个角度的差值后,旋转那个角度的图像。

After rotating the image checked the quadrant through angle and maintained int variable which incremented according to the quadrant and by fulfilling the condition set the new image(the desired one).

旋转图像后,通过角度检查象限并保持 int 变量,该变量根据象限递增,并通过满足条件设置新图像(所需的图像)。

Maintained the click event according to the value of the int variable.

根据int变量的值维护click事件。

回答by radhoo

To do this from scratch, you would need a way to transform your touch coordinates, into polar coordinates (to have the rotation angle). This can be done easily like this:

要从头开始执行此操作,您需要一种将触摸坐标转换为极坐标(以获得旋转角度)的方法。这可以像这样轻松完成:

private float cartesianToPolar(float x, float y) {
  return (float) -Math.toDegrees(Math.atan2(x - 0.5f, y - 0.5f));
}

To rotate the imageview, or the element you are using to display your knob, you can use a matrix like this:

要旋转图像视图或用于显示旋钮的元素,您可以使用如下矩阵:

Matrix matrix=new Matrix();
ivRotor.setScaleType(ScaleType.MATRIX);   
matrix.postRotate((float) deg, m_nWidth/2, m_nHeight/2);//getWidth()/2, getHeight()/2);
ivRotor.setImageMatrix(matrix);

Where deg is the angle and ivRobor is the knob imageview.

其中 deg 是角度,ivRobor 是旋钮图像视图。

A complete working sample for Android, is available on Google code at: https://code.google.com/p/android-rotaryknob-view/

一个完整的 Android 工作示例,可在谷歌代码上找到:https: //code.google.com/p/android-rotaryknob-view/

回答by Ashwani

Here is the complete code for this:

这是完整的代码:

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.graphics.Matrix;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends Activity{
//  Button rotate;
ImageView i;
ImageView ii;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    i= (ImageView) findViewById(R.id.i);
    i.setImageResource(R.drawable.gg);
    ii= (ImageView) findViewById(R.id.ii);
    ii.setImageResource(R.drawable.gg);

    // i.setBackgroundColor(Color.rgb(255, 255, 255));
}

public void ii(View v)
{

    RotateAnimation rotate =
            //new RotateAnimation(0f,generateRandomNumber(),55f,55f);
            new RotateAnimation(0, generateRandomNumber(), 
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

    rotate.setDuration(1500);
    rotate.setInterpolator(new LinearInterpolator());
    i.startAnimation(rotate);
    i.setRotation(generateRandomNumber());
    RotateAnimation rotate1 =
            //new RotateAnimation(0f,generateRandomNumber(),55f,55f);
            new RotateAnimation(0, 999999999, Animation.RELATIVE_TO_SELF, 
 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotate1.setDuration(99999);

    rotate1.setInterpolator(new LinearInterpolator());
    // i= (ImageView) findViewById(R.id.i);
    //     i.setImageResource(R.drawable.g);

    ii.startAnimation(rotate1);

/*i= (ImageView) findViewById(R.id.i);
    i.setImageResource(R.drawable.g);
    ObjectAnimator animator = ObjectAnimator.ofFloat(i,"rotationY", 360f);
    animator.setDuration(1000);
    animator.start();
 */

   /* Matrix matrix = new Matrix();
   i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);   //required
    matrix.postRotate(generateRandomNumber());
  i.setImageMatrix(matrix);
 */




   /* Matrix matrix = new Matrix();
    Bitmap bMap = BitmapFactory.decodeResource(getResources(), 
 R.drawable.g);
   matrix.postRotate(generateRandomNumber());

    Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 
 0,bMap.getWidth(),bMap.getHeight(), matrix, true);
    i.setImageBitmap(bMapRotate);*/
}


public float generateRandomNumber() {

    Random rand = new Random();
    int randomNum = rand.nextInt((10000000 - 125000) + 1);

    return (float)randomNum;
}



int backpressed=0;
@Override
public void onBackPressed() {
    backpressed++;
    if(backpressed>1)
    {
        super.onBackPressed();
        finish();
    }
    else
    {
        Toast.makeText(this, "Press back again to exit", 
  Toast.LENGTH_LONG).show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    Thread.sleep(2000);}
                catch (Exception e)
                {
                    e.printStackTrace();
                }
                backpressed=0;
            }
        });
    }
}
}

Here is the XML:

这是 XML:

tools:context=".MainActivity">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=".............."
    android:textColor="#00ff10"
    android:textStyle="bold"
    android:textSize="25dp"
    android:layout_gravity="center"/>
<ImageView
    android:layout_width="200dp"
    android:layout_height="80dp"
    android:layout_gravity="center"
    android:src="@drawable/a"

    />
<ImageView
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_gravity="center"
    android:id="@+id/i"

    />
<DigitalClock
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_gravity="center"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Play"
    android:backgroundTint="#ff0044"
    android:id="@+id/rotate"
    android:layout_gravity="center"
    android:onClick="ii"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="powered by ashwani"
    android:textColor="#00ff10"
    android:textStyle="bold"
    android:layout_gravity="center"/>
<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:id="@+id/ii"

    />