2016-04-20 4 views
0

Я использую этот учебник, чтобы сделать вращающийся вид: - Rotating DiallerВращающийся круг в нижней части экрана

Вот код для этого: -

private static Bitmap imageOriginal, imageScaled; 
private static Matrix matrix; 

private ImageView dialer; 
private int dialerHeight, dialerWidth; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    if (imageOriginal == null) { 
     imageOriginal = BitmapFactory.decodeResource(getResources(), R.drawable.dialler); 
    } 

    // initialize the matrix only once 
    if (matrix == null) { 
     matrix = new Matrix(); 
    } else { 
     // not needed, you can also post the matrix immediately to restore the old state 
     matrix.reset(); 
    } 


    dialer = (ImageView) findViewById(R.id.imageView_ring); 
    dialer.setOnTouchListener(new MyOnTouchListener()); 
    dialer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 

     @Override 
     public void onGlobalLayout() { 
      // method called more than once, but the values only need to be initialized one time 
      if (dialerHeight == 0 || dialerWidth == 0) { 
       dialerHeight = dialer.getHeight(); 
       dialerWidth = dialer.getWidth(); 

       // resize 
       Matrix resize = new Matrix(); 
       resize.postScale((float)Math.min(dialerWidth, dialerHeight)/(float)imageOriginal.getWidth(), (float)Math.min(dialerWidth, dialerHeight)/(float)imageOriginal.getHeight()); 
       imageScaled = Bitmap.createBitmap(imageOriginal, 0, 0, imageOriginal.getWidth(), imageOriginal.getHeight(), resize, false); 

       // translate to the image view's center 
       float translateX = dialerWidth/2 - imageScaled.getWidth()/2; 
       float translateY = dialerHeight/2 - imageScaled.getHeight()/2; 
       matrix.postTranslate(translateX, translateY); 

       dialer.setImageBitmap(imageScaled); 
       dialer.setImageMatrix(matrix); 
      } 
     } 
    }); 
} 

private class MyOnTouchListener implements View.OnTouchListener { 

    private double startAngle; 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 

     switch (event.getAction()) { 

      case MotionEvent.ACTION_DOWN: 
       startAngle = getAngle(event.getX(), event.getY()); 
       break; 

      case MotionEvent.ACTION_MOVE: 
       double currentAngle = getAngle(event.getX(), event.getY()); 
       rotateDialer((float) (startAngle - currentAngle)); 
       startAngle = currentAngle; 
       break; 

      case MotionEvent.ACTION_UP: 

       break; 
     } 

     return true; 
    } 

} 

private double getAngle(double xTouch, double yTouch) { 
    double x = xTouch - (dialerWidth/2d); 
    double y = dialerHeight - yTouch - (dialerHeight/2d); 

    switch (getQuadrant(x, y)) { 
     case 1: 
      return Math.asin(y/Math.hypot(x, y)) * 180/Math.PI; 
     case 2: 
      return 180 - Math.asin(y/Math.hypot(x, y)) * 180/Math.PI; 
     case 3: 
      return 180 + (-1 * Math.asin(y/Math.hypot(x, y)) * 180/Math.PI); 
     case 4: 
      return 360 + Math.asin(y/Math.hypot(x, y)) * 180/Math.PI; 
     default: 
      return 0; 
    } 
} 

/** 
* @return The selected quadrant. 
*/ 
private static int getQuadrant(double x, double y) { 
    if (x >= 0) { 
     return y >= 0 ? 1 : 4; 
    } else { 
     return y >= 0 ? 2 : 3; 
    } 
} 

private void rotateDialer(float degrees) { 
    matrix.postRotate(degrees, dialerWidth/2, dialerHeight/2); 

    dialer.setImageMatrix(matrix); 
} 

Это прекрасно работает, когда изображение центр. Мне нужно, чтобы изображение было внизу экрана и полукругом (половина круга была бы за пределами экрана ниже). Что-то вроде этого: - enter image description here

Он должен вращаться при касании пользователя.

Любая помощь будет оценена по достоинству. Спасибо.

ответ

0

Вы можете поместить свой ImageView в RelativeLayout, выровнять его на дно, а затем установить layout_marginBottom как отрицательное значение:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <ImageView 
     android:src="@drawable/graphic_ring" 
     android:id="@+id/imageView_ring" 
     android:layout_height="100dp" 
     android:layout_width="fill_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="-50dp"/> 

</LinearLayout>