【问题标题】:How to rotate the image without changing its size?如何在不改变大小的情况下旋转图像?
【发布时间】:2012-01-19 09:47:12
【问题描述】:

我正在开发一个 android 应用程序,我必须在其中围绕另一个圆形图像旋转一个圆形图像,但是当我运行它时,我的圆形图像的大小会自动连续变化。所以请帮助我如何解决这个问题。这是我的代码

public class JobSearch extends Activity implements OnTouchListener {
    private ImageView dialer;
    private float y=0;
    public boolean onTouch(View v, MotionEvent event) {
        double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY());
        int rotation=(int)Math.toDegrees(r);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                //x=event.getX();
                y=event.getY();
                updateRotation(rotation);
                break;
            case MotionEvent.ACTION_UP:
                break;
        }//switch       
     return true;    
    }//onTouch
    private void updateRotation(double rot){
        float newRot=new Float(rot);
        Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.round_button_big);
        Matrix matrix=new Matrix();
        matrix.postRotate(newRot);//,bitmap.getWidth()/2,bitmap.getHeight()/2);
        Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
        dialer.setImageBitmap(reDrawnBitmap);
        if(newRot>=-5 && newRot<=5)
            Toast.makeText(this,"12 O\'Clock",Toast.LENGTH_SHORT).show();
        if(newRot>=85 && newRot<=95)
            Toast.makeText(this,"3 O\'Clock",Toast.LENGTH_SHORT).show();
        if(newRot>=175 || newRot<=-175)
            Toast.makeText(this,"6 O\'Clock",Toast.LENGTH_SHORT).show();
        if(newRot>=-95 && newRot<=-85)
            Toast.makeText(this,"9 O\'Clock",Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        dialer = (ImageView) findViewById(R.id.big_button);
        dialer.setOnTouchListener(this);
    }//onCreate
}

【问题讨论】:

    标签: android bitmap matrix rotation


    【解决方案1】:

    这个这个;完美地为我工作。

    private Bitmap rotateBitmap(Bitmap bitmap, int rotationAngleDegree){
    
            int w = bitmap.getWidth();
            int h = bitmap.getHeight();
    
            int newW=w, newH=h;
            if (rotationAngleDegree==90 || rotationAngleDegree==270){
                newW = h;
                newH = w;
            }
            Bitmap rotatedBitmap = Bitmap.createBitmap(newW,newH, bitmap.getConfig());
            Canvas canvas = new Canvas(rotatedBitmap);
    
            Rect rect = new Rect(0,0,newW, newH);
            Matrix matrix = new Matrix();
            float px = rect.exactCenterX();
            float py = rect.exactCenterY();
            matrix.postTranslate(-bitmap.getWidth()/2, -bitmap.getHeight()/2);
            matrix.postRotate(rotationAngleDegree);
            matrix.postTranslate(px, py);
            canvas.drawBitmap(bitmap, matrix, new Paint( Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG ));
            matrix.reset();
    
            return rotatedBitmap;
        }
    

    【讨论】:

      【解决方案2】:

      试试这个代码旋转图像 -

      public class bitmaptest extends Activity {
      @Override
      public void onCreate(Bundle icicle) {
          super.onCreate(icicle);
          LinearLayout linLayout = new LinearLayout(this);
      
          // load the origial BitMap (500 x 500 px)
          Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
                 R.drawable.android);
      
          int width = bitmapOrg.width();
          int height = bitmapOrg.height();
          int newWidth = 200;
          int newHeight = 200;
      
          // calculate the scale - in this case = 0.4f
          float scaleWidth = ((float) newWidth) / width;
          float scaleHeight = ((float) newHeight) / height;
      
          // createa matrix for the manipulation
          Matrix matrix = new Matrix();
          // resize the bit map
          matrix.postScale(scaleWidth, scaleHeight);
          // rotate the Bitmap
          matrix.postRotate(45);
      
          // recreate the new Bitmap
          Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                            width, height, matrix, true);
      
          // make a Drawable from Bitmap to allow to set the BitMap
          // to the ImageView, ImageButton or what ever
          BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
      
          ImageView imageView = new ImageView(this);
      
          // set the Drawable on the ImageView
          imageView.setImageDrawable(bmd);
      
          // center the Image
          imageView.setScaleType(ScaleType.CENTER);
      
          // add ImageView to the Layout
          linLayout.addView(imageView,
                  new LinearLayout.LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
                  )
          );
      
          // set LinearLayout as ContentView
          setContentView(linLayout);
      }
      }
      

      另外,请参阅Tutorial

      【讨论】:

      【解决方案3】:

      这是我围绕自身旋转图像并慢慢降低速度的代码

      rotateAnimation1 = new RotateAnimation(0, 360,
                          Animation.RELATIVE_TO_SELF, 0.5f,
                          Animation.RELATIVE_TO_SELF, 0.5f);
                  rotateAnimation1.setInterpolator(new LinearInterpolator());
                  rotateAnimation1.setDuration(duration);
                  rotateAnimation1.setRepeatCount(0);
                  imgBottle.startAnimation(rotateAnimation1);
      
                  flagSpinAvailable = false;
      
                  rotateAnimation1.setAnimationListener(new AnimationListener() {
                      public void onAnimationStart(Animation anim) {
                      };
      
                      public void onAnimationRepeat(Animation anim) {
                      };
      
                      public void onAnimationEnd(Animation anim) {
                          duration = duration + 70;
                          startMoving();
                      };
                  });
      

      【讨论】:

        【解决方案4】:

        这是旋转图像的代码

        Bitmap bMap = BitmapFactory.decodeResource(getResources(),R.drawable.test);
        Matrix mat = new Matrix();
        mat.postRotate(90);
        Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0,
                                 bMap.getWidth(), bMap.getHeight(), mat, true);
        BitmapDrawable bmd = new BitmapDrawable(bMapRotate);
        image.setImageBitmap(bMapRotate);
        image.setImageDrawable(bmd);
        

        【讨论】:

        • 嘿Nagarjuna,您提供的代码只会旋转图像一次,但我的要求是旋转图像直到用户旋转它。这就是我通过newRot(动态角度)的原因作为 postRotate() 方法的参数。请看我上面的代码。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-22
        • 2021-12-25
        • 1970-01-01
        • 2021-06-13
        相关资源
        最近更新 更多