【问题标题】:Image rotation works in lollipop but not in marshmallow图像旋转在棒棒糖中有效,但在棉花糖中无效
【发布时间】:2017-05-26 05:02:28
【问题描述】:

我想在向右滑动时旋转一个瓶子。它在 lollipop 中运行良好,但在 marshmallow 中运行不顺畅。图片大小100*100。

请告诉我错误以及如何解决它。

以下是我的代码:

            GestureDetector.OnGestureListener listener = new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

                    // clearAnimation();
                    int sanctity = 50;
                    //
                    Speed = Math.round(velocityX / 1000);
                    if ((e2.getX() - e1.getX()) > sanctity) {
                        swipeLayout.setVisibility(View.GONE);
                        scroller.fling(0, 0, (int) Math.hypot(velocityX, velocityY), 0, 0, Integer.MAX_VALUE, 0, 0);
                        invalidate();


                    }


                    return true;
                }
            };


            @Override
            public void computeScroll() {
                if (scroller.computeScrollOffset()) {
                    firstime = false;
                    float current = scroller.getCurrX();
                    Log.d("computeScroll ", String.valueOf(current));

                    setRotation(current * FACTOR);

                    invalidate();




                        }

                    }

【问题讨论】:

    标签: android android-5.0-lollipop android-6.0-marshmallow image-rotation android-5.1.1-lollipop


    【解决方案1】:
    public class MainActivity extends Activity{
        Button buttonClick;
        ImageView img;
        Bitmap source;
        float angle=0;
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main); 
           img= (ImageView) findViewById(R.id.imgView);
           // Create Bitmap object for the source image
           source=BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/Rose.jpg");
    
           img.setImageBitmap(source);
           buttonClick = (Button) findViewById(R.id.bt);
           buttonClick.setOnClickListener(new OnClickListener(){
              public void onClick(View view){
                  angle+=90;
                  Bitmap rotatedImage=rotateImage(source,angle);
                  img.setImageBitmap(rotatedImage);
              }
           });
    
    
        }
    
        public static Bitmap rotateImage(Bitmap sourceImage, float angle)
        {
            Matrix matrix = new Matrix();
            matrix.postRotate(angle);
            return Bitmap.createBitmap(sourceImage, 0, 0, sourceImage.getWidth(), sourceImage.getHeight(), matrix, true);
        }
    
    }
    

    现在你只需要创建一个图像视图和按钮来触发旋转事件。这在两个版本中都很好用

    【讨论】:

      猜你喜欢
      • 2016-07-30
      • 2016-05-02
      • 2016-10-17
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      • 2016-09-26
      • 1970-01-01
      相关资源
      最近更新 更多