0

我想要在右側滑動時旋轉一個瓶子。它的正常工作中棒棒糖棉花糖其無法順利工作。圖像的大小爲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(); 




         } 

        } 

回答

0
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); 
    } 

} 

現在你只需要創建一個圖像視圖和和按鈕來觸發旋轉事件。該兩個版本中工作良好

相關問題