How To Create a Rotating Wheel Control?

后端 未结 3 1959
闹比i
闹比i 2020-12-02 17:57

I am trying to implement the Rotatory wheel in android, just like the image displayed below.I came across the tutorial from this link. But i want to impleme

3条回答
  •  广开言路
    2020-12-02 18:28

    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:

    tools:context=".MainActivity">
    
    
    
    
    

提交回复
热议问题